我创建了一个全局变量,它已经在两个模板中使用,我能够访问第一个模板,无法在第二个模板中获取值。以下是我的工作
<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>
答案 0 :(得分:2)
如果我正确地遵循代码的逻辑(这完全不确定),你已经将全局变量声明为:
<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
答案 1 :(得分:0)
XSLT中的变量是命名值,它们不是可以在不同时间放置不同值的内存位置。这是声明性和程序性编程之间的根本区别。
如果您想解释您尝试解决的问题(即转换的输入和输出),那么我确信我们可以解释如何在XSLT中编写它。将解决方案从完全错误的方法逆向设计到解决方案是不可能的。