[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

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


Subject: [xsl] Nesting elements and children using for-each-group:
From: "Mark Peters markpeters.work@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 11 Jan 2016 17:47:48 -0000

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>

Here's one example of the many XSL templates I've tried unsuccessfully.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
    <xsl:output method="xml" indent="yes" />

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

    <!--  -->
    <xsl:template match="topic">
        <xsl:copy>
            <xsl:apply-templates select="@id"/>
            <xsl:apply-templates select="title"/>
            <body>
                <xsl:apply-templates select="body/*[not(section)]"/>
            </body>
            <xsl:for-each-group
select="section[not(@outputclass='nested')]|section[@outputclass='nested']"
group-starting-with="section[not(@outputclass='nested')]">
                <section>
                    <xsl:apply-templates select="."/>
                    <!-- . is the group leader -->
                    <xsl:if test="current-group()[2]">
                        <!-- current-group() except . is the rest -->
                        <xsl:for-each-group select="current-group() except
." group-starting-with="section[@outputclass='nested']">
                            <section>
                                <xsl:apply-templates select="."/>
                            </section>
                            <!-- . is the h2 -->
                        </xsl:for-each-group>

                    </xsl:if>
                </section>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

Output:

<?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>

In general, it doesn't take me this long to grasp XSL concepts. This one is
throwing me for a loop.

Thanks in advance for any suggestions you can offer.

Mark

-- 
Senior technical writer
Bloomberg BNA Software


Current Thread
Keywords
xsl