如何访问多个模板标签全局变量值(How to access global variable valu

2019-10-21 20:17发布

我创建了一个全局变量,并在两个模板其被用于我能够访问我第一个模板,没能获得在第二模板中的值。 下面是我的工作方式

 <xsl:variable name="currentValue"></xsl:variable> //global variable declaration

  <xsl:template match="/">
  <xsl:variable name="unique-accounts" select="/*/*/*/accountId/text()generate-id()=generate-id(key('account-by-id', .)[1])]"/>
 <xsl:for-each select="$unique-accounts">
      <xsl:variable name="currentValue" select="current()"/>
       <xsl:value-of select="$currentValue"/> //here value is printing
       <xsl:apply-templates select="//secondTemplate"/>
 </xsl:for-each>
 </xsl:template> //close od first template

<xsl:template match="secondTemplate"> 
  <xsl:value-of select="$currentValue"/>  //here value is not printing
</xsl:template>

Answer 1:

如果我按照你的逻辑代码正确(这不是完全肯定),你已经声明了一个全局变量为:

<xsl:variable name="currentValue"></xsl:variable>

即作为空。 然后,您在呼唤你的第二个模板中,这个全局变量:

<xsl:template match="secondTemplate"> 
  <xsl:value-of select="$currentValue"/>
</xsl:template>

并得到一个空的结果 - 这正是你应该期望什么。

在您的第一个模板,以下声明:

<xsl:variable name="currentValue" select="current()"/>

覆盖全局变量声明为模板的范围 (更准确地说,报关和他们的后代以下的兄弟姐妹-但是由于声明是您在模板做的第一件事,把它归结为同样的事情)。

在更多的技术术语,绑定模板阴影由顶级的绑定建立内部建立xsl:variable元素:
http://www.w3.org/TR/xslt/#dt-shadows



Answer 2:

在XSLT变量命名值,它们不是,你可以在不同的时间不同的地方值的存储位置。 这是声明和程序编程之间的根本区别。

如果你想解释一下你正在试图解决的问题(也就是输入和转换的输出),那么我敢肯定,我们可以解释如何把它写在XSLT。 逆向工程从一个完全错误的方法来解决的要求是不可能的。



文章来源: How to access global variable value in multiple template tags
标签: xslt