If you look at Example 8-1, the document europe.xml in examples/ch08, you'll notice that the European states are not listed in alphabetical order. Example 8-1. Unalphabetized European countries<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet href="pretty.xsl" type="text/xsl"?> <europe> <state>Belgium</state> <state>Germany</state> <state>Finland</state> <state>Greece</state> <state>Ireland</state> <state>Luxembourg</state> <state>Portugal</state> <state>Spain</state> <state>Andorra</state> <state>Belarus</state> <state>Monaco</state> <state>Sweden</state> <state>United Kingdom</state> <state>Austria</state> <state>Malta</state> <state>Vatican City</state> <state>Bulgaria</state> <state>Bosnia-Herzegovina</state> <state>Cyprus</state> <state>France</state> <state>Estonia</state> <state>Italy</state> <state>Hungary</state> <state>Latvia</state> <state>Ukraine</state> <state>Lithuania</state> <state>Moldova</state> <state>Denmark</state> <state>Poland</state> <state>Romania</state> <state>Slovenia</state> <state>The Netherlands</state> <state>Turkey</state> <state>Albania</state> <state>Serbia and Montenegro</state> <state>Croatia</state> <state>Slovakia</state> <state>Iceland</state> <state>Czech Republic</state> <state>Liechtenstein</state> <state>Macedonia, Former Yugoslav Republic of</state> <state>Norway</state> <state>Russia</state> <state>San Marino</state> <state>Switzerland</state> </europe> You can sort the names of the European states in ascending order (that is, in English as a, b, c, and so on) by using the sort element with no attributes, as a child of apply-templates. The sort element is used in the stylesheet shown in Example 8-2, sort.xsl. Example 8-2. Creating a sorted list of countries as text<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:template match="europe"> <xsl:text>Alphabetical List of European States</xsl:text> <xsl:text> Total Number of States: </xsl:text> <xsl:value-of select="count(state)"/> <xsl:text> </xsl:text> <xsl:apply-templates select="state"> <xsl:sort/> </xsl:apply-templates> </xsl:template> <xsl:template match="state"> <xsl:text> - </xsl:text> <xsl:apply-templates/> <xsl:text> </xsl:text> </xsl:template> </xsl:stylesheet> This stylesheet produces plain text output (it uses the text method of output), as shown in Example 8-3. A few text instruction elements are sprinkled here and there to label the output or add a line feed (using the character reference). The count( ) function is also used to count the number of state elements in the source tree, and the return value of this function is displayed using value-of. The sort element appears as a child of apply-templates. It may only appear as a child of either apply-templates or for-each.
In this stylesheet, when state elements are selected with apply-templates, the processor will also apply a sort. To see what happens, apply the sort.xsl stylesheet to europe.xml using this command: xalan europe.xsl sort.xsl The plain text, alphabetized result tree shown in Example 8-3 will be output to your screen. Example 8-3. A sorted text list produced by sort.xslAlphabetical List of European States Total Number of States: 45 - Albania - Andorra - Austria - Belarus - Belgium - Bosnia-Herzegovina - Bulgaria - Croatia - Cyprus - Czech Republic - Denmark - Estonia - Finland - France - Germany - Greece - Hungary - Iceland - Ireland - Italy - Latvia - Liechtenstein - Lithuania - Luxembourg - Macedonia, Former Yugoslav Republic of - Malta - Moldova - Monaco - Norway - Poland - Portugal - Romania - Russia - San Marino - Serbia and Montenegro - Slovakia - Slovenia - Spain - Sweden - Switzerland - The Netherlands - Turkey - Ukraine - United Kingdom - Vatican City If you would like pretty output worthy of a browser, you could also create an HTML wrapper with the stylesheet shown in Example 8-4, pretty.xsl. Example 8-4. A stylesheet for producing a sorted list of states presented in HTML<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="europe"> <html> <head><title>European States</title></head> <style type="text/css">body {font-family: sans-serif}</style> <body> <h3>Alphabetical List of European States</h3> <p><b>Total Number of States:</b><xsl:text> </xsl:text> <xsl:value-of select="count(state)"/></p> <ul> <xsl:apply-templates select="state"> <xsl:sort/> </xsl:apply-templates> </ul> </body> </html> </xsl:template> <xsl:template match="state"> <li><xsl:apply-templates/></li> </xsl:template> </xsl:stylesheet> This stylesheet produces indented HTML output by default (without explicitly stating so in an output element), because the first element in the result tree is html and there is no output element to define the method. Process the stylesheet with Xalan: xalan -i 1 europe.xml pretty.xsl Once again, the -i option followed by 1 tells the processor to indent the output by one space. You will see output like Example 8-5. Example 8-5. The HTML results of using pretty.xsl<html> <head> <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>European States</title> </head> <style type="text/css">body {font-family: sans-serif}</style> <body> <h3>Alphabetical List of European States</h3> <p> <b>Total Number of States:</b> 45</p> <ul> <li>Albania</li> <li>Andorra</li> <li>Austria</li> <li>Belarus</li> <li>Belgium</li> <li>Bosnia-Herzegovina</li> <li>Bulgaria</li> <li>Croatia</li> <li>Cyprus</li> <li>Czech Republic</li> <li>Denmark</li> <li>Estonia</li> <li>Finland</li> <li>France</li> <li>Germany</li> <li>Greece</li> <li>Hungary</li> <li>Iceland</li> <li>Ireland</li> <li>Italy</li> <li>Latvia</li> <li>Liechtenstein</li> <li>Lithuania</li> <li>Luxembourg</li> <li>Macedonia, Former Yugoslav Republic of</li> <li>Malta</li> <li>Moldova</li> <li>Monaco</li> <li>Norway</li> <li>Poland</li> <li>Portugal</li> <li>Romania</li> <li>Russia</li> <li>San Marino</li> <li>Serbia and Montenegro</li> <li>Slovakia</li> <li>Slovenia</li> <li>Spain</li> <li>Sweden</li> <li>Switzerland</li> <li>The Netherlands</li> <li>Turkey</li> <li>Ukraine</li> <li>United Kingdom</li> <li>Vatican City</li> </ul> </body> </html> If you simply open europe.xml with a browser such as Netscape 7.1, the browser will apply the stylesheet pretty.xsl referenced in the XML stylesheet PI, and the result will appear in the browser as shown in Figure 8-1. Figure 8-1. European states sorted alphabetically in Netscape 7.1 |