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

Sorting according to several criteria

Several criteria can be specified to affect sorting order by multiple xsl:sort elements. In the first step the first xsl:sort is used. If the position of nodes cannot be decided according to this criterion these nodes are sorted according to the second one and so on.

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="/">
                  <sss>
                        <xsl:apply-templates  select="/aaa/bbb">
                              <xsl:sort/>
                              <xsl:sort  select="@x"/>
                        </xsl:apply-templates>
                  </sss>
            </xsl:template>

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

      </xsl:stylesheet>
XML

      <aaa>
            <bbb  x="1">x</bbb>
            <bbb  x="2">x</bbb>
            <bbb  x="3">b</bbb>
            <bbb  x="4">b</bbb>
            <bbb  x="5">a</bbb>
      </aaa>
Output

      <sss>
            <bbb  x="5">a</bbb>
            <bbb  x="3">b</bbb>
            <bbb  x="4">b</bbb>
            <bbb  x="1">x</bbb>
            <bbb  x="2">x</bbb>
      </sss>


Previous chapter: Number Formatting
Next chapter: Axes
Previous page: Sorting numbers
Next page: Sorting in xsl:for-each