XSLT and XPath function reference in alphabetical order

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 5, translated from German)

A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z

current-group

Category:

Context information within a sequence

Origin:

XSLT 2.0

Return value:

A sequence consisting of one or more items; the currently processed group of a xsl:for-each-group instruction.

Call/Arguments:

current-group()

No arguments.

Purpose of use:

When performing a xsl:for-each-group instruction, for each determined group in a row the template block contained in xsl:for-each-group is instantiated, whereby each currently processed group is referred to as Current Group.

During the processing of the groups, the first member of each group (initial item) is considered as context item. The total number of the groups to be processed makes the context size and the position of the group in the processing order makes its context position. The functions fn:position() and fn:last() return, used here, appropriate values.

In the template block of xsl:for-each-group, the current-group() function is used in order to make resource to the members of this group as a sequence, for example, in order to output the string values of all group members with xsl:value-of. The function returns – since groups (must) have at least one member – a sequence of at least one item.

If the function is called up outside the context of the xsl:for-each-group processing, it returns the empty sequence.

No current-group() in XPath patterns:
The current-group() function is not permitted as part of XPath patterns. If used in XPath patterns, a static error will occur which makes the processing of the stylesheet impossible.

Example:

(Translated from the German data2type XSLT 2.0 reference, xsl:for-each-group element, example 2.)

Source document:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <city country="de">
      <name>Berlin</name>
      <population>3420786</population>
      <surface>891,85</surface>
      <community>11000000</community>
      <evaluation>5</evaluation>
   </city>
   <city country="de">
      <name>Hamburg</name>
      <population>1773218</population>
      <surface>755,264</surface>
      <community>02000000</community>
      <evaluation>4</evaluation>
   </city>
   <city country="fr">
      <name>Paris</name>
      <population>2166200</population>
      <surface>105,40</surface>
      <evaluation>3</evaluation>
   </city>
   <city country="de">
      <name>Ludwigshafen</name>
      <population>163777</population>
      <surface>77,68</surface>
      <evaluation>-3</evaluation>
   </city>
</root>

XSLT stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
   <xsl:template match="root">
      <directory>
         <xsl:for-each-group select="city" group-by="@country">
            <group>
               <key>
                  <xsl:value-of select="current-grouping-key()"></xsl:value-of>
               </key>
                  <xsl:for-each select="current-group()">
                     <city>
                        <xsl:value-of select="name"></xsl:value-of>
                     </city>
                  </xsl:for-each>
            </group>
         </xsl:for-each-group>
      </directory>
   </xsl:template>
</xsl:stylesheet>

Result:

<?xml version="1.0" encoding="UTF-8"?>
<directory>
   <group>
      <key>de</key>
      <city>Berlin</city>
      <city>Hamburg</city>
      <city>Ludwigshafen</city>
   </group>
   <group>
      <key>fr</key>
      <city>Paris</city>
   </group>
</directory>

In this example the cities are grouped by means of their country codes ("de" or "fr"). The current-group function returns all current group members (Berlin, Hamburg and Ludwigshafen or Paris) for each loop run.

Function definition:

XSLT 1.0:

The function is not available.

XSLT 2.0:

current-group() as item()*

   

<< back next >>

 

 

 


Copyright © Galileo Press, Bonn 2008
Printing of the online version is permitted exclusively for private use. Otherwise this chapter from the book "XSLT 2.0 & XPath 2.0" is subject to the same provisions as those applicable for the hardcover edition: The work including all its components is protected by copyright. All rights reserved, including reproduction, translation, microfilming as well as storage and processing in electronic systems.


Galileo Press, Rheinwerkallee 4, 53227 Bonn, Germany