The xsl:sort element is used to define a sort key. This sort key determines the order in which selected nodes are processed by the xsl:for-each or xsl:apply-templates elements.
A sort can be based upon more than one xsl:sort element. Each sort is applied in the order in which it occurs. Duplicate values are left in document order. After the first sort has reordered the nodes, the second sort is applied to any nodes that had duplicate values in the first sort. The third sort is applied to any nodes that had duplicate values in the second sort, and so on.
This is not a self-closing element. The separate closing element is mandatory.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="devguru_staff/programmer">
<xsl:sort select="age" order="descending" />
<xsl:value-of select="name" />
<xsl:text> - </xsl:text>
<xsl:value-of select="age" />
<br />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Daisy Duck - 51
Road Runner - 48
Porky Pig - 44
Bugs Bunny - 31
Minnie Mouse - 24
Pluto - 21
This is the code for xslt_example_sort.xsl.
We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_sort.xsl"?>