sort_1-input.xml
<?xml version="1.0"?>
<products>
<product
id="p1" price="3250" stock="4"/>
<product
id="p2" price="1000" stock="5"/>
<product
id="p3" price="1200" stock="19"/>
<product
id="p4" price="1500" stock="5"/>
<product
id="p5" price="1225" stock="3"/>
</products>
sort_1-stylesheet.xsl
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output
indent="yes"/>
<xsl:template
match="/">
<products>
<xsl:for-each
select="products/product">
<xsl:sort
select="@stock" data-type="number" order="descending"/>
<product
id="{@id}" price="{@price}" stock="{@stock}"/>
</xsl:for-each>
</products>
</xsl:template>
</xsl:stylesheet>
sort_1-output.xml
<?xml version="1.0" encoding="UTF-8"?>
<products>
<product
id="p3" price="1200" stock="19"/>
<product
id="p2" price="1000" stock="5"/>
<product
id="p4" price="1500" stock="5"/>
<product
id="p1" price="3250" stock="4"/>
<product
id="p5" price="1225" stock="3"/>
</products>
xsl:sort can be used inside xsl:for-each, xsl:for-each-group, xsl:apply-templates and inside xsl:perform-sort (XSLT 2.0). All attributes in xsl:sort are optional. Some of the more important are used in example.
Updated 2009-03-19