Xslt 分配给<;xsl:variable>;退却后

Xslt 分配给<;xsl:variable>;退却后,xslt,xslt-2.0,Xslt,Xslt 2.0,我用下面的方法给变量赋值 <xsl:variable name="NewValue"> <xsl:value-of select="normalize-space(//root/id/amount)"/> </xsl:variable> 赋值之后,我想给同一个变量赋值。像这样:- <xsl:variable name="NewValue" select="normalize-space(//root/id/amountnew)">

我用下面的方法给变量赋值

<xsl:variable name="NewValue">
  <xsl:value-of select="normalize-space(//root/id/amount)"/>
</xsl:variable>

赋值之后,我想给同一个变量赋值。像这样:-

<xsl:variable name="NewValue" select="normalize-space(//root/id/amountnew)">

有什么办法吗


下面是我的XML示例:

<VolLien>
  <Vest_DocType>SDD</Vest_DocType>
  <Vest_Instrument>395072</Vest_Instrument>
  <Vest_OfOfficialEntity>eee</Vest_OfOfficialEntity>
  <Vest_RecDate>12/24/2009</Vest_RecDate>
  <Vest_Grantee1>abc dd</Vest_Grantee1>
  <Vest_Grantor1>sss</Vest_Grantor1>
  <Vest_RejectCode />
  <Vest_RejectReason /> 
  <Vest_ImageNum> </Vest_ImageNum>
</VolLien>

SDD
395072
eee
12/24/2009
abc dd
sss
我的问题是,我需要获取最新的
(比如SDD),然后我需要在xml中搜索此(相同SDD)的
之前的任何日期


然后单独提出该特定部分(
),并再次提出最新版本。如果可以进行重新分配,我将定位节点的位置,并获取与该节点关联的值。现在我用另一个循环来做这个。如果有的话,我可以避免extrs循环

否。XSLT变量是只读的。不能多次分配它们

XSLT不是像PHP这样的命令式编程语言。重新分配变量既不可能也没有必要


编辑:根据您的评论:

我的问题是我需要获取最新的特定的
(比如SDD),然后我需要在xml中搜索此(相同SDD)的
之前的任何日期

下面是一个XSLT代码段,可以为您实现这一点:

<!-- a key to retrieve all <VolLien> nodes of a particular DocType -->
<xsl:key name="VolLien-by-DocType" match="VolLien" use="Vest_DocType" />

<xsl:template match="/">
  <xsl:call-template name="latest-VolLien">
    <xsl:with-param name="DocType" select="'SDD'" />
  </xsl:call-template>
</xsl:template>

<!-- this template processes specified <VolLien> nodes in the right order -->
<xsl:template name="latest-VolLien">
  <xsl:param name="DocType" select="''" />

  <xsl:for-each select="key('VolLien-by-DocType', $DocType)">
    <!-- sorting is a little complicated because you 
         use dd/mm/yyyy instead of yyyymmdd -->
    <xsl:sort 
      select="
        number(substring(Vest_RecDate, 7, 4)) + 
        number(substring(Vest_RecDate, 4, 2)) + 
        number(substring(Vest_RecDate, 1, 2))
      "
      data-type="number"
    />
    <!-- do something with last <VolLien> (in date order) -->
    <xsl:if test="position() = last()">
      <xsl:apply-templates select="." />
    </xsl:if>
  </xsl:for-each>
</xsl:template>

<xsl:template match="VolLien">
  <!-- this template will actually process the nodes,
       do whatever you want here -->
</xsl:template>

添加到Tomalak的答案中

变量被限制/局部到一个块,在该块中它被声明并赋值(声明和赋值同时发生)

在示例中假设:

  <xsl:template match="Node1">
    <xsl:variable name="test" select="'test_value'"/>
    <xsl:for-each select="foo">
      <xsl:variable name="var1" select="."/>
      <xsl:value-of select="$var1"/>
    </xsl:for-each>
    <xsl:for-each select="bar">
      <xsl:value-of select="$var1"/>
      <!--The variable "$var1" is out of scope.. So above statement is wrong-->
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="Node2">
    <xsl:value-of select="$test"/>
    <!--The variable "$test" is out of scope.. So above statement is wrong too!-->
  </xsl:template>


对前面答案的进一步补充:您正在尝试使用从其他编程语言学到的低级命令式技术来解决一些问题。为了帮助您使用适合XSLT的声明性方法解决问题,我们需要对问题进行描述。尽可能高层次-将输出描述为输入的函数,而不是描述从一个输入到另一个输入所需的计算步骤。

请添加您正在处理的XML示例,以及您尝试执行的操作的简短说明。您好,我用xml数据在xsl中进行了一些规则操作。对于某个场景,我想为已经声明的变量重新分配另一个值。这就是我寻找任何像这样的选项的原因,无论如何,谢谢你的快速响应和宝贵的支持input@Varun我还没有完全理解你的要求,但是看看我修改过的答案,也许这会让你开始。(附言:你可以删除这些评论,因为我把它们移到了问题上。)