XSLT <sort>

Sorting XML data in your output

🔢 What is <xsl:sort>?

The <xsl:sort> element sorts nodes before processing them. It's used inside <xsl:for-each> or <xsl:apply-templates> to arrange data alphabetically, numerically, or in custom order.


<!-- Sort by title -->
<xsl:for-each select="book">
    <xsl:sort select="title"/>
</xsl:for-each>
                                    

Understanding <sort>

The <xsl:sort> element organizes XML nodes in a specific order before they are processed. You can sort alphabetically, numerically, in ascending or descending order, making your output data well-organized and easy to read.

🔤

Alphabetical Sort

Sort text data A-Z

<xsl:sort select="name"/>
🔢

Numerical Sort

Sort numbers correctly

<xsl:sort select="price" 
         data-type="number"/>
⬆️

Ascending Order

Low to high (default)

order="ascending"
⬇️

Descending Order

High to low

order="descending"

🔹 Basic Alphabetical Sort

Sort books alphabetically by title:

XML Data:

<?xml version="1.0"?>
<library>
    <book><title>Zebra Tales</title></book>
    <book><title>Apple Recipes</title></book>
    <book><title>Mountain Climbing</title></book>
</library>

XSLT Template:

<xsl:template match="library">
    <h3>Books (Sorted A-Z):</h3>
    <ul>
        <xsl:for-each select="book">
            <xsl:sort select="title"/>
            <li><xsl:value-of select="title"/></li>
        </xsl:for-each>
    </ul>
</xsl:template>

Output:

Books (Sorted A-Z):

  • Apple Recipes
  • Mountain Climbing
  • Zebra Tales

🔹 Numerical Sort

Sort products by price using numerical sorting:

XML Data:

<products>
    <product>
        <name>Laptop</name>
        <price>999</price>
    </product>
    <product>
        <name>Mouse</name>
        <price>25</price>
    </product>
    <product>
        <name>Keyboard</name>
        <price>75</price>
    </product>
</products>

XSLT Template:

<xsl:template match="products">
    <h3>Products by Price:</h3>
    <ul>
        <xsl:for-each select="product">
            <xsl:sort select="price" data-type="number"/>
            <li>
                <xsl:value-of select="name"/> - 
                $<xsl:value-of select="price"/>
            </li>
        </xsl:for-each>
    </ul>
</xsl:template>

Output:

Products by Price:

  • Mouse - $25
  • Keyboard - $75
  • Laptop - $999

🔹 Descending Order Sort

Sort in reverse order from high to low:

XML Data:

<students>
    <student>
        <name>Alice</name>
        <score>85</score>
    </student>
    <student>
        <name>Bob</name>
        <score>92</score>
    </student>
    <student>
        <name>Charlie</name>
        <score>78</score>
    </student>
</students>

XSLT Template:

<xsl:template match="students">
    <h3>Top Scores:</h3>
    <ol>
        <xsl:for-each select="student">
            <xsl:sort select="score" data-type="number" 
                      order="descending"/>
            <li>
                <xsl:value-of select="name"/> - 
                <xsl:value-of select="score"/> points
            </li>
        </xsl:for-each>
    </ol>
</xsl:template>

Output:

Top Scores:

  1. Bob - 92 points
  2. Alice - 85 points
  3. Charlie - 78 points

🔹 Multiple Sort Criteria

Sort by multiple fields - first by category, then by name:

XML Data:

<items>
    <item category="B">Zebra</item>
    <item category="A">Lion</item>
    <item category="B">Apple</item>
    <item category="A">Tiger</item>
</items>

XSLT Template:

<xsl:template match="items">
    <ul>
        <xsl:for-each select="item">
            <xsl:sort select="@category"/>
            <xsl:sort select="."/>
            <li>
                [<xsl:value-of select="@category"/>] 
                <xsl:value-of select="."/>
            </li>
        </xsl:for-each>
    </ul>
</xsl:template>

Output:

  • [A] Lion
  • [A] Tiger
  • [B] Apple
  • [B] Zebra

🔹 Sort Attributes

Common attributes used with <xsl:sort>:

  • select: Specifies which node to sort by
  • data-type: "text" (default) or "number"
  • order: "ascending" (default) or "descending"
  • case-order: "upper-first" or "lower-first"
<xsl:sort select="price" 
          data-type="number" 
          order="descending"/>

💡 Key Points:

  • Always use inside <xsl:for-each> or <xsl:apply-templates>
  • Use data-type="number" for numerical sorting
  • Default order is ascending (A-Z, 0-9)
  • Can sort by multiple criteria using multiple <xsl:sort> elements
  • Sort happens before the loop processes nodes

🧠 Test Your Knowledge

What attribute sorts numbers correctly?