Re: [xsl] RE: loop elements based on attribute

Subject: Re: [xsl] RE: loop elements based on attribute
From: Martin Honnen <Martin.Honnen@xxxxxx>
Date: Sat, 16 Aug 2008 16:10:35 +0200
Mukul Gandhi wrote:

<xsl:for-each-group select="*" group-adjacent="self::Pkt">

produces following error [1],

Error on line 8 of test.xsl:
  XTTE1100: An empty sequence is not allowed as the @group-adjacent attribute of
 xsl:for-each-group in built-in template rule

1) Why do we get error [1] in the 1st case ?

Well the error message explains that, an empty sequence is not allowed as the grouping key for group-adjacent and your expression self::Pkt evaluates to an empty sequence for the 'A' and 'B' elements in the input XML.

2) In the second case, why putting the expression in 'boolean'
function works, while without boolean, we get an error ?

Now, using group-adjacent="boolean(self:Pkt)" the grouping key for the 'Pkt' elements is the boolean value true and for the other elements (i.e. the 'A' and 'B' elements) it is the boolean value false (as boolean(self:Pkt) is boolean() applied to an empty sequence which yields false).

> And, why do
we get 4 groups, and not 2 ?

See the definition for group-adjacent:

"the items in the population are examined, in population order. If an item has the same value for the grouping key as its preceding item within the population (in population order), then it is assigned to the same group as its preceding item; otherwise a new group is created and the item becomes its first member"

So the 'A' element which has the grouping key false becomes a member of the first group, then the 'Pkt' element following that becomes a member of the second group, all 'Pkt' elements following that also become a member of the second group, then the 'B' and 'A' element become a member of the third group, and finally the 'Pkt' elements become members of the fourth group.

If you only want to group the 'Pkt' elements then you could use e.g.
<xsl:if test="current-grouping-key()">
<xsl:copy-of select="current-group()"/>
as only for the 'Pkt' elements the group-adjacent="boolean(self::Pkt)" gives true.


	Martin Honnen

Current Thread