Updated April 18, 2023
Introduction to XSLT sort
XSLT sort is defined as the sorting element for the nodes that specifies the order in which the input element is processed. <xsl: sort> is inserted inside the <xsl: for-each> element in the XSL file. Sorting is necessary when presenting data for application purpose. XSLT sorts alphabetical and numeric sorting too with a single operation performed with a sheet. It makes use of sorting iterators for sorting nodes which is enclosed in sorting objects. The XSLT processor evaluates during runtime.
Syntax:
xsl:sort
<case-order="ascending" | "descending"
data-type="number" "name" | "text"
lang="lan_code"
order="ascending" | " descending"
< select="expression" // value of select>
</xsl:sort>
The sort key above determines the preference order where it occurs and the duplicate values are neglected. It starts with the open-angle bracket and ends with the separate closing element. The default order is ascending.
How does sort Function Work in XSLT?
The ordering of XML data within a structure is defined by XML schemas. Here we shall see how to sort records in an XML document using one particular item list. Sort function helps in determining the order of processing by the node selected by <xsl: apply-templates> or for-each element. That is using two-templates in XSLT sheets. They could have several child nodes on sort defined as primary, secondary and so on.
<xsl:for-each>
<xsl:for-each select="/Airline/type/economy">
<xsl:value-of select="Airline/name"/>
Next is the template match to process our data.
<xsl:template match="Rootelement">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="root">
<xsl:sort select="child element1" order=”ascending| descending:>
</xsl:template match>
Attributes of XSLT sort
This sort covers several attributes and it occurs in previously mentioned two elements.
1. select
This attribute selects the content of the characters that are to be sort and it is mentioned in the XPATH expressions whereby we can select the text0, comments and the ancestor’s nodes etc. This specifies what elements to sort with and their value is an expression.
2. data_type
This includes three values text, number and qname respectively. Here the qname specifies a particular datatype. Text sorting is a Unicode text value. In the case of the numeric type, they are sorted according to a numeric value. If the strings do not match the value they are assigned as zeros.
All these data types are defined in XML specifications and it is supported here. The resulting string value is the sort key of the respective node.
Numeric Sorting
The values are ordered numerically with an actual value. This provides a more precise result.
<xsl:template match="/">
<xsl:for-each select="/book/type/author">
<xsl:sort select="./bookdb" data-type="number" order="ASCENDING" />
bookdb: <xsl:value-of select="./bookdb"/>
</xsl:for-each>
</xsl:template>
It returns in different ordering like 0,02.
Examples of XSLT sort
Given below are the examples of XSLT sort:
Example #1
Let’s create an example to create table kids with the attribute id and the child elements by iterating the elements.
Code:
kids.xml
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "kinder.xsl"?>
<Grade>
<kids id = "520">
<fname>Dallic</fname>
<Mname>Kate</Mname>
<Address>Vegas</Address>
<points>87</points>
</kids>
<kids id = "526">
<fname>Blippi</fname>
<Mname>horne</Mname>
<Address>California</Address>
<points>82</points>
</kids>
<kids id = "521">
<fname>Thomas</fname>
<Mname>Mark</Mname>
<Address>Arizona</Address>
<points>87</points>
</kids>
</Grade>
kids.xsl
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:template match = "/">
<html>
<body>
<h2>Kids</h2>
<table border = "1">
<tr bgcolor = "#9acd32">
<th>id</th>
<th>fname</th>
<th>Mname</th>
<th>Address</th>
<th>points</th>
</tr>
<xsl:for-each select = "Grade/kids">
<xsl:sort select = "fname"/>
<tr>
<td><xsl:value-of select = "@id"/></td>
<td><xsl:value-of select = "fname"/></td>
<td><xsl:value-of select = "Mname"/></td>
<td><xsl:value-of select = "Address"/></td>
<td><xsl:value-of select = "points"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Explanation:
- Here is the result we get from the first defined stylesheet after sorting. Here the id is magically reordered with the XSLT.
Output:
Example #2
Sorting in alphabetical order.
Code:
alp.xml
<?xml version="1.0" encoding="UTF-8"?>
<Aquarium>
<category name="sea_animal">
<sea>
<name>FloridaAquarium</name>
<place>Tampa</place>
<No_Species>100</No_Species>
<years>34</years>
</sea>
<sea>
<name>GeorgiaAquarium</name>
<place>Atlanta</place>
<No_Species>110</No_Species>
<years>29</years>
</sea>
<sea>
<name>MonteryBay </name>
<place>Montery</place>
<No_Species>214</No_Species>
<years>33</years>
</sea>
</category>
<category name="whales">
<sea>
<name>New England</name>
<place>Boston</place>
<No_Species>301</No_Species>
<years>95</years>
</sea>
<sea>
<name>Mysticpen Aquarium</name>
<place>california</place>
<No_Species>214</No_Species>
<years>147</years>
</sea>
</category>
</Aquarium>
alp.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="/Aquarium/category/sea">
<xsl:sort select="./name" order="ASCENDING" />
sea: <xsl:value-of select="./name"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Explanation:
- The XSL transformation selects all the elements in the aquarium and orders the name element alphabetically in an ascending order format.
Output:
Example #3
Code:
alp.xml
<?xml version="1.0" encoding="UTF-8"?>
<listing>
<list1>25</list1>
<list1>13</list1>
<list1>zzz</list1>
<list1>11</list1>
</listing>
alp.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="no"/>
<xsl:strip-space elements="*"/>
<xsl: variable name="demo">
<xsl:text>
</xsl:text>
</xsl:variable>
<xsl:template match="/">
<xsl:for-each select="listing/list1">
<xsl:sort select="./>
<xsl:value-of select="."/>
<xsl:value-of select="$demo"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Explanation:
- The result is based on the text when the XSLT processor process the data.
- Therefore, the output sorts the text value ‘zzz’ in the order.
- When the mode is changed to ‘number’ it gives the output as 25, 13,11.
Output:
Example #4
Using descending sort.
Code:
emp.xml
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "kinder.xsl"?>
<edu_employee>
<blogger id="10">
<name> Sunitha Eswaraiah</name>
<age>29</age>
<Address>India</Address>
</blogger>
<blogger id="12">
<name> Lucia henry</name>
<age>25</age>
<Address>USA</Address>
</blogger>
<blogger id="16">
<name>Billiard Kay</name>
<age>35</age>
<Address>Boston</Address>
</blogger>
</edu_employee>
emp.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="edu_employee/blogger">
<xsl:sort select="age" order="descending" />
<xsl:value-of select="name" />
<xsl:text> - </xsl:text>
<xsl:value-of select="age" />
<br />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Explanation:
- This is the code for XSL file where the value of age is sorted in descending order.
Output:
Example #5
Using perform_sort in XSLT.
Code:
sa.xml
<?xml version="1.0"?>
<sales>
<sale id="s1" price="3012" load="8" country="Sweden"/>
<sale id="s2" price="1200" load="7" country="Germany"/>
<sale id="s3" price="1500" load="16" country="Norway"/>
<sale id="s4" price="1400" load="6" country="Paris"/>
<sale id="s5" price="1325" load="3" country="Japan"/>
</sales>
sa.xsl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="/">
<xsl:variable name="sale">
<xsl:perform-sort select="sales/sale">
<xsl:sort select="@load" data-type="number" order="descending"/>
<xsl:sort select="@country" order="ascending"/>
</xsl:perform-sort>
</xsl:variable>
<products>
<xsl:copy-of select="$sale"/>
</products>
</xsl:template>
</xsl:stylesheet>
Explanation:
- Here in the above code, we have mentioned the number attribute to sort in ascending order and the output is shown below.
Output:
Conclusion
Therefore, in this article, we have seen how to sort a few XML elements with simple examples using XSLT style sheets and generate output with these sorted elements. This <xsl: sort> rearranges a group of elements based on the criteria defined in the XSL Sheet.
Recommended Articles
This is a guide to XSLT sort. Here we discuss the introduction, working, attributes, numeric sorting and examples respectively. You may also have a look at the following articles to learn more –