XSLT函数——generate-id()

      generate-id()函数用于为某个节点生成唯一id。参数node用于指定为哪个节点生成唯一id。该id值每次运行XSLT转换时都不相同,而且任意两个节点的id值也不相同。当参数node缺省时,将返回上下文节点的唯一id。

      语法:generate-id(node)

      参数:node节点,可选

     返回值:整数

示例:

xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
    <students>
        <student id="001">
            <name>Mike</name>    
            <mark course="Chinese">80</mark>
        </student>
        <student id="002">
            <name>Linda</name>    
            <mark course="Maths">85</mark>
        </student>
        <student id="003">
            <name>Joe</name>    
            <mark course="English">79</mark>
        </student>
        <student id="002">
            <name>Linda</name>    
            <mark course="Chinese">68</mark>
        </student>
        <student id="001">
            <name>Mike</name>    
            <mark course="English">65</mark>
        </student>
        <student id="003">
            <name>Joe</name>    
            <mark course="Chinese">85</mark>
        </student>
        <student id="003">
            <name>Joe</name>    
            <mark course="Maths">80</mark>
        </student>
        <student id="002">
            <name>Linda</name>    
            <mark course="English">70</mark>
        </student>
        <student id="001">
            <name>Mike</name>    
            <mark course="Maths">90</mark>
        </student>
    </students>

xslt:

  1. <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
         <xsl:output method="xml" version="1.0" encoding="gb2312" indent="yes"/>    
        <xsl:template match="students">
            <xsl:copy>
                <xsl:for-each select="student">
                    <xsl:sort select="@id" data-type="number"/>
                    <xsl:if test="generate-id(.)=generate-id(../student[@id=current()/@id])">
                        <xsl:copy>
                            <xsl:attribute name="id">
                                <xsl:value-of select="@id"/>
                            </xsl:attribute> 
                            <xsl:attribute name="name">
                                <xsl:value-of select="name"/>
                            </xsl:attribute>  
                            <xsl:for-each select="../student[@id=current()/@id]">
                                <xsl:sort select="mark/@course"/>
                                <xsl:copy-of select="mark"/>                        
                            </xsl:for-each>                  
                        </xsl:copy>                
                    </xsl:if>            
                </xsl:for-each>        
            </xsl:copy>
        </xsl:template>
    </xsl:stylesheet>

结果:

  1. <?xml version="1.0" encoding="gb2312"?>
    <students>
        <student id="001" name="Mike">
            <mark course="Chinese">80</mark>
            <mark course="English">65</mark>
            <mark course="Maths">90</mark>
        </student>
        <student id="002" name="Linda">
            <mark course="Chinese">68</mark>
            <mark course="English">70</mark>
            <mark course="Maths">85</mark>
        </student>
        <student id="003" name="Joe">
            <mark course="Chinese">85</mark>
            <mark course="English">79</mark>
            <mark course="Maths">80</mark>
        </student>
    </students>
posted @ 2010-01-17 03:25  Asharp  阅读(2224)  评论(0编辑  收藏  举报