XSLT <sort> Element

XSLT <sort> Element is used to sort the node which is selected. The XSLT <sort> element is added inside the <xsl:for-each> element.

Syntax:

<xsl:sort
select= node
order="ascending" | "descending"
case-order="upper-first" | "lower-first"
lang=XML:LANG-CODE
data-type="text" | "number"
/>

Parameters:

  • select: It is used to select a node with which we sort the node.
  • order: It specifies the ordered sequence.
  • case-order: It specifies the order in which upper case and lower case letters are considered first.
  • lang: It specifies language is to be used by the sort.
  • data-type: It defines which is first sorted alphabets or numbers.

Example 1: In this example, we will sort the element of XML with the help of the age node. Save Both files as mentioned and open xsl file on the Browser, it will show the output as shown below:

XML




<!--File: Test.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl "href="Rule.xsl" ?>
<student>
   <s>
      <name> Divyank Singh Sikarwar </name>
      <branch>CE</branch>
      <age>18</age>
      <city> Agra </city>
   </s>
   <s>
      <name> Aniket Chauhan </name>
      <branch> CSE</branch>
      <age>20</age>
      <city> Shahjahanpur </city>
   </s>
   <s>
      <name> Simran Agarwal</name>
      <branch> CSE</branch>
      <age>23</age>
      <city> Buland Shar</city>
   </s>
   <s>
      <name> Abhay Chauhan</name>
      <branch>ME</branch>
      <age>17</age>
      <city> Shahjahanpur</city>
   </s>
   <s>
      <name> Himanshu Bhatia</name>
      <branch>IT</branch>
      <age>25</age>
      <city> Indore</city>
   </s>
</student>


XML




<!--File: Rule.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>
            <h1 align="center">Students' Basic Details</h1>
            <table border="3" align="center" >
               <tr>
                  <th>Details</th>
               </tr>
               <xsl:for-each select="student/s">
                  <xsl:sort select="age"/>
                  <tr>
                     <td>
                        <xsl:apply-templates select="name"/>
                        <xsl:apply-templates select="branch"/>
                        <xsl:apply-templates select="age"/>
                     </td>
                  </tr>
               </xsl:for-each>
            </table>
         </body>
      </html>
   </xsl:template>
   <xsl:template match="name">
      Name:
      <span style="font-family:cursive;color:#ff0000">
         <xsl:value-of select="."/>
      </span>
      <br />
   </xsl:template>
   <xsl:template match="branch">
      Branch:
      <span style="font-family:serif;color:#0ff000">
         <xsl:value-of select="."/>
      </span>
      <br />
   </xsl:template>
   <xsl:template match="age">
      Age: 
      <span style="font-family:fantsy;color:#0000ff">
         <xsl:value-of select="."/>
      </span>
      <br />
   </xsl:template>
</xsl:stylesheet>


Output:

sort

Example 2: In this example, we will sort the elements of XML in descending order with name node. Save Both files as mentioned and open xsl file on the Browser, it will show the output as shown below:

XML




<!--File: Test.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl "href="Rule.xsl" ?>
<student>
   <s>
      <name> Divyank Singh Sikarwar </name>
      <branch> CSE</branch>
      <age>18</age>
      <city> Agra </city>
   </s>
   <s>
      <name> Aniket Chauhan </name>
      <branch> CSE</branch>
      <age> 20</age>
      <city> Shahjahanpur </city>
   </s>
   <s>
      <name> Simran Agarwal</name>
      <branch> CSE</branch>
      <age> 23</age>
      <city> Buland Shar</city>
   </s>
   <s>
      <name> Abhay Chauhan</name>
      <branch> CSE</branch>
      <age> 17</age>
      <city> Shahjahanpur</city>
   </s>
   <s>
      <name> Himanshu Bhatia</name>
      <branch> IT</branch>
      <age> 25</age>
      <city> Indore</city>
   </s>
</student>


XML




<!--File: Rule.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>
            <h1 align="center">Students' Basic Details</h1>
            <table border="3" align="center" >
               <tr>
                  <th>Name</th>
                  <th>Branch</th>
                  <th>Age</th>
               </tr>
               <xsl:for-each select="student/s">
                  <xsl:sort select="name" order="descending"/>
                  <tr>
                     <td>
                        <xsl:apply-templates select="name"/>
                     </td>
                     <td>
                        <xsl:apply-templates select="branch"/>
                     </td>
                     <td>
                        <xsl:apply-templates select="age"/>
                     </td>
                  </tr>
               </xsl:for-each>
            </table>
         </body>
      </html>
   </xsl:template>
   <xsl:template match="name">
      <span style="color:#ff0000">
         <xsl:value-of select="."/>
      </span>
      <br />
   </xsl:template>
   <xsl:template match="branch">
      <span style="color:#0ff000">
         <xsl:value-of select="."/>
      </span>
      <br />
   </xsl:template>
   <xsl:template match="age">
      <span style="color:#0000ff">
         <xsl:value-of select="."/>
      </span>
      <br />
   </xsl:template>
</xsl:stylesheet>


Output:

sort