8.2 Reversing the Sort

The sort element uses ascending order by default, as if the order attribute were present with a value of ascending, like so:

<xsl:sort order="ascending"/>

This order follows the normal a, b, c order of the English alphabet. You can also sort in descending order, that is, using English, in the order z, y, x. To do this, you have to add an order attribute to sort, as does the stylesheet descending.xsl, shown in Example 8-6.

Example 8-6. A stylesheet for sorting country names backward
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/>      <xsl:template match="europe">    <xsl:apply-templates select="state">     <xsl:sort order="descending"/>    </xsl:apply-templates>    <xsl:text>Number of European States: </xsl:text>    <xsl:value-of select="count(state)"/>    <xsl:text>&#10;</xsl:text>  </xsl:template>      <xsl:template match="state">    <xsl:text> - </xsl:text>    <xsl:apply-templates/>    <xsl:text>&#10;</xsl:text>  </xsl:template>     </xsl:stylesheet>

Now apply it with:

xalan europe.xml descending.xsl

to get the output shown in Example 8-7.

Example 8-7. A reverse-sorted list of countries produced using descending.xsl
 - Vatican City  - United Kingdom  - Ukraine  - Turkey  - The Netherlands  - Switzerland  - Sweden  - Spain  - Slovenia  - Slovakia  - Serbia and Montenegro  - San Marino  - Russia  - Romania  - Portugal  - Poland  - Norway  - Monaco  - Moldova  - Malta  - Macedonia, Former Yugoslav Republic of  - Luxembourg  - Lithuania  - Liechtenstein  - Latvia  - Italy  - Ireland  - Iceland  - Hungary  - Greece  - Germany  - France  - Finland  - Estonia  - Denmark  - Czech Republic  - Cyprus  - Croatia  - Bulgaria  - Bosnia-Herzegovina  - Belgium  - Belarus  - Austria  - Andorra  - Albania Number of European States: 45

The output is in reverse, or descending, order in English.



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