|
[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
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!
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|

Cart








