<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<h1>Sorting Examples</h1>
<xsl:apply-templates select="presidents"/>
</body>
</html>
</xsl:template>
<!--********************************************************************
** presidents template
*****************************************************************-->
<xsl:template match="presidents">
<!--*****************************************************************
** Sorting using xsl:for-each
**************************************************************-->
<h2>All presidents sorted by first name using xsl:for-each</h2>
<xsl:for-each select="president">
<xsl:sort select="name/first"/>
<xsl:apply-templates select="name"/>
</xsl:for-each>
<!--*****************************************************************
** Sorting using xsl:apply-templates
**************************************************************-->
<h2>All presidents sorted by first name using xsl:apply-templates</h2>
<xsl:apply-templates select="president/name">
<xsl:sort select="first"/>
</xsl:apply-templates>
<h2>All presidents sorted by date using xsl:apply-templates</h2>
<xsl:apply-templates select="president/name">
<xsl:sort select="../term/@from" data-type="number" order="descending"/>
</xsl:apply-templates>
<!--*****************************************************************
** Multi-field sorting
**************************************************************-->
<h2>Multi-field sorting example</h2>
<xsl:apply-templates select="president/name">
<xsl:sort select="last"/>
<xsl:sort select="first" order="descending"/>
</xsl:apply-templates>
<!--*****************************************************************
** Nested xsl:for-each loops
**************************************************************-->
<h2>All presidents and vice presidents using xsl:for-each</h2>
<ul>
<xsl:for-each select="president">
<xsl:sort select="name/first" order="descending"/>
<li>
<xsl:apply-templates select="name"/>
</li>
<ul>
<xsl:for-each select="vicePresident">
<xsl:sort select="name/first"/>
<li>
<xsl:apply-templates select="name"/>
</li>
</xsl:for-each>
</ul>
</xsl:for-each>
</ul>
<!--*****************************************************************
** Same as previous, only using xsl:apply-templates
**************************************************************-->
<h2>All presidents and vice presidents using xsl:apply-templates</h2>
<ul>
<xsl:apply-templates select="president">
<xsl:sort select="name/first" order="descending"/>
</xsl:apply-templates>
</ul>
</xsl:template>
<!--*****************************************************************
** 'president' template, outputs the president's name and vice
** president's name.
**************************************************************-->
<xsl:template match="president">
<li>
<xsl:apply-templates select="name"/>
</li>
<ul>
<xsl:for-each select="vicePresident">
<xsl:sort select="name/first"/>
<li>
<xsl:apply-templates select="name"/>
</li>
</xsl:for-each>
</ul>
</xsl:template>
<!--*****************************************************************
** name template, outputs first, middle, and last name
**************************************************************-->
<xsl:template match="name">
<xsl:text disable-output-escaping="yes">&nbsp;</xsl:text>
<xsl:value-of select="first"/>
<xsl:text disable-output-escaping="yes">&nbsp;</xsl:text>
<xsl:value-of select="middle"/>
<xsl:text disable-output-escaping="yes">&nbsp;</xsl:text>
<xsl:value-of select="last"/>
<br/>
</xsl:template>
</xsl:stylesheet>