xsl声明变量并重新分配/更改for-each中的值不起作用

时间:2014-02-09 19:45:51

标签: xslt

我在for-each中声明变量“flag”,并为每个重新分配值。我在范围内得到错误重复变量。

My code is:

  <xsl:variable name="flag" select="'0'"/>
<xsl:template match="/">
     <xsl:for-each select="Properties/Property">
          <xsl:variable name="flag" select="'0'"/>
     <xsl:choose>             
                  <xsl:when test="$language='en-CA'">                 
                    <xsl:for-each select="Localization/[Key=$language]">
                      <xsl:value-of select="Value/Value"/>
                      <xsl:variable name="flag" select="'1'"/>
                    </xsl:for-each>
                    <xsl:if test="$flag ='0'">
                    <xsl:value-of select="$flag"/>
                    </xsl:if>

                  </xsl:when>
    </xsl:choose>
    </xsl:for-each>
</xsl:template>

我们可以更新/重新分配变量值吗?如果没有,我们还有其他选择吗?     有什么帮助吗?

4 个答案:

答案 0 :(得分:1)

XSLT不是一种过程语言,XSLT中的变量在过程语言中的行为不像变量;他们的行为更像是数学中的变量。也就是说,它们是价值的名称。公式x = x + 1在数学中毫无意义,在XSLT中也没有意义。

对程序代码中的规范进行反向工程总是很困难,尤其是来自不正确的过程代码。那么告诉我们你想要实现的目标,我们将告诉你XSLT的方式(即声明/功能方式)。

答案 1 :(得分:0)

XSLT变量是单一赋值。

答案 2 :(得分:0)

您可以创建xsl模板并执行xsl递归。 例如:

<xsl:template name="IncrementUntil5">
    <xsl:param name="counter" select="number(1)" />
    <xsl:if test="$counter &lt; 6">
        <test><xsl:value-of select="$counter"/></test>
        <xsl:call-template name="IncrementUntil5">
            <xsl:with-param name="counter" select="$counter + 1"/>
        </xsl:call-template>
    </xsl:if>        
</xsl:template>

然后这样称呼:

   <xsl:template match="/">       
        <div>
            <xsl:call-template name="IncrementUntil5"/>
        </div>    
    </xsl:template>

答案 3 :(得分:0)

试试这个:

<xsl:template match="/">
  <xsl:for-each select="Properties/Property">
    <xsl:choose>             
      <xsl:when test="$language='en-CA' and Localization/Key='en-CA'">                 
        <xsl:value-of select="Value/Value"/>
      </xsl:when>
    </xsl:choose>
  </xsl:for-each>
</xsl:template>

您不需要遍历集合来确定是否存在某些内容,如果存在与之匹配的任何元素,则简单的XPath Localization/Key='en-CA'将为true。

相关问题