·  XSLT – Elements – <xsl:sort>

 

- Info :

  - <xsl:for-each> will not return elements in the order they appear in XML file but in the order after elements are sorted.

  - In below example <student> elements are sorted b their <name> subelement.

 

- Example:

 

    test.xml

    <?xml version="1.0"?>

    <?xml-stylesheet type="text/xsl" href="test.xsl"?>

    

    <people>

    

      <student>

        <name>Jack</name>

      </student>

    

      <student>

        <name>Anton</name>

      </student>

    

    </people>

    

    test.xsl

    <?xml version="1.0" ?>

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    

      <xsl:template     match ="/people">      

        <xsl:for-each   select="student">

          <xsl:sort     select="name"/>

          <xsl:value-of select="name"/> <br/>

        </xsl:for-each>

      </xsl:template>

      

    </xsl:stylesheet>

    

    result

    Anton

    Jack