16.5 Grouping in XSLT 2.0

Grouping in XSLT is the process by which you can group nodes based on a given criterion. In XSLT 1.0, the process is a little complicated and requires somewhat elaborate expressions, often employing the preceding-sibling axes to check whether a node belongs to a group. You could also group nodes with a key using the Muenchian method, which was demonstrated in Chapter 11. You can also read about how to do XSLT 1.0 grouping in Chapter 6 of Doug Tidwell's XSLT (O'Reilly) or in Chapter 9 of Michael Kay's XSLT Programmer's Reference, Second Edition (Wrox). I prefer grouping in XSLT 2.0 because it is much simpler and easier to explain, the ease of which probably grew out of my experience with grouping in Version 1.0

Grouping in XSLT 1.0 usually brings the for-each instruction element into service. XSLT 2.0 has a new instruction element called for-each-group that makes grouping a relative snap. I'll show you how in the following example.

Glance at group2.xml, in Example 16-6, which lumps the XPath 2.0's context-related functions into two piles by labeling them with a type attribute.

Example 16-6. A list of XPath 2.0 context-related functions
<?xml version="1.0"?>     <list>  <description>XPath 2.0 Context Functions</description>  <date>2003-10-03</date>  <function type="new">context-item(  )</function>  <function type="new">current-date(  )</function>  <function type="new">current-dateTime(  )</function>  <function type="new">current-time(  )</function>  <function type="new">default-collation(  )</function>  <function type="new">implicit-timezone(  )</function>  <function type="legacy">last(  )</function>  <function type="legacy">position(  )</function> </list>

The eight functions in this list are either legacy or new functions. The group2.xsl stylesheet, in Example 16-7, groups the functions in group2.xml according to the content of the type attribute.

Example 16-7. A stylesheet grouping elements using XSLT 2.0
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/>     <xsl:template match="list"> <xsl:copy>  <xsl:for-each-group select="function" group-by="@type">   <functions type="{@type}">    <xsl:value-of select="current-group(  )" separator=", "/>   </functions>  </xsl:for-each-group> </xsl:copy> </xsl:template>     </xsl:stylesheet>

The for-each-group function selects the node-set to group with the select attribute all function children of list, that is. The group-by attribute determines the key for grouping, which, in this case, is the content of the type attribute in the source. The functions literal result element uses an attribute value template to reflect the value of the type attribute.

The value-of element's select attribute uses the current-group( ) function also a new kid on the block in XSLT 2.0 to keep track of which group is which. The separator attribute is also a new addition to XSLT 2.0. It tells the XSLT 2.0 processor to write a comma followed by a space after each found node is sent to the result tree.

In XSLT 1.0, value-of outputs only the first node of a returned node-set in string form; in XSLT 2.0, all nodes can be returned, so you have to plan accordingly.


You might guess correctly that for-each-group has several other attributes, which it does, namely, group-adjacent, group-starting-with, group-ending-with, and collation. I'm not going to cover them here, but you can read more about for-each-group and its attributes in Section 14 of the XSLT 2.0 specification.

Use this command to transform group.xml:

java -jar saxon7.jar group2.xml group2.xsl

The result is two lists of functions, grouped and comma-separated, in functions elements:

<?xml version="1.0" encoding="UTF-8"?> <list>    <functions type="new">context-item(  ), current-date(  ), current-dateTime(  ),  current-time(  ), default-collation(  ), implicit-timezone(  )</functions>    <functions type="legacy">last(  ), position(  )</functions> </list>

This example should give you a feel of how to group nodes in XSLT 2.0. In the example that follows, you will learn how to use the new top-level function element.



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