Tom
Tom

Reputation: 153

XSLT grouping-key of the parent group

I have 3 nested xsl:for-each-group statements and i'm using a variable to store the parent's current-grouping-key(), because i need this value in the last for-each-group as a filter. but i'm wondering if it's not possible to access the parent's current-grouping-key somehow else without declaring a specific variable?

Example:

<xsl:for-each-group select="//results/result[measuring_plane != '']" group-by="inspection_feature">

  <xsl:variable name="v_current_inspection_feature" select="inspection_feature" as="xs:string"/>

  <xsl:for-each-group select="//results/result[inspection_feature = current-grouping-key() and measuring_plane != '']" group-by="description">

    <xsl:for-each-group select="//results/result[inspection_feature = $v_current_inspection_feature and description = current-grouping-key() and measuring_plane != '']" group-by="step">

SOLUTION:

<xsl:for-each-group select="//results/result[measuring_plane != '']" group-by="inspection_feature">
  <xsl:for-each-group select="current-group()" group-by="description">
    <xsl:for-each-group select="current-group()" group-by="step">

Upvotes: 0

Views: 414

Answers (3)

Michael Kay
Michael Kay

Reputation: 163595

You ask:

i'm wondering if it's not possible to access the parent's current-grouping-key somehow else without declaring a specific variable?

Answer: no, it isn't. The XSLT design where various instructions set an implicit "current X" (e.g. current item, current group, current grouping key, current regex group) is a deliberate design choice that makes simple things simpler and complex things more difficult. In this case, getting the current grouping key of an outer for-each-group fits into the "complex things" category, and requires binding a variable.

Upvotes: 0

michael.hor257k
michael.hor257k

Reputation: 117140

To add to what Martin Honnen wrote in his answer:

Within the xsl:for-each-group instruction, the first item of the current group is the context item. And of course the context item has the same value for the grouping key expression as the rest of the group.

This means that in your example, after grouping first by inspection_feature and then by description, you can refer to inspection_feature to get the value of the grouping key used in the first grouping and to description to get the value of the current grouping key.

But again, if the only purpose of this is to select the current group for subgrouping, you can simply use the current-group()function.

Upvotes: 1

Martin Honnen
Martin Honnen

Reputation: 167716

Use select="current-group()" for the nested for-each-group instructions.

Upvotes: 1

Related Questions