XSLT <xsl:sort> Element

The <xsl:sort> element is used to sort the output.

Where to put the Sort Information

To sort the output, simply add an <xsl:sort> element inside the <xsl:for-each> element in the XSL file:


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"

<xsl:template match="/">
  <h2>My CD Collection</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
    <xsl:for-each select="catalog/cd">
      <xsl:sort select="artist"/>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="artist"/></td>


Try it yourself »

Note: The select attribute indicates what XML element to sort on.

Color Picker