Previous | Next | Indexes
Zvon > Tutorials > XSLT 2.0 Tutorial > Sorting (1/8) >

Sorting nodes - xsl:sort

Nodes selected by xsl:apply-templates can be alphabetically sorted using xsl:sort element as a child of the instruction. If there is no select attribute on the xsl:sort element then the text content of the nodes is used as the sorting criterion.

XSLT

      <xsl:stylesheet
                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                  version="2.0">
            <xsl:output  method="xml"
                        indent="yes"
                        omit-xml-declaration="yes"/>

            <xsl:template  match="/">
                  <xsl:apply-templates  select="/aaa/bbb">
                        <xsl:sort/>
                  </xsl:apply-templates>
            </xsl:template>

            <xsl:template  match="bbb">
                  <bbb>
                        <xsl:value-of  select="."/>
                  </bbb>
            </xsl:template>

      </xsl:stylesheet>
XML

      <aaa>
            <bbb>C</bbb>
            <bbb>A</bbb>
            <bbb>D</bbb>
            <bbb>B</bbb>
      </aaa>
Output

      <bbb>A</bbb>
      <bbb>B</bbb>
      <bbb>C</bbb>
      <bbb>D</bbb>


Previous chapter: Number Formatting
Next chapter: Axes
Previous page: - - -
Next page: Select attribute of xsl:sort