>> English << | Français | Deutsch | Magyar | 中文 | Polski ZVON > Tutorials > XSLT Tutorial
>> Page 20 << | Prev | Next | Contents | Element Index

XSLT stylesheet 1 sorts in text and XSLT stylesheet 2 in numeric mode. Notice the important difference. 2 is after 1 in alphabet so 2 goes after 10 in text mode.

XSLT stylesheet 1

XML Source
<source>

<car id="11"/>
<car id="6"/>
<car id="105"/>
<car id="28"/>
<car id="9"/>

</source>

Output
<TABLE>
  <TR>
     <TH>Car-105</TH>
  </TR>
  <TR>
     <TH>Car-11</TH>
  </TR>
  <TR>
     <TH>Car-28</TH>
  </TR>
  <TR>
     <TH>Car-6</TH>
  </TR>
  <TR>
     <TH>Car-9</TH>
  </TR>
</TABLE>

HTML view
Car-105
Car-11
Car-28
Car-6
Car-9
XSLT stylesheet
<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="text" select="@id"/>
               <TR>
                    <TH>
                         <xsl:text>Car-</xsl:text>
                         <xsl:value-of select="@id"/>
                    </TH>
               </TR>
          </xsl:for-each>
     </TABLE>
</xsl:template>


</xsl:stylesheet>


XSLT stylesheet 2

XML Source
<source>

<car id="11"/>
<car id="6"/>
<car id="105"/>
<car id="28"/>
<car id="9"/>

</source>

Output
<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>

HTML view
Car-6
Car-9
Car-11
Car-28
Car-105
XSLT stylesheet
<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>