abondormgag
New Member
I'm trying to group elements using xsl:for-each in XSLT 2.0.Here is an illustration of my input:\[code\]<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>\[/code\]I am trying to group this document by matching on each \[code\]<title>\[/code\] element and grouping every following element until the next \[code\]<title>\[/code\] into an element \[code\]<section>\[/code\]. Here is what I want my output to look like:\[code\]<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>\[/code\]My current template that is not working:\[code\]<xsl:template match="chapter"> <xsl:for-each-group select="*" group-starting-with="title"> <section> <xsl:value-of select="current-group()" /> </section> </xsl:for-each-group> </xsl:template>\[/code\]The stylesheet above does group the sections that I want however it breaks my nesting and totally erases the \[code\]<chapter>\[/code\] element. It also groups each \[code\]<heading>\[/code\] element into it's own \[code\]<section>\[/code\]. Any suggestions?Thanks in advance.