XSLT `xsl:sort` Element: Sorting XML Data for Enhanced Output Presentation
Learn how to sort XML data in your XSLT transformations using the `xsl:sort` element. This tutorial explains its attributes (`select`, `order`, `data-type`, `lang`, `case-order`), demonstrating how to sort nodes based on various criteria (element values, attributes) and control sorting order (ascending, descending) for creating well-organized and readable output.
Using the XSLT `xsl:sort` Element for Sorting XML Output
Introduction
In XSLT (Extensible Stylesheet Language Transformations), the `xsl:sort` element is used to sort the nodes selected by an XSLT template. This allows you to present your XML data in a specific order in the transformed output.
`xsl:sort` Element Parameters
The `xsl:sort` element has several attributes that control the sorting process. These attributes are added within the `xsl:for-each` element or other similar elements.
| Index | Attribute | Description |
|---|---|---|
| 1 | select |
Specifies the node or expression to use as the sorting key. |
| 2 | lang |
Specifies the language to use for sorting strings (affects collation). |
| 3 | data-type |
Specifies the data type of the sorting key (e.g., `text`, `number`). |
| 4 | order |
Specifies the sort order (`ascending` or `descending`). Defaults to `ascending`. |
| 5 | case-order |
Specifies the case-sensitivity for string sorting (e.g., `upper-first`, `lower-first`, `upper-first`). Defaults to `upper-first`. |
Example: Sorting Employee Data by Last Name
Let's say you have an XML file (Employee.xml) containing employee data, and you want to sort this data by the employee's last name using an XSLT stylesheet (Employee.xsl).
Employee.xml (Sample XML Data)
<Employees>
<Employee id="1">
<FirstName>Aryan</FirstName>
<LastName>Gupta</LastName>
<NickName>Raju</NickName>
<Salary>30000</Salary>
</Employee>
<Employee id="2">
<FirstName>Sara</FirstName>
<LastName>Khan</LastName>
<NickName>Zoya</NickName>
<Salary>25000</Salary>
</Employee>
<Employee id="3">
<FirstName>Peter</FirstName>
<LastName>Symon</LastName>
<NickName>John</NickName>
<Salary>10000</Salary>
</Employee>
</Employees>
Employee.xsl (XSLT Stylesheet)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<table border="1">
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Salary</th>
</tr>
<xsl:for-each select="Employees/Employee">
<xsl:sort select="LastName" /> <!-- Sorting by Last Name -->
<tr>
<td><xsl:value-of select="@id"/></td>
<td><xsl:value-of select="FirstName"/></td>
<td><xsl:value-of select="LastName"/></td>
<td><xsl:value-of select="NickName"/></td>
<td><xsl:value-of select="Salary"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
Example Output
<table border="1">
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Salary</th>
</tr>
<tr>
<td>2</td>
<td>Sara</td>
<td>Khan</td>
<td>Zoya</td>
<td>25000</td>
</tr>
<tr>
<td>1</td>
<td>Aryan</td>
<td>Gupta</td>
<td>Raju</td>
<td>30000</td>
</tr>
<tr>
<td>3</td>
<td>Peter</td>
<td>Symon</td>
<td>John</td>
<td>10000</td>
</tr>
</table>
Conclusion
The `xsl:sort` element is a fundamental part of XSLT for controlling the order of elements in the transformed output. Understanding its attributes allows you to create well-structured and easily readable XML transformations.