XSLT <xsl:sort> Element

The XSLT <xsl:sort> element is used to specify a sort criteria on the nodes. It displays the output in sorted form.

The <xml:sort> element is added inside the <xsl:for-each> element in the XSL file, to sort the output.

snippet
<xsl:sort 
   select = string-expression  
   lang = { nmtoken } 
   data-type = { "text" | "number" | QName } 
   order = { "ascending" | "descending" } 
   case-order = { "upper-first" | "lower-first" } > 
</xsl:sort>

Parameter explanation

Index Name Description
1) select It is used for sorting key of the node.
2) lang It specifies language alphabet used to determine sort order.
3) data-type It specifies data-type of the text.
4) order It is used to specify the sorting order. By default sorting order is ascending order.
5) case-order It is used to specify sorting order of string by capitalization. Default is "upper-first".

XSLT <xsl:sort> Element Example

Let's take an example to create a table of <employee> element with its attribute "id" and its child <firstname> , <lastname> , <nickname> , and <salary> by iterating over each student sort them by last name.

Employee.xml

snippet
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "employee.xsl"?> 
<class> 
   <employee id = "001">
      <firstname>Aryan</firstname> 
      <lastname>Gupta</lastname> 
      <nickname>Raju</nickname> 
      <salary>30000</salary>
   </employee> 
   <employee id = "024"> 
      <firstname>Sara</firstname> 
      <lastname>Khan</lastname> 
      <nickname>Zoya</nickname> 
      <salary>25000</salary>
   </employee> 
   <employee id = "056"> 
      <firstname>Peter</firstname> 
      <lastname>Symon</lastname> 
      <nickname>John</nickname> 
      <salary>10000</salary> 
   </employee> 
</class>

Employee.xsl

snippet
<?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>Employee</h2> 
            <table border = "1"> 
               <tr bgcolor = "pink"> 
                  <th>ID</th> 
                  <th>First Name</th> 
                  <th>Last Name</th> 
                  <th>Nick Name</th> 
                  <th>Salary</th> 
               </tr> 
               <xsl:for-each select = "class/employee"> 
               <xsl:sort select = "lastname"/> 
                     <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> 
         </body> 
      </html>
  </xsl:template>  
</xsl:stylesheet>

Output:

XSLT Xsl Sort element 1

Note: The above table is sorted by Last Name.

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +