perform-sort_1-input.xml
<?xml version="1.0"?>
<products>
<product
id="p1" price="3250" stock="4" country="Denmark"/>
<product
id="p2" price="1000" stock="5" country="Germany"/>
<product
id="p3" price="1200" stock="19" country="Germany"/>
<product
id="p4" price="1500" stock="5" country="Australia"/>
<product
id="p5" price="1225" stock="3" country="Japan"/>
</products>
perform-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="/">
<xsl:variable
name="product">
<xsl:perform-sort
select="products/product">
<xsl:sort
select="@stock" data-type="number" order="descending"/>
<xsl:sort
select="@country" order="ascending"/>
</xsl:perform-sort>
</xsl:variable>
<products>
<xsl:copy-of
select="$product"/>
</products>
</xsl:template>
</xsl:stylesheet>
perform-sort_1-output.xml
<?xml version="1.0" encoding="UTF-8"?>
<products>
<product
id="p3" price="1200" stock="19" country="Germany"/>
<product
id="p4" price="1500" stock="5" country="Australia"/>
<product
id="p2" price="1000" stock="5" country="Germany"/>
<product
id="p1" price="3250" stock="4" country="Denmark"/>
<product
id="p5" price="1225" stock="3" country="Japan"/>
</products>
In XSLT 2.0 xsl:perform-sort can be used to sort the content of e.g. a variable without using xsl:for-each or xsl:apply-templates.
Updated 2009-03-19