[xsl] Value of variable not appearing in the <xsl:otherwise> of xml:choose

Subject: [xsl] Value of variable not appearing in the <xsl:otherwise> of xml:choose
From: "Terry Ofner tdofner@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 21 Sep 2021 17:11:05 -0000
I have this xml input:

<div id="584175">
    <p>1.2 Modes</p>
    <p>Some content</p>
    <div class="analyze_visual" lookup="2.1.2.2" av_itemIDnum="584177"
nextExposID="584176">
        <p>Analyzing the Visual</p>
        <div class="av" type="analyze">
            <img alt="" src="http://assets.xxx.com/xxx.png"/>
            <p class="caption">Figure 2-2</p>
        </div>
    </div>
    <p>Ethics</p>
    <p>More content</p>
</div>


I need to divide it into three parts with any <div class=banalyze_visualb>
as the trigger:

<div id="584175b>

    <part num=b1b globalNum=b584175">
        <p>1.2 Modes</p>
        <p>Some content</p>
    </part>

    <part num="2">
        <div class="analyze_visual" lookup="2.1.2.2" av_itemIDnum="584177"
nextExposID="584176">
            <p>Analyzing the Visual</p>
            <div class="av" type="analyze">
                <img alt="" src="http://assets.xxx.com/xxx.png"/>
                <p class="caption">Figure 2-2</p>
            </div>
        </div>
    </part>

    <part num="3" globalNum="584176">
        <p>Ethics</p>
        <p>more content</p>
    </part>

</div>


I am using for-each-group()
group-ending-with=bdiv[@class=banalyze_visualb] to accomplish the
creation of the parts.
The Issue: I am having trouble transferring the attribute nextExposID into the
third part. Here is the stylesheet:

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

<xsl:template match="div">
        <xsl:variable name="exploreExposID">
            <xsl:value-of select="@id"/>
        </xsl:variable>
        <div>
            <xsl:copy-of select="@*"/>
    <xsl:for-each-group select="*"
group-ending-with="div[@class='analyze_visual']">
        <xsl:variable name="part3ID">
            <xsl:value-of
select="current-group()[self::div[@class='analyze_visual']]/@nextExposID"></x
sl:value-of>
        </xsl:variable>
        <xsl:choose>
            <xsl:when
test="current-group()[self::div[@class='analyze_visual']]">
                <part num="1" globalNumn="{$exploreExposID}">
                    <xsl:copy-of
select="current-group()[not(self::div[@class='analyze_visual'])][not(self::h9
)]"/>
                </part>
                <part num="2" globalNum="{$part3ID}">
                    <xsl:copy-of
select="current-group()[self::div[@class='analyze_visual']]"></xsl:copy-of>
                </part>
            </xsl:when>
            <xsl:otherwise>
                <part num="3">
                    <xsl:attribute name=bglobalNum">
                        <xsl:value-of select="$part3ID"/>
                    </xsl:attribute>
                    <xsl:copy-of select="current-group()"/>
                </part>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:for-each-group>
        </div>
    </xsl:template>

</xsl:stylesheet>


Notice in the result below that the variable part3ID appears in part 2 just
fine, but it doesnbt display in part 3. I suspect this is due to the
<otherwise> of the xsl:choose. This seems strange to me, since I have declared
the variable outside the xsl:choose.

My questions
Is there a way to pass that variable to part 3?
Or, is there a better way to create the parts I need?

(I know that my numbering of the parts is clunky. I will address the numbering
to accommodate situations with more than one <div> after I solve this issue.)

Output modified slightly with returns to improve readability.

<div id="584175b>

   <part num="1" globalNumn="584175">
      <p>1.2 Modes</p>
      <p>Some content</p>
   </part>

   <part num="2" globalNum="584176">
      <div class="analyze_visual"
           lookup="2.1.2.2"
           av_itemIDnum="584177"
           nextExposID="584176">
         <p>Analyzing the Visual</p>
         <div class="av" type="analyze">
            <img alt="" src="http://assets.xxx.com/xxx.png"/>
            <p class="caption">Figure 2-2</p>
         </div>
      </div>
   </part>

   <part num="3" globalNum="">
      <p>Ethics</p>
      <p>More content</p>
   </part>

</div>

Current Thread