[xsl] Including precediing-siblings with the first group

Subject: [xsl] Including precediing-siblings with the first group
From: "rick@xxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 18 Aug 2022 14:27:35 -0000
Hi All,



I have an input file that I want to split into files at each section. The
first section should include everything before it. Below are my input, desired
output, and stylesheet. I am pretty sure this is the simplest approach, but
any suggestions would be appreciated. Thank you.



Rick



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

<chapter id="Assault_gd1g1l1027760">

    <title>Transferred Intent</title>

    <toc/>

    <para/>

    <section id="GI_gd1g1o1030579">

        <title>General Comments</title>

        <para>General comments here</para>

    </section>

    <section id="GI_gd1g1p1030593">

        <title>InstructionbTransferred IntentbDifferent Offense</title>

        <para>Instructions here</para>

    </section>

    <section id="GI_gd1g1q1030657">

        <title>InstructionbTransferred IntentbDifferent Person or
Propertyo?=</title>

        <para>More instructions here</para>

    </section>

</chapter>



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

<root>

   <file>

      <title>Transferred Intent</title>

      <toc/>

      <para/>

      <section id="GI_gd1g1o1030579">

         <title>General Comments</title>

         <para>General comments here</para>

      </section>

   </file>

   <file>

      <section id="GI_gd1g1p1030593">

         <title>InstructionbTransferred IntentbDifferent Offense</title>

         <para>Instructions here</para>

      </section>

   </file>

   <file>

      <section id="GI_gd1g1q1030657">

         <title>InstructionbTransferred IntentbDifferent Person or
Property</title>

         <para>More instructions here</para>

      </section>

   </file>

</root>



<?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:mode on-no-match="shallow-copy"/>



    <xsl:template match="/chapter">

        <root>

            <xsl:for-each-group select="*" group-starting-with="section">

                <xsl:choose>

                    <xsl:when test="not(self::section)">

                        <file>

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

                            <xsl:apply-templates
select="following-sibling::section[1]"/>

                        </file>

                    </xsl:when>

                    <xsl:when
test="self::section[preceding-sibling::section]">

                        <file>

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

                        </file>

                    </xsl:when>

                </xsl:choose>

            </xsl:for-each-group>

        </root>



    </xsl:template>



</xsl:stylesheet>

Current Thread