Group related functionality in XSLT

I have a use question group-adjacent

.

I've seen two patterns:

Template 1:

<xsl:for-each-group select="*" group-adjacent="boolean(self::p[@class = 'code'])">

      

Template 2:

<xsl:for-each-group select="*" group-adjacent="@class">

      

Depending on what is being used, I noticed that it current-grouping-key()

returns false.

What is the purpose of using a boolean function in a group join?

+3


source to share


2 answers


With form, <xsl:for-each-group select="*" group-adjacent="boolean(self::p[@class = 'code'])">

the grouping key is a boolean value that is true for adjacent elements p

that have an attribute class

with a value code

, and in the second form <xsl:for-each-group select="*" group-adjacent="@class">

, the grouping value is a string and groups all adjacent elements with the same attribute values class

.

So it depends on your needs if you have for example

<items>
  <item class="c1">...</item>
  <item class="c1">...</item>
  <item class="c2">...</item>
</items>

      

you can use the second approach for grouping by value class

.



On the other hand, if you want to identify adjacent elements p

with a specific attribute class

, for example in

<body>
  <h1>...</h1>
  <p class="code">...</p>
  <p class="code">...</p>
  <h2>...</h2>
  <p class="code">...</p>
</body>

      

then the first approach allows it.

+4


source


Depending on what is being used, I noticed that current-grouping-key () returns false.

current-grouping-key () returns true or false, depending on the current group. In your first example, current-grouping-key () will be true for any group of siblings of p

class "code", false for other groups.



What is the purpose of using a boolean function in a group join?

Without this, the grouping key will be the result of evaluating an expression self::p[@class = 'code']

that returns an empty sequence, which in turn raises an error.

+2


source







All Articles