xsl-list
[Top] [All Lists]

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

2021-09-22 12:06:33
Hi,

Another optimization to consider is to group using
group-adjacent="@class='analyze_visual'" returning a Boolean grouping key,
and groups that are nicely split.

Then part/@num should collapse into '{position()}' and things get simpler.
Also gracefully handles the case of multiple splits.

Cheers, Wendell




On Tue, Sep 21, 2021 at 10:19 PM Dimitre Novatchev 
dnovatchev(_at_)gmail(_dot_)com <
xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

Just replace the variable definition with this:

        <xsl:variable name="part3ID">
            <xsl:sequence select=
            "string(
                (current-group()[self::div[@class='analyze_visual']],
                 preceding-sibling::div[@class='analyze_visual'][1]
                )[1]/@nextExposID
                )"/>
        </xsl:variable>

The complete transformation becomes:

<xsl:stylesheet version="2.0" xmlns:xsl="
http://www.w3.org/1999/XSL/Transform";>
  <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <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:sequence select=
            "string(
                (current-group()[self::div[@class='analyze_visual']],
                 preceding-sibling::div[@class='analyze_visual'][1]
                )[1]/@nextExposID
                )"/>
        </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="globalNum">
                        <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>

And when applied on the provided XML document:
<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>

The wanted result is produced:

<div id="584175">
   <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="584176">
      <p>Ethics</p>
      <p>More content</p>
   </part>
</div>

Cheers,
Dimitre


On Tue, Sep 21, 2021 at 10:11 AM Terry Ofner tdofner(_at_)gmail(_dot_)com
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

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=“analyze_visual”> as the trigger:

<div id="584175”>

    <part num=“1” globalNum=“584175">
        <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=“div[@class=‘analyze_visual’] 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"></xsl: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=“globalNum">
                        <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 doesn’t 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="584175”>

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



XSL-List info and archive
EasyUnsubscribe (by email)



--
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they
write all patents, too? :)
-------------------------------------
Sanity is madness put to good use.
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.




-- 
...Wendell Piez... ...wendell -at- nist -dot- gov...
...wendellpiez.com... ...pellucidliterature.org... ...pausepress.org...
...github.com/wendellpiez... ...gitlab.coko.foundation/wendell...
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>