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

Re: Two possible group-ending-with nodes

Subject: Re: Two possible group-ending-with nodes
From: "rick@xxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 6 Jan 2023 15:40:05 -0000
Re:  Two possible group-ending-with nodes
This definitely makes sense. I think it is a group-adjacent problem. Here is
my modified input with the correct output:

 

<?xml version="1.0" encoding="UTF-8"?>

<root>

    <p class="CBBodyIndented">Out 1</p>

    <p class="CBNote">In header</p>

    <p class="CBNoteBody">In content 1</p>

    <p class="CBBodyIndented" style="margin-left:96px;">In content 2</p>

    <p class="CBBodyIndented">Out 2</p>

    <p class="CBBody">Out 3</p>

    <p class="CBBodyIndented" style="margin-left:96px;">Out 4</p>

</root>

 

<?xml version="1.0" encoding="UTF-8"?>

<root>

   <p class="CBBodyIndented">Out 1</p>

   <div class="note">

      <div class="note-header">

         <p class="CBNote">In header</p>

      </div>

      <div class="note-content">

         <p class="CBNoteBody">In content 1</p>

         <p class="CBBodyIndented" style="margin-left:96px;">In content
2</p>

      </div>

   </div>

   <p class="CBBodyIndented">Out 2</p>

   <p class="CBBody">Out 3</p>

   <p class="CBBodyIndented" style="margin-left:96px;">Out 4</p>

</root>

 

Below is my stylesheet. I am using the same for-each-group group-adjacent
loop twice because I need some content inside the <div class-"note-content">
and the rest of it outside. Any suggestions for a better approach will be
appreciated. Thank you!

 

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

    xmlns:math="http://www.w3.org/2005/xpath-functions/math"

    exclude-result-prefixes="xs math"

    version="3.0" expand-text="yes">

    

    <xsl:output indent="yes"/>

    

    <xsl:template match="/root">

        <xsl:copy>

            <xsl:for-each-group select="*"
group-starting-with="p[@class='CBNote']">

                <xsl:choose>

                    <xsl:when test="self::p[@class='CBNote']">

                        <div class="note">

                            <div class="note-header">

                                <xsl:copy-of select="."/>

                            </div>

                            <div class="note-content">

                                <xsl:for-each-group
select="tail(current-group())"
group-adjacent="if(self::p[@class='CBNoteBody']|self::p[@class='CBBodyIndent
ed'][@style='margin-left:96px;']) then 1 else 0">

                                    <xsl:if test="current-grouping-key()=1
and position()=1">

                                        <xsl:copy-of
select="current-group()"/>

                                    </xsl:if>

                                </xsl:for-each-group>

                            </div>

                        </div>

                    </xsl:when>

                    <xsl:otherwise><xsl:apply-templates
select="current-group()"/></xsl:otherwise>

                </xsl:choose>

                <xsl:for-each-group select="tail(current-group())"
group-adjacent="if(self::p[@class='CBNoteBody']|self::p[@class='CBBodyIndent
ed'][@style='margin-left:96px;']) then 1 else 0">

                    <xsl:if test="current-grouping-key()=0 or
position()&gt;1">

                        <xsl:copy-of select="current-group()"/>

                    </xsl:if>

                </xsl:for-each-group>

            </xsl:for-each-group>

        </xsl:copy>    

    </xsl:template>

    

    <xsl:mode on-no-match="shallow-copy"/>

    

</xsl:stylesheet>

 

From: Michael Kay michaelkay90@xxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> 
Sent: Friday, January 6, 2023 10:05 AM
To: xsl-list <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Subject: Re:  Two possible group-ending-with nodes

 

It ends the first inner group at the first element because it matches the
first condition, p[@class='CBNoteBody']; it ends the second inner group at
the second element because it matches the second condition
p[@class='CBBodyIndented'][@style='margin-left:96px;'], and it ends the
third inner group at the third element because everything after the last
matching element goes in a final group regardless.

Michael Kay
Saxonica

 

XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>  

EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/612310>  (by
email <> ) 

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.