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

Nodes selected with xsl:for-each ( XSLT stylesheet 1 and XSLT stylesheet 2 ) or xsl:apply-templates ( XSLT stylesheet 3 ) can be sorted. Order of sorting determines order attribute. XSLT stylesheet 1 sorts in ascending and XSLT stylesheet 2 in descending mode.

XSLT stylesheet 1

XML Source
<source>

<name>John</name>
<name>Josua</name>
<name>Charles</name>
<name>Alice</name>
<name>Martha</name>
<name>George</name>

</source>

Output
<TABLE>
  <TR>
     <TH>Alice</TH>
  </TR>
  <TR>
     <TH>Charles</TH>
  </TR>
  <TR>
     <TH>George</TH>
  </TR>
  <TR>
     <TH>John</TH>
  </TR>
  <TR>
     <TH>Josua</TH>
  </TR>
  <TR>
     <TH>Martha</TH>
  </TR>
</TABLE>

HTML view
Alice
Charles
George
John
Josua
Martha
XSLT stylesheet
<xsl:stylesheet version = '1.0'
     xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="/">
     <TABLE>
          <xsl:for-each select="//name">
               <xsl:sort order="ascending" select="."/>
               <TR>
                    <TH>
                         <xsl:value-of select="."/>
                    </TH>
               </TR>
          </xsl:for-each>
     </TABLE>
</xsl:template>


</xsl:stylesheet>


XSLT stylesheet 2

XML Source
<source>

<name>John</name>
<name>Josua</name>
<name>Charles</name>
<name>Alice</name>
<name>Martha</name>
<name>George</name>

</source>

Output
<TABLE>
  <TR>
     <TH>Martha</TH>
  </TR>
  <TR>
     <TH>Josua</TH>
  </TR>
  <TR>
     <TH>John</TH>
  </TR>
  <TR>
     <TH>George</TH>
  </TR>
  <TR>
     <TH>Charles</TH>
  </TR>
  <TR>
     <TH>Alice</TH>
  </TR>
</TABLE>

HTML view
Martha
Josua
John
George
Charles
Alice
XSLT stylesheet
<xsl:stylesheet version = '1.0'
     xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="/">
     <TABLE>
          <xsl:for-each select="//name">
               <xsl:sort order="descending" select="."/>
               <TR>
                    <TH>
                         <xsl:value-of select="."/>
                    </TH>
               </TR>
          </xsl:for-each>
     </TABLE>
</xsl:template>


</xsl:stylesheet>


XSLT stylesheet 3

XML Source
<source>

<name>John</name>
<name>Josua</name>
<name>Charles</name>
<name>Alice</name>
<name>Martha</name>
<name>George</name>

</source>

Output
<TABLE>
  <TR>
     <TH>Martha</TH>
  </TR>
  <TR>
     <TH>Josua</TH>
  </TR>
  <TR>
     <TH>John</TH>
  </TR>
  <TR>
     <TH>George</TH>
  </TR>
  <TR>
     <TH>Charles</TH>
  </TR>
  <TR>
     <TH>Alice</TH>
  </TR>
</TABLE>

HTML view
Martha
Josua
John
George
Charles
Alice
XSLT stylesheet
<xsl:stylesheet version = '1.0'
     xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="/">
     <TABLE>
          <xsl:apply-templates select="//name">
               <xsl:sort order="descending" select="."/>
          </xsl:apply-templates>
     </TABLE>
</xsl:template>

<xsl:template match="name">
     <TR>
          <TH>
               <xsl:value-of select="."/>
          </TH>
     </TR>
</xsl:template>


</xsl:stylesheet>