XSLT sort

XSLT <xsl:sort> Element

To sort the output, we can use the <xsl:sort> element.

Where to use the Sort Information:

In the XSL file, we can simply add an <xsl:sort> element inside the <xsl:for-each> element, to sort the output. To specify what XML element to sort on, the select attribute is used.



<?xml version="1.0" encoding="UTF-8"?>
  <book category="Child">
    <title lang="en">ABC</title>
    <author>Author Name</author>
  <book category="IT">
    <title lang="en">XQuery Book</title>
    <author>Author 1</author>
    <author>Author 2</author>
  <book category="Sociology">
    <title lang="en">Sociology 1</title>
    <author>Author Name</author>
  <book category="GK">
    <title lang="en">Current Affairs</title>
    <author>Author Name</author>
  <book category="Science">
    <title lang="en">Science Book</title>
    <author>Author 1</author>
    <author>Author 2</author>
    <author>Author 3</author>

XSLT Code:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
<xsl:template match="/">
    <h2>List of Books</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
      <xsl:for-each select="bookstore/book">
      <xsl:sort select="price"/>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="price"/></td>


Please Share