Accessing a variable outside the for loop in xsl

余生颓废 提交于 2019-12-14 04:08:18

问题


I am setting a variable as shown below..

<xsl:variable name="FoundFloating"> <xsl:value-of select="'no'" />
    </xsl:variable>

Now I am doing some processing as shown below ..

<xsl:if  test="$abcid=$def_id"> 
<xsl:for-each "$abcd">
                    <xsl:variable name="abcRate"> <xsl:value-of select="./def_Period/"/>
                    </xsl:variable>
                    <xsl:choose>
                        <xsl:when test="$abcdf !=$abcRate">
                        <xsl:variable name="$FoundFloating"> <xsl:value-of select="yes" />
                            </xsl:variable>
                        </xsl:when>
                        </xsl:choose>
            </xsl:for-each>

Now after this xsl for I am evaluating as shown below.. But my query is that whether foundfloating variable is accessible as the for loop is already ended..

<xsl:choose>
                <xsl:when test="$FoundFloating='yes'">  <xsl:value-of select="'AAA'" />
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="'BBBA'" />
                </xsl:otherwise>
            </xsl:choose>

Now after this xsl for I am evaluating as shown below.. But my query is that whether foundfloating variable is accessible as the for loop is already ended.. Please advise on this as I have updated the post


回答1:


The first thing to note is that variables are immutable in XSLT. This means once declared, they cannot be changed. What is actually happening in your XSLT sample is you are declaring a whole new variable, with the same name.

The FoundFloating variable you are declaring within the for loop will not be accessible outside the for loop, as it will only be local in scope. In fact, it will only be accessible inside the xsl:when statement it is defined in. It is a different variable to the global one you defined, and only exists in the loop.

You don't really need the loop here. You can combine the condition in the xsl:for-each and the condition in the xsl:when into a single variable declaration.

<xsl:if test="$abcid=$def_id"> 
   <xsl:variable name="FoundFloating">
      <xsl:if test="$abcd[def_Period != $abcdf]">yes</xsl:if>
   </xsl:variable>
   <xsl:choose>
       <xsl:when test="$FoundFloating='yes'">

(This replaces the xsl:for-each statement entirely)

In fact, this can be simplified even more, by simply setting FoundFloating to the node itself (if there is one), rather than "yes"

<xsl:if test="$abcid=$def_id"> 
   <xsl:variable name="FoundFloating" select="$abcd[def_Period != $abcdf]" />
   <xsl:choose>
      <xsl:when test="$FoundFloating">

This works because the essence of the test is whether a certain node exists matching the condition. Rather than setting a variable to "Yes" or "No" if it exists or not, the variable is set to the node itself. Then, if it does the exist the statement <xsl:when test="$FoundFloating"> returns true, but false if it doesn't.

So, you don't need the xsl:for-each loop, and you only need to declare the FoundFloating variable once.



来源:https://stackoverflow.com/questions/21522194/accessing-a-variable-outside-the-for-loop-in-xsl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!