XML technologies / XSLT / XSLT and XPath functions / Alphabetical XSLT and XPath reference / current-grouping-key

(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-grouping-key

Category:

Context information within a sequence

Origin:

XSLT 2.0

Return value:

An atomic value; the current value of the grouping criterion during the processing of a group formed by xsl:for-each-group.

Call/Arguments:

current-grouping-key()

No arguments.

Purpose of use:

With the help of the current-grouping-key() function, the current value of the grouping criterion on which the group is based can be used during the processing of a group formed by the XSLT instruction xsl:for-each-group. This value is determined by the attributes group-by or group-adjacent of the instruction. The returned value is always an atomic value (a singleton sequence) and never a sequence consisting of several values.

Outside the evaluation context of a xsl:for-each-group instruction, the function always returns the empty sequence.

No current-grouping-key() in XPath patterns:
The current-grouping-key() function is not permitted as a 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 their country codes ("de" or "fr"). The current-grouping-key() function returns the current key value by means of which the grouping was made ("de" or "fr").

Function definition:

XSLT 1.0:

The function is not available.

XSLT 2.0:

current-grouping-key() as xs:anyAtomicType

   

 

 

 

back to top