xsl:sort
Specifies a sort criterion as a child of xsl:for-each, xsl:apply-templates, xsl:perform-sort, or xsl:for-each-group.
<xsl:for-each select="item">
<xsl:sort select="@price" data-type="number"/>
...
</xsl:for-each>xsl:sort is not a stand-alone instruction — it modifies the iteration order of its parent. Multiple xsl:sort children act as primary, secondary, etc. sort keys in document order. Sort keys must come before any other children.
When data-type is not set and the sort key returns a numeric value, goxslt sorts numerically by default (XSLT 2.0+ behavior). Non-numeric values in a numeric sort are treated as NaN and sort before all numbers.
Attributes
| Attribute | Description |
|---|---|
select |
XPath expression for the sort key. Default: . (the context item). |
order |
ascending (default) or descending. AVT. |
data-type |
text or number. AVT. |
lang |
Language code for collation. |
case-order |
upper-first or lower-first. AVT. |
collation |
Collation URI. |
Examples
Two-key sort:
<xsl:for-each select="book">
<xsl:sort select="@year" data-type="number" order="descending"/>
<xsl:sort select="title"/>
...
</xsl:for-each>Order chosen by an external parameter:
<xsl:apply-templates select="row">
<xsl:sort select="@name" order="{$direction}"/>
</xsl:apply-templates>