4everJang
4everJang

Reputation: 333

XSLT Index of groups created by for-each-group

I need to divide a long table into smaller tables using for-each-group with the group-ending-with attribute. Inside each table I need to have the index of that group but I cannot find out which variable is available for that - if there is one.

Sample xml:

<table>
  <row type='A'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
  <row type='A'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
  <row type='A'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
  <row type='A'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
</table>

Desired output:

<table>
  <title>Table 1</title>
  <row type='A'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
</table>
<table>
  <title>Table 2</title>
  <row type='A'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
</table>
<table>
  <title>Table 3</title>
  <row type='A'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
</table>
<table>
  <title>Table 4</title>
  <row type='A'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
  <row type='B'><cell>Some value</cell>
</table>

I have the grouping done, but I cannot find a way to get the index to put into the table titles. Maybe it is trivial but the search words group and index do not give any useful result.

Upvotes: 1

Views: 493

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117102

Use the position() function.

No code, since no code was provided - and neither the sample input nor the expected output are well-formed XML.

(Addition by Michael Kay): Last para of §14.1 https://www.w3.org/TR/xslt-30/#element-for-each-group reads: -

"The sequence constructor contained in the xsl:for-each-group element is evaluated once for each of the groups, in processing order. The sequences that result are concatenated, in processing order, to form the result of the xsl:for-each-group element. Within the sequence constructor, the context item is the initial item of the relevant group, the context position is the position of this group in the processing order of the groups, and the context size is the number of groups. This has the effect that within the sequence constructor, a call on position() takes successive values 1, 2, ... last()."

Upvotes: 2

Related Questions