如何在XSLT中更改或重新分配变量?

时间:2013-10-08 18:09:22

标签: xml xslt

如何将值重新分配给先前分配的变量?我需要它像这样工作:

<xsl:variable name="variable2" select="'N'" />
....
<xsl:when test="@tip = '2' and $variable2 != 'Y'">                                                   
    <xsl:variable name="variable2" select="'Y'" />
</xsl:when>

5 个答案:

答案 0 :(得分:14)

XSLT中的变量可能只被赋值一次。这是通过设计完成的。请参阅Why Functional languages?以了解一般的动机。

不是重新分配变量,而是直接对输入文档写入条件,或者使用不同的本地参数递归调用函数(或命名模板)。

您需要做的任何事情都可以通过不需要重新分配变量的方法来完成。要获得更具体的答案,请提供更具体的问题。

另见:

答案 1 :(得分:5)

只需使用多个变量。这是你的工作实例......

    <xsl:variable name="variable1" select="'N'" />
    ....
    <xsl:variable name="variable2">
        <xsl:choose>
            <xsl:when test="@tip = '2' and $variable1 != 'Y'">
                <xsl:value-of select="'Y'" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$variable1" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

答案 2 :(得分:1)

你不能 - XSLT中的'变量'实际上更像是其他语言中的常量,它们不能改变价值。

答案 3 :(得分:0)

可以使用XSLT 3.0版提供的累加器声明可重新分配的变量。 :

    <?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" exclude-result-prefixes="xs" version="3.0" >
      <xsl:mode use-accumulators="variable2" streamable="no"/>
      <xsl:output omit-xml-declaration="no" indent="yes"/>

      <xsl:accumulator name="variable2" initial-value="'N'">
       <xsl:accumulator-rule match="Inpayment" select="if ($value = 'N' and @tip = '2') then 'Y' else 'N' "/>
      </xsl:accumulator>

      <xsl:template match="node()|@*">
        <xsl:copy>
          <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
      </xsl:template>

      <xsl:template match="Inpayment">
        <xsl:copy>
          <xsl:apply-templates select="@*"/>
          <xsl:value-of select="accumulator-before('variable2')"/>
          <xsl:apply-templates select="node()"/>
        </xsl:copy>
      </xsl:template>

    </xsl:stylesheet>

答案 4 :(得分:-1)

在测试之前,我几乎相信其他回复中的观点。 这样真的可以很好地运行。 xslt处理器是saxon-he 9.8.0.12

我的代码:

        <?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:map="http://www.w3.org/2005/xpath-functions/map"
    exclude-result-prefixes="xs map"
    version="2.0">

    <xsl:template match="/">
        <xsl:variable name="i1" select="123" as="xs:integer"/>
        <xsl:variable name="s1" select="'abcd'" as="xs:string"/>
        <xsl:variable name="d1" select="234.5" as="xs:float"/>

        <!-- we test that variable v1 can be assignment multi times and it is ok.  -->
        <xsl:variable name="v1" select="$i1"/>
        v1 is: <xsl:value-of select="$v1"/>
        <xsl:variable name="v1" select="$s1"/>
        v1 is: <xsl:value-of select="$v1"/>
        <xsl:variable name="v1" select="$d1"/>
        v1 is: <xsl:value-of select="$v1"/>

        <xsl:variable name="map1" select="map{'haha':119, 'good':110}"/>
        <xsl:variable name="map2" select="map:put($map1, 'go', 122)"/>
        <xsl:variable name="map1" select="map:put($map2, 'hello', 999)"/>
        map1(haha) is <xsl:sequence select="$map1?haha"/>
        map1(hello) is <xsl:sequence select="$map1?hello"/>
    </xsl:template>

</xsl:stylesheet>

the result screenshort of the running

作为您的问题,您可以这样做:

<xsl:variable name="variable2" select="'N'" />
<xsl:variable name="variable2" select="hello:func1()" />

<xsl:function name="hello:func1" as="xl:string">
    .....
    <xsl:when test="@tip = '2' and $variable2 != 'Y'">                                                   
        <xsl:value-of select="'Y'" />
    </xsl:when>
</xsl:function>
相关问题