sorts in numeric mode. : Sort « XSLT stylesheet « XML






sorts in numeric mode.

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
  <car id="11"/>
  <car id="6"/>
  <car id="105"/>
  <car id="28"/>
  <car id="9"/>
</data>

File: Transform.xslt

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
      <TABLE>
        <xsl:for-each select="//car">
          <xsl:sort data-type="number" select="@id"/>
          <TR>
            <TH>
              <xsl:text>Car-</xsl:text>
              <xsl:value-of select="@id"/>
            </TH>
          </TR>
        </xsl:for-each>
      </TABLE>
    </xsl:template>
</xsl:stylesheet>

Output:

<?xml version="1.0" encoding="UTF-8"?><TABLE><TR><TH>Car-6</TH></TR><TR><TH>Car-9</TH></TR><TR><TH>Car-11</TH></TR><TR><TH>Car-28</TH></TR><TR><TH>Car-105</TH></TR></TABLE>

 








Related examples in the same category

1.Sort value first then output
2.sort select="salary" data-type="number" order="descending"
3.Sort by two columns
4.Sort by attribute value
5.Sort by substring
6.sort by different level of node
7.sort element by data type
8.for each sort descending
9.sort with current-grouping-key() function
10.sort by attribute
11.Sort by element text
12.Set sort order as ascending
13.sorts in text
14.sorts upercase letters first
15.sorts lowercase letters first