[xsl] Process the following group

Subject: [xsl] Process the following group
From: "rick@xxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 7 Jul 2022 19:10:26 -0000
Hi All,

 

I have something like this:

 

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

<root>

    <div1/>

    <div1/>

    <div2/>

    <div2/>

    <div1/>

</root>

 

And I want to end up with this:

 

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

<root>

    <div1/>

    <div1>

        <div2/>

        <div2/>

    </div1>

    <div1/>

</root>

 

Here is my stylesheet. When I am the last in the <div1> group, I want to
process the following <div2> group, if there is one.

 

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

        <root>

            <xsl:for-each-group select="*" group-adjacent="local-name()">

                <xsl:choose>

                    <xsl:when test="local-name(current-group()[1])='div1'">

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

                    </xsl:when>

                </xsl:choose>

            </xsl:for-each-group>

        </root>

    </xsl:template>

    

    <xsl:template match="div1">

        <div1>

            <!-- Am I the last in the group? -->

            <xsl:if test="position()=last()">

                <xsl:comment>I want process the entire following div2 group
in here.</xsl:comment>

            </xsl:if>

        </div1>

    </xsl:template>

    

</xsl:stylesheet>

 

Thanks for any pointers you can provide.

 

Rick

 

Rick Quatro

Carmen Publishing Inc.

585-729-6746

rick@xxxxxxxxxxxxxxx <mailto:rick@xxxxxxxxxxxxxxx> 

http://www.frameexpert.com/store

Current Thread