How Does xsl:sort Reorder Nodes in XSLT?
The <xsl:sort> element in XSLT is used inside
<xsl:for-each> loops or
<xsl:apply-templates> instructions to reorder the
processed node sequence before template rules execute. Rather than
modifying the underlying XML source document, XSLT evaluates the sorting
criteria specified by the stylesheet author, generates a sorted
intermediate node list in memory, and iterates over those nodes in the
newly defined order. Understanding its core attributes and multi-level
sorting capabilities enables precise control over XML transformation
output.
Core Attributes Governing Sort Behavior
The sorting engine relies on several attributes to determine how nodes are compared and arranged:
select: Defines the XPath expression evaluated relative to the current node being sorted. The resulting value serves as the sort key. If omitted, the string-value of the context node itself (.) is used.order: Specifies the sort direction. It accepts eitherascending(the default) ordescending.data-type: Determines whether values are compared as plain text (text, the default) or numeric values (number). Setting this correctly prevents numerical errors, such as10appearing before2in standard alphanumeric sorting.case-order: Directs whether uppercase or lowercase characters take precedence in textual comparisons, acceptingupper-firstorlower-first.lang: Declares the language whose collation rules should be applied during textual sorting (for example,lang="en"orlang="de").
Execution Flow and Position Handling
When an XSLT processor encounters <xsl:sort>
children within <xsl:apply-templates> or
<xsl:for-each>, it alters the sequence of
evaluation:
- Node Selection: The processor first selects the
initial sequence of nodes matching the
selectexpression of the parent instruction. - Key Evaluation: For every node in the selected
sequence, the processor calculates the sort key defined in
<xsl:sort>. - Sequence Reordering: The nodes are arranged into a new sequence based on the calculated sort keys and comparison attributes.
- Context Update: When processing the sorted nodes,
dynamic context functions like
position()andlast()reflect the new, sorted sequence rather than document order.
Multi-Level Sorting
Complex data sets often require tie-breaking when two or more nodes
share identical values in their primary sort key. In XSLT, multi-level
sorting is achieved by declaring multiple <xsl:sort>
elements consecutively. The processor treats the first
<xsl:sort> element as the primary key, the second as
the secondary key, and so on.
<xsl:template match="catalog">
<xsl:apply-templates select="product">
<xsl:sort select="category" order="ascending" data-type="text"/>
<xsl:sort select="price" order="descending" data-type="number"/>
</xsl:apply-templates>
</xsl:template>In this pattern, all products are first grouped alphabetically by category. Within each individual category, products are subsequently ordered from highest price to lowest.