Re: [xsl] Nesting elements and children using for-each-group:

Subject: Re: [xsl] Nesting elements and children using for-each-group:
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 11 Jan 2016 17:56:55 -0000
Mark Peters markpeters.work@xxxxxxxxx wrote:
Hi,

Maybe I'm using the wrong XSL instruction. Maybe for-each-group is the
wrong option. I've spent the last few days reading about this
instruction and trying variations based on examples posted in this and
other forums without success. Most of the examples seem focused on
either organizing a flat structure such as HTML or pulling nested
elements up to the top level.

Here's a simplified version of my source XML:

<?xml version="1.0" encoding="utf-8"?>
<topic id="topica">
     <title>Topic A</title>
     <body>
         <p>Random content.</p>
         <section id="section1">
             <title>Section 1</title>
             <p>Random content.</p>
         </section>
         <section id="section2">
             <title>Section 2</title>
             <p>Random content.</p>
         </section>
         <section id="section2a" outputclass="nested">
             <title>Section 2a</title>
             <p>Random content.</p>
         </section>
         <section id="section2b" outputclass="nested">
             <title>Section 2b</title>
             <p>Random content.</p>
         </section>
         <section id="section3">
             <title>Section 3</title>
             <p>Random content.</p>
         </section>
         <section id="section3a" outputclass="nested">
             <title>Section 3a</title>
             <p>Random content.</p>
         </section>
     </body>
</topic>

What I want to do is nest all <section /> elements with the
@outputclass="nested" attribute under the immediately preceding <section
/> element that has no such attribute.

All of the sections, in turn, would follow the topic/body node rather
than be nested within it.

Desired output:

<?xml version="1.0" encoding="utf-8"?>
<topic id="topica">
     <title>Topic A</title>
     <body>
         <p>Random content.</p>
     </body>
     <section id="section1">
         <title>Section 1</title>
         <p>Random content.</p>
     </section>
     <section id="section2">
         <title>Section 2</title>
         <p>Random content.</p>
         <section id="section2a" outputclass="nested">
             <title>Section 2a</title>
             <p>Random content.</p>
         </section>
         <section id="section2b" outputclass="nested">
             <title>Section 2b</title>
             <p>Random content.</p>
         </section>
     </section>
     <section id="section3">
         <title>Section 3</title>
         <p>Random content.</p>
         <section id="section3a" outputclass="nested">
             <title>Section 3a</title>
             <p>Random content.</p>
         </section>
     </section>
</topic>

Try


<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="2.0">


    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>


<xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template>

<xsl:template match="body">
<xsl:copy>
<xsl:apply-templates select="node() except section"/>
</xsl:copy>
<xsl:for-each-group select="section" group-starting-with="section[not(@outputclass = 'nested')]">
<xsl:copy>
<xsl:apply-templates select="@*, node(), current-group() except ."/>
</xsl:copy>
</xsl:for-each-group>
</xsl:template>
</xsl:transform>


Current Thread