Re: [xsl] Two possible group-ending-with nodes

Subject: Re: [xsl] Two possible group-ending-with nodes
From: "rick@xxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 6 Jan 2023 15:47:49 -0000
Martin, I see your solution in the previous post and it is cleaner using the
note variable. Thank you very much!

 

Rick

 

From: rick@xxxxxxxxxxxxxx <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> 
Sent: Friday, January 6, 2023 10:40 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] 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 <mailto:michaelkay90@xxxxxxxxx>
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx
<mailto:xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> > 
Sent: Friday, January 6, 2023 10:05 AM
To: xsl-list <xsl-list@xxxxxxxxxxxxxxxxxxxxxx
<mailto:xsl-list@xxxxxxxxxxxxxxxxxxxxxx> >
Subject: Re: [xsl] 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) 

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