1

所以,我知道你不能访问范围之外的变量,它们是不可变的,XSLT 是功能性的而不是命令性的,等等......

但是我需要一种通用的方法来处理全局可变变量(这听起来很邪恶:)。这是一个例子......

<xsl:template match="t1">
  <xsl:if test="someLogic">
    <!-- I know, can't do this but just to explain... -->
    <xsl:variable name="varName">numberOrText, maybe even some arithmetic like $varName+1</xsl:variable>
  </xsl:if>
</xsl:template>

<xsl:template match="t2">
  <xsl:value-of select="$varName"/>
</xsl:template>

挑战在于,在处理过程中的任何时候都可能存在任意数量的模板,例如 t1 和 t2,以及修改和使用变量的模板。

也许问题的一部分是值取决于处理顺序,但这是故意的——这就是所需要的。

我想到的一种可能性是将值作为参数传递到任何地方。但问题是一个叶子模板可能需要更改它,然后处理会重新开始......它会丢失该更改。除非模板有某种方法可以返回参数,然后我可以传递这些返回的参数?考虑一种通用的纯函数式编程语言,似乎人们可能会这样做——递归调用,但使用返回值进行进一步调用,这样人们就可以“继承”这些值。

我已经看到使用扩展来完成这项工作——调用 Java 方法或类似的东西,然后你可以拥有全局可变值,但是......我真的不想那样“作弊”。

欢迎任何指针,想法等。

4

1 回答 1

0

I think the answer was included in my question and other comments. Here's quoting some of it:

"some way for a template to return parameters and then I could pass those returned parameters on? Thinking of a general purpose pure functional programming language, it seems like that's how one might do this--calling recursively, but using the return values for further calls so one can "carry forward" the values."

From user357812: "I think that maybe this can be done with a most fine-grained tree traversal pattern and tunnel param. Only with input and output it's posible to say exactly how."

于 2011-10-24T17:41:32.590 回答