I am looking to transform a node, based on the number of elements. i.e. 5, then close the node and re-open a new one.
see example below
<root>
<branch>
<foo>bar</foo>
<foo>bar2</foo>
<foo>bar3</foo>
<foo>bar4</foo>
<foo>bar5</foo>
<foo>bar6</foo>
<foo>bar7</foo>
</branch>
<root>
Should become
<root>
<branch>
<foo>bar</foo>
<foo>bar2</foo>
<foo>bar3</foo>
<foo>bar4</foo>
<foo>bar5</foo>
<branch>
</branch>
<foo>bar6</foo>
<foo>bar7</foo>
</branch>
<root>
Please help on how this can be done in XSLT.
I. This XSLT 1.0 transformation:
when applied on the provided XML document (corrected to well-formed):
produces the wanted, correct result:
Explanation:
This is a case of “positional grouping”, where every starting element of a group is the first of a 5-tuple (so its position satisfies:
position() mod 5 = 1.II. XSLT 2.0 Solution:
when this XSLT 2.0 transformation is applied on the same XML document (above), the same wanted, correct result is produced.
Explanation:
Proper use of the
<xsl:for-each-group>XSLT 2.0 instruction with thegroup-adjacentattribute and thecurrent-group()function.