[xsl] Grouping based on child value

Subject: [xsl] Grouping based on child value
From: "rick@xxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 5 Jun 2025 18:48:47 -0000
Hi all,



Here is my XML:



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

<root>

    <record>

        <col1>PG</col1>

    </record>

    <record>

        <col1>1</col1>

    </record>

    <record>

        <col1>2</col1>

    </record>

    <record>

        <col1>PG</col1>

    </record>

    <record>

        <col1>3</col1>

    </record>

    <record>

        <col1>4</col1>

    </record>

</root>



I want to group all of the <record> elements, starting with
col1[starts-with(.,'PG')]. I am expecting 2 groups of 3 record elements
each. Here is my style sheet:



<?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="3.0" expand-text="yes">



    <xsl:output indent="yes"/>



    <xsl:template match="root">

        <xsl:copy>

            <xsl:message
select="count(record[col1[starts-with(.,'PG')]])"></xsl:message>

            <xsl:for-each-group select="record"
group-starting-with="descendant::col1[.='PG']">

                <group></group>

            </xsl:for-each-group>

        </xsl:copy>

    </xsl:template>



</xsl:stylesheet>



However, my result is a single group:



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

<root>

   <group/>

</root>



Any help would be appreciated. Thank you.



Rick Quatro

Current Thread