I'm trying to group elements using xsl:for-each in XSLT 2.0.
Here is an illustration of my input:
<root>
<chapter>
<heading> heading text </heading>
<title> title </title>
<p> 1111 </p>
<p> 2222 </p>
<center> text </center>
<p> 3333 </p>
<title> another title </title>
<p> 4444 </p>
<center> text </center>
<p> 5555 </p>
</chapter>
<chapter>
<heading> heading text </heading>
<title> another title </title>
<p> 6666 </p>
<p> 7777 </p>
<title> another title </title>
<p> 8888 </p>
<p> 9999 </p>
</chapter>
<root>
I am trying to group this document by matching on each <title>
element and grouping every following element until the next <title>
into an element <section>
. Here is what I want my output to look like:
<root>
<chapter>
<heading> Heading text </heading>
<section>
<title> title </title>
<p> 1111 </p>
<p> 2222 </p>
<center> text </center>
<p> 3333 </p>
</section>
<section>
<title> title </title>
<p> 4444 </p>
<center> text </center>
<p> 5555 </p>
</section>
<section>
<title> title </title>
<p> 6666 </p>
<p> 7777 </p>
<center> text </center>
<p> 8888 </p>
<p> 9999 </p>
</section>
<chapter>
<root>
My current template that is not working:
<xsl:template match="chapter">
<chapter>
<xsl:for-each-group select="*" group-starting-with="title">
<section>
<xsl:copy-of select="current-group()" />
</section>
</xsl:for-each-group>
</chapter>
</xsl:template>
The stylesheet above does group the sections that I want however however it also groups each <heading>
element into it's own <section>
for some reason. Any suggestions?
Thanks in advance.