[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Nesting elements and children using for-each-group

Subject: Re: 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
Re:  Nesting elements and children using for-each-group
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.