8.3 By the Numbers

So far, you have sorted nodes alphabetically. You can also sort nodes numerically by specifying the sort element's data-type attribute with a value of number. By default, sort works as if data-type were present and had a value of text, which indicates that you want to sort text alphabetically.

To see how it works, have a look at Example 8-8, the document member.xml.

Example 8-8. An XML list of EU member states
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet href="year.xsl" type="text/xsl"?>     <!-- European Union member states -->     <member>  <state joined="1995">Austria</state>  <state joined="1950">Belgium</state>  <state joined="1973">Denmark</state>  <state joined="1995">Finland</state>  <state joined="1950">France</state>  <state joined="1950">Germany</state>  <state joined="1981">Greece</state>  <state joined="1973">Ireland</state>  <state joined="1950">Italy</state>  <state joined="1950">Luxembourg</state>  <state joined="1950">The Netherlands</state>  <state joined="1986">Portugal</state>  <state joined="1986">Spain</state>  <state joined="1995">Sweden</state>  <state joined="1973">United Kingdom</state> </member>

Example 8-8 holds state elements, each containing the name of a European Union (EU) member state, in alphabetical order. Each of the 15 state elements also has a joined attribute with a number value, indicating the year the country joined the EU.

If you want to sort by year rather than name, you could use the stylesheet shown in Example 8-9, numeric.xsl.

Example 8-9. A stylesheet for sorting countries by year of EU membership
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/>      <xsl:template match="member">    <xsl:text>Number of EU Member States: </xsl:text>    <xsl:value-of select="count(state)"/>    <xsl:text>&#10;</xsl:text>    <xsl:apply-templates select="state/@joined">     <xsl:sort data-type="number"/>    </xsl:apply-templates>    <xsl:text>&#10;</xsl:text>  </xsl:template>      <xsl:template match="state/@joined">    <xsl:text> - </xsl:text>    <xsl:apply-templates select=".."/>    <xsl:text> (</xsl:text>    <xsl:value-of select="."/>    <xsl:text>)&#10;</xsl:text>  </xsl:template>     </xsl:stylesheet>

The sort element in Example 8-9 has a data-type attribute with a value of number and sorts by the year in the attribute joined. The template that matches state/@joined may seem a little obscure, but it gets exactly what it's after, namely, the name of the European state (obtained with ..), followed by a year (obtained with .), placing the year in parentheses.

To see what happens, apply the stylesheet with:

xalan member.xml numeric.xsl

and you will get the output shown in Example 8-10.

Example 8-10. The sorted list of countries produced by running numeric.xsl
Number of EU Member States: 15  - Belgium (1950)  - France (1950)  - Germany (1950)  - Italy (1950)  - Luxembourg (1950)  - The Netherlands (1950)  - Denmark (1973)  - Ireland (1973)  - United Kingdom (1973)  - Greece (1981)  - Portugal (1986)  - Spain (1986)  - Austria (1995)  - Finland (1995)  - Sweden (1995)

You can see from the output that the state nodes were sorted according to the year in the joined attribute, not alphabetically according to the name of the European state. Because the states are already in alphabetical order in the source tree, they also come out in alphabetical order in the result tree, after being sorted by year.

If you want to list the most recent year first, you can do so by adding the order attribute, as seen in Example 8-11, the stylesheet recent.xsl.

Example 8-11. A stylesheet for reverse-sorting countries by year of EU membership
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/>      <xsl:template match="member">    <xsl:text>Number of EU Member States: </xsl:text>    <xsl:value-of select="count(state)"/>    <xsl:text>&#10;</xsl:text>    <xsl:apply-templates select="state/@joined">     <xsl:sort data-type="number" order="descending"/>    </xsl:apply-templates>    <xsl:text>&#10;</xsl:text>  </xsl:template>      <xsl:template match="state/@joined">    <xsl:text> - </xsl:text>    <xsl:apply-templates select=".."/>    <xsl:text> (</xsl:text>    <xsl:value-of select="."/>    <xsl:text>)&#10;</xsl:text>  </xsl:template>     </xsl:stylesheet>

In recent.xsl, the order attribute is added to sort and has a value of descending. Now apply it with this command:

xalan member.xml recent.xsl

and your results will look like Example 8-12.

Example 8-12. The reverse-sorted list of countries produced by running recent.xsl
Number of EU Member States: 15  - Austria (1995)  - Finland (1995)  - Sweden (1995)  - Portugal (1986)  - Spain (1986)  - Greece (1981)  - Denmark (1973)  - Ireland (1973)  - United Kingdom (1973)  - Belgium (1950)  - France (1950)  - Germany (1950)  - Italy (1950)  - Luxembourg (1950)  - The Netherlands (1950)

If you open member.xml with a browser, the stylesheet year.xsl (shown in Example 8-13) will be applied.

Example 8-13. A stylesheet for sorting countries by year into an XHTML representation
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <xsl:output doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"/> <xsl:output doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>      <xsl:template match="member">    <html xmlns="http://www.w3.org/1999/xhtml">    <head><title>EU Member States</title>    <style type="text/css">    h3 {font-size: 16pt}    body {font-size: 13pt}</style>    </head>    <body>    <h3>EU Member States</h3>    <p>There are <xsl:text> </xsl:text>     <xsl:value-of select="count(state)"/>    member states, listed starting from the most recent year:</p>    <ul>    <xsl:apply-templates select="state">     <xsl:sort select="@joined" data-type="number" order="descending"/>    </xsl:apply-templates>    </ul>    </body>    </html>  </xsl:template>      <xsl:template match="state">    <xsl:element name="li" namespace="http://www.w3.org/1999/xhtml">    <xsl:apply-templates/>    <xsl:text> (</xsl:text>    <xsl:value-of select="@joined"/>    <xsl:text>)</xsl:text>    </xsl:element>  </xsl:template>     </xsl:stylesheet>

The stylesheet presents the same results as recent.xsl but in strict XHTML 1.0, as shown in Mozilla Firebird in Figure 8-2.

Figure 8-2. The document member.xml transformed in Mozilla Firebird
figs/lxsl_0802.gif


Learning XSLT
Learning XSLT
ISBN: 0596003277
EAN: 2147483647
Year: 2003
Pages: 164

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net