8.4 Multiple Sorts

You can sort nodes more than once, if needed, and you can also sort child nodes a different way than you sort their parent nodes. The select attribute of sort can help you do the job, as will be demonstrated in this section. Example 8-14, the document shopping.xml, represents a short, disorderly shopping list.

Example 8-14. An XML shopping list
<list>  <freezer>   <item>peas</item>   <item>green beans</item>   <item>pot pie</item>   <item>ice cream</item>  </freezer>  <bakery>   <item>rolls</item>   <item>jelly doughnuts</item>   <item>bread</item>  </bakery>  <produce>   <item>bananas</item>   <item>kumquats</item>   <item>apples</item>  </produce> </list>

To help get things in better shape, Example 8-15, the stylesheet shopping.xsl, uses sort twice to sort different node-sets.

Example 8-15. A stylesheet for sorting the grocery list on two levels
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/>      <xsl:template match="list">    <xsl:apply-templates select="*">     <xsl:sort select="name(  )"/>    </xsl:apply-templates>  </xsl:template>      <xsl:template match="*">    <xsl:text>Section: </xsl:text>    <xsl:value-of select="name(  )"/>    <xsl:text>&#10;</xsl:text>    <xsl:apply-templates select="item">     <xsl:sort/>    </xsl:apply-templates>  </xsl:template>      <xsl:template match="item">    <xsl:text> * </xsl:text>     <xsl:apply-templates/>    <xsl:text>&#10;</xsl:text>  </xsl:template>     </xsl:stylesheet>

Example 8-15 outputs plain text. The first template in this stylesheet matches list and then sorts on the names (using name( )) of the element children (using *) of list. This is the first sort. The second template matches only on the element children of list, again using *. After inserting some text (such as Section:) and the name of the element (again with name( )), the template sorts the text node content of item children. This is the second sort.

Finally, the last template matches item elements, prefixing text nodes with an asterisk (a bullet) in the result tree, and throwing in a line break after the text.

To see the results, type the command:

xalan shopping.xml shopping.xsl

and you will see Example 8-16.

Example 8-16. The sorted list of groceries produced by running shopping.xsl
Section: bakery  * bread  * jelly doughnuts  * rolls Section: freezer  * green beans  * ice cream  * peas  * pot pie Section: produce  * apples  * bananas  * kumquats

Originally, in the source document, the child elements of list were ordered freezer, bakery, and produce. Now they are alphabetically correct, that is, bakery, freezer, and produce. The children of each of these elements all item elements are correctly ordered as well.

Using copy and copy-of, the stylesheet in Example 8-17 (list.xsl) generates an XML result.

Example 8-17. A stylesheet for produce XML alphabetized by its content
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/>      <xsl:template match="list">   <xsl:copy>    <xsl:apply-templates select="*">     <xsl:sort select="name(  )"/>    </xsl:apply-templates>   </xsl:copy>  </xsl:template>      <xsl:template match="*">   <xsl:copy>    <xsl:apply-templates select="item">     <xsl:sort/>    </xsl:apply-templates>   </xsl:copy>  </xsl:template>      <xsl:template match="item">   <xsl:copy-of select="."/>  </xsl:template>     </xsl:stylesheet>

As a result of the following command:

xalan -i 1 shopping.xml list.xsl

you'll get nicely alphabetized nodes, as shown in Example 8-18.

Example 8-18. The sorted list of groceries produced by running list.xsl
<?xml version="1.0" encoding="UTF-8"?> <list>  <bakery>   <item>bread</item>   <item>jelly doughnuts</item>   <item>rolls</item>  </bakery>  <freezer>   <item>green beans</item>   <item>ice cream</item>   <item>peas</item>   <item>pot pie</item>  </freezer>  <produce>   <item>apples</item>   <item>bananas</item>   <item>kumquats</item>  </produce> </list>


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