Name

[2.0] current-grouping-key()

Returns the value used to select the current group.

Syntax

xs:anyAtomicType? current-grouping-key()

Inputs

None.

Output

The value used to select the items in the current group. The current-grouping-key() function is only useful inside an <xsl:for-each-group> element with a group-by or group-adjacent attribute. Calling current-grouping-key() anywhere else returns the empty sequence.

Defined in

XSLT 2.0 section 14, “Grouping.”

Example

We’ll use a simplified version of our address book document to illustrate the current-grouping-key() function:

<?xml version="1.0"?>
<!-- simple-addresses.xml -->
<addressbook>
  <address>
    <name>Mr. Chester Hasbrouck Frisby</name>
    <city>Sheboygan</city>
    <state>WI</state>
  </address>
  <address>
    <name>Mary Backstayge</name>
    <city>Skunk Haven</city>
    <state>MA</state>
  </address>
  <address>
    <name>Ms. Natalie Attired</name>
    <city>Winter Harbor</city>
    <state>ME</state>
  </address>
  <address>
    <name>Harry Backstayge</name>
    <city>Skunk Haven</city>
    <state>MA</state>
  </address>
  <address>
    <name>Mary McGoon</name>
    <city>Boylston</city>
    <state>VA</state>
  </address>
  <address>
    <name>Ms. Amanda Reckonwith</name>
    <city>Lynn</city>
    <state>MA</state>
  </address>
</addressbook>

We’ll group these names by <state> with this stylesheet:

<?xml version="1.0"?>
<!-- current-grouping-key.xsl --> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:text>&#xA;Here's a test of the </xsl:text> ...

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.