XSLT 2.0 如何跨各种循环和结构执行计数器和变量

发布于 2024-09-10 16:05:26 字数 899 浏览 4 评论 0原文

所以,我知道你不能访问范围之外的变量,它们是不可变的,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 方法或类似的东西,然后您可以拥有全局可变值,但是......我真的不想像那样“作弊”。

欢迎任何指点、想法等。

So, I know you can't access variables outside scope, that they're immutable, that XSLT is functional not imperative, etc...

But I need a general purpose approach to something that would be trivial with global mutable variables (that sounds evil just saying it :). Here's an example...

<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>

And the challenge is there could be any number of templates like t1 and t2 at any time during processing and templates that both modify and use the variable.

Maybe part of the problem is that the values depends on the order of processing, but that's deliberate--that's what is required.

One possibility I thought of was to pass the values everywhere as parameters. But the problem is that one leaf template might need to change it, and then processing goes back up... it loses that change. Unless there is 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.

I have seen this done using extensions--calling out to Java methods or something like that and then you can have global mutable values, but... I'd really rather not "cheat" like that.

Any pointers, ideas, etc., welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

话少心凉 2024-09-17 16:05:26

我认为答案已包含在我的问题和其他评论中。这里引用其中的一些内容:

“模板以某种方式返回参数,然后我可以传递这些返回的参数?考虑一种通用的纯函数式编程语言,似乎这就是人们可以做到这一点的方式——递归调用,但是使用返回值进行进一步调用,以便可以“继承”这些值。”

来自 user357812:“我认为也许这可以通过最细粒度的树遍历模式和隧道参数来完成。只有通过输入和输出,才能准确说明如何完成。”

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."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文