使用变量选择元素
基本上我有一个看起来像这样的小模板:
<xsl:template name="templt">
<xsl:param name="filter" />
<xsl:variable name="numOrders" select="count(ORDERS/ORDER[$filter])" />
</xsl:template>
我试图使用它来调用
<xsl:call-template name="templt">
<xsl:with-param name="filter" select="PRICE < 15" />
</xsl:call-template>
它 不幸的是,它似乎在调用模板之前对其进行评估(因此有效地传递了“false”)将其括在引号中只会使其成为字符串字面意义,所以这也不起作用。 有谁知道我想要实现的目标是否可能? 如果是这样,你能解释一下吗? 干杯
Basically I have a small template that looks like:
<xsl:template name="templt">
<xsl:param name="filter" />
<xsl:variable name="numOrders" select="count(ORDERS/ORDER[$filter])" />
</xsl:template>
And I'm trying to call it using
<xsl:call-template name="templt">
<xsl:with-param name="filter" select="PRICE < 15" />
</xsl:call-template>
Unfortunately it seems to evaluate it before the template is called (So effectively "false" is being passed in) Enclosing it in quotes only makes it a string literal so that doesn't work either. Does anybody know if what I'm trying to achive is possible? If so could you shed some light on it? Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下面怎么样:
如果您仍然只想使用单个参数,您可以首先在模板“templt”中进行标记。
how about the following:
If you still want to use a single parameter only, you could tokenize in template 'templt' first.
Divo 的回答很好。
但是,它将任何最终过滤限制为指定子项的名称和值。
很高兴知道可以传递一个(相当于一个)函数作为参数。 这个非常强大的概念在 FXSL(XSLT 函数式编程库)中实现。 FXSL 完全是用 XSLT 本身编写的。
这是使用 过滤器函数/模板。 我们将过滤器作为参数传递给执行过滤的模板。 过滤器可以是任何代码/逻辑。 在这种特殊情况下,我们将一个引用作为参数传递给一个检查数字是否为偶数的模板。 完整的转换仅输出那些值为偶数的“num”元素。
我们可以很容易地通过任何其他过滤器,使用完全相同的技术:过滤(不)偶数、平方、质数等。
请注意,这个过滤器不会'不必自己编写“过滤器”模板——它是一次性编写的,并且由 FXSL 库提供。 因此,您通常只需使用即可。 指令导入“过滤器”模板和 FXSL 已提供的许多其他有用的函数/模板。
以下转换:
当应用于此源 XML 文档时:
产生所需的(已过滤的)结果,仅包含具有偶数值的节点:
有关 XSLT 中函数式编程的更多信息,请访问 FXSL 页面 和库本身可以从其 sourceforce 项目。
回到具体问题:
此转换:
当应用于此源 XML 文档时:
产生所需的结果:
Divo's answer is a good one.
However, it restricts any eventual filtering to specifying a child's name and value.
It is good to know that one can pass a (what amounts to a) function as a parameter. This very powerful concept is implemented in FXSL -- the Functional Programming Library for XSLT. FXSL is written entirely in XSLT itself.
Here is an appropriate example using the filter function/template. We are passing the filter as a parameter to a template that performs the filtering. The filter can be any code/logic. In this particular case we are passing as parameter a reference to a template that checks if a number is even. The complete transformation outputs only those "num" elements, whose value is an even number.
We could very easily pass any other filter, using exactly the same technique: to filter (un)even numbers, squares, prime numbers, ... etc.
Do note, that one doesn't have to write himself the "filter" template -- it is written once and forever and provided by the FXSL library. As result you typically just uses the <xsl:import/> directive to import the "filter" template and many other useful functions/templates already provided by FXSL.
The transformation below:
when applied on this source XML document:
produces the desired (filtered) result, containing only the nodes with even values:
More information about Functional Programming in XSLT can be found on the page of FXSL and the library itself can be downloaded from its sourceforce project.
To return to the concrete problem:
This transformation:
when applied on this source XML document:
produces the wanted result:
使用 EXSLT 库,特别是 dyn:evaluate 函数,它可以评估作为 XPath 表达式的字符串。
Use the EXSLT library, specifically the dyn:evaluate function, which can evaluate a string as an XPath expression.