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

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


Subject: Re: [xsl] Nesting elements and children using for-each-group:
From: "G. Ken Holman g.ken.holman@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 11 Jan 2016 18:02:44 -0000

Nope! I see I messed up my solution as I forgot to handle the other body content (I was too focused on the sections).

It looks like Martin has posted an example while I was writing mine ... use his.

Good luck!

. . . . . . . . Ken

At 2016-01-11 12:58 -0500, G. Ken Holman wrote:
At 2016-01-11 17:47 +0000, Mark Peters markpeters.work@xxxxxxxxx wrote:
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.

I think the following will do it for you.


I hope this helps!

. . . . . Ken

t:\ftemp>type mark.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>

t:\ftemp>call xslt2 mark.xml mark.xsl
<?xml version="1.0" encoding="UTF-8"?><topic id="topica">
    <title>Topic A</title>

        <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>
t:\ftemp>type mark.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs"
  version="2.0">

<xsl:template match="@*|node()"><!--identity for all other nodes-->
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="body">
  <xsl:for-each-group group-starting-with="section[not(@outputclass)]"
                      select="node()">
    <xsl:choose>
      <xsl:when test="not(self::section)">
        <!--process the content without handling-->
        <xsl:apply-templates select="."/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy>
          <xsl:apply-templates select="@*"/>
          <xsl:apply-templates/>
          <xsl:apply-templates select="current-group() except self::*"/>
        </xsl:copy>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:for-each-group>
</xsl:template>

</xsl:stylesheet>
t:\ftemp>rem Done!


--
Check our site for free XML, XSLT, XSL-FO and UBL developer resources |
Free 5-hour lecture:  http://www.CraneSoftwrights.com/links/video.htm |
Crane Softwrights Ltd.             http://www.CraneSoftwrights.com/s/ |
G. Ken Holman                    mailto:gkholman@xxxxxxxxxxxxxxxxxxxx |
Google+ profile:       http://plus.google.com/+GKenHolman-Crane/about |
Legal business disclaimers:     http://www.CraneSoftwrights.com/legal |


--- This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus


Current Thread
Keywords