Previous | Next
Looping and SortingAs shown throughout this chapter, you can use Looping with <xsl:for-each>Sometimes it is convenient to explicitly drive the selection process with an <xsl:for-each select="president"> ...content for each president element </xsl:for-each> The SortingSorting can be applied in either a data-driven or template-driven approach. In either case, <xsl:sort select="first"/> <xsl:sort select="last" order="descending"/> <xsl:sort select="term/@from" order="descending" data-type="number"/> <xsl:sort select="name/first" data-type="text" case-order="upper-first"/> In the last line, the Looping and Sorting ExamplesThe easiest way to learn about looping and sorting is to play around with a lot of small examples. The code in Example 2-9 applies numerous different looping and sorting strategies to our list of presidents. Comments in the code indicate what is happening at each step. Example 2-9. Looping and sorting<?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> Notice that when applying a sort to |