XSLT <xsl:sort> – Sort XML Nodes in Your Transformation
Introduction – Why Learn <xsl:sort>?
In many XML applications, you need to sort items before displaying them—by name, price, date, or ID. The <xsl:sort> element in XSLT allows you to do just that. It works inside <xsl:for-each> or <xsl:apply-templates> to organize node sets during transformation output.
In this guide, you’ll learn:
- How
<xsl:sort>works and where to use it - Syntax for sorting by text, number, or attribute
- Sort ascending or descending
- Practical examples with
<xsl:for-each>and<xsl:apply-templates>
Syntax of <xsl:sort>
<xsl:sort select="XPathExpression" data-type="text|number" order="ascending|descending"/>
select: XPath expression to use as the sort keydata-type: Optional –text(default) ornumberorder: Optional –ascending(default) ordescending
Sample XML
<catalog>
<book id="102">
<title>XSLT Advanced</title>
<price>599</price>
</book>
<book id="101">
<title>Learn XSLT</title>
<price>499</price>
</book>
<book id="103">
<title>XSLT for Beginners</title>
<price>399</price>
</book>
</catalog>
Example – Sort by Title (Alphabetical)
<xsl:for-each select="catalog/book">
<xsl:sort select="title" data-type="text" order="ascending"/>
<p><xsl:value-of select="title"/></p>
</xsl:for-each>
Output:
- Learn XSLT
- XSLT Advanced
- XSLT for Beginners
Example – Sort by Price (Lowest to Highest)
<xsl:for-each select="catalog/book">
<xsl:sort select="price" data-type="number" order="ascending"/>
<p><xsl:value-of select="title"/> – ₹<xsl:value-of select="price"/></p>
</xsl:for-each>
Output:
- XSLT for Beginners – ₹399
- Learn XSLT – ₹499
- XSLT Advanced – ₹599
Example – Sort by Attribute (@id)
<xsl:for-each select="catalog/book">
<xsl:sort select="@id" data-type="number"/>
<p><xsl:value-of select="title"/></p>
</xsl:for-each>
Sorts books based on their id attributes numerically.
Using <xsl:sort> with <xsl:apply-templates>
<xsl:apply-templates select="catalog/book">
<xsl:sort select="title"/>
</xsl:apply-templates>
Applies sorting when template rules are used instead of inline output.
Things to Remember
<xsl:sort>must appear first inside<xsl:for-each>or<xsl:apply-templates>- Default sort is text and ascending
- Use
data-type="number"for numerical values (e.g., prices, ratings, IDs)
Best Practices for <xsl:sort>
- ✔️ Use
data-type="number"for numeric accuracy - ✔️ Always sort before value-of or output rendering
- ✔️ Chain multiple
<xsl:sort>elements to apply secondary sorting - Don’t forget to check if values exist—missing values may cause blank sorting
Summary – Recap & Next Steps
XSLT <xsl:sort> makes your transformations dynamic and organized. Whether you’re generating a product list, an article feed, or a data report, sorting nodes lets you present XML data clearly and professionally.
Key Takeaways:
- Use
<xsl:sort>inside loops or templates to control output order - Supports text and number sorting
- Combine with attributes, values, or child elements as sort keys
Real-world relevance: Used in sorted report generation, catalog listings, content filters, price lists, and feeds.
FAQs – XSLT <sort>
Can I sort by element text?
Yes. Use <xsl:sort select="title"/> or similar.
Can I sort by multiple criteria?
Yes. Use multiple <xsl:sort> tags in order of priority.
What happens if data-type is missing?
Defaults to text sort.
Can I use @attribute in sort?
Absolutely. Use @id, @price, etc.
Can I reverse the sort order?
Yes. Use order="descending".
Share Now :
