[xsl] Two possible group-ending-with nodes

Subject: [xsl] Two possible group-ending-with nodes
From: "rick@xxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 6 Jan 2023 14:42:22 -0000
Hi All,

 

I am trying to add some hierarchy to flat content. I have this as an input
file:

 

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

<root>

    <p class="CBNote"/>

    <p class="CBNoteBody"/>

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

    <p class="CBBodyIndented"/>

    <p class="CBBody"/>

</root>

 

This is my desired output:

 

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

<root>

    <div class="note">

        <div class="note-header">

            <p class="CBNote"/>

        </div>

        <div class="note-content">

            <p class="CBNoteBody"/>

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

        </div>

    </div>

    <p class="CBBodyIndented"/>

    <p class="CBBody"/>

</root>

 

After the note-header, the bottom boundary of the note content is either one
of these:

 

p[@class='CBNoteBody']|p[@class='CBBodyIndented'][@style='margin-left:96px;'
]

 

Here is my stylesheet:

 

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

                            <xsl:for-each-group
select="tail(current-group())"
group-ending-with="p[@class='CBNoteBody']|p[@class='CBBodyIndented'][@style=
'margin-left:96px;']">

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

                            </xsl:for-each-group>

                        </div>

                    </xsl:when>

                </xsl:choose>

            </xsl:for-each-group>

        </xsl:copy>    

    </xsl:template>

    

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

    

</xsl:stylesheet>

 

Here is my current output:

 

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

<root>

   <div class="note">

      <div class="note-header">

         <p class="CBNote"/>

      </div>

      <p class="CBNoteBody"/>

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

      <p class="CBBodyIndented"/>

   </div>

</root>

 

It looks like my group-ending-with is giving me 3 groups instead of 2 like I
expected. Any pointers will be appreciated. Thank you.

 

Rick

Current Thread