创建节点集并作为参数传递
使用 XSLT 1.0,我试图本质上创建一个小节点集,然后将其作为参数传递给模板,如下所示:
<xsl:call-template name="widget">
<xsl:with-param name="flags">
<items>
<item>widget.recent-posts.trim-length=100</item>
<item>widget.recent-posts.how-many=3</item>
<item>widget.recent-posts.show-excerpt</item>
</items>
</xsl:with-param>
</xsl:call-template>
这个想法是,然后从 widget
模板中我可以写一些类似的内容:
<xsl:value-of select="$flags/item[1]" />
显然我遇到了编译错误..我怎样才能实现这种事情?
Using XSLT 1.0, I'm trying to essentially create a small node set and then pass it as a parameter to a template, something like the following:
<xsl:call-template name="widget">
<xsl:with-param name="flags">
<items>
<item>widget.recent-posts.trim-length=100</item>
<item>widget.recent-posts.how-many=3</item>
<item>widget.recent-posts.show-excerpt</item>
</items>
</xsl:with-param>
</xsl:call-template>
The idea is that then from within the widget
template I could write something like:
<xsl:value-of select="$flags/item[1]" />
Obviously I get compile errors.. how can I achieve this sort of thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
XSLT 1.0 中有一种方法(非标准)动态创建临时树并在其上计算 XPath 表达式,但这需要使用
xxx:node-set()< /code> 函数
。
每当在
xsl:variable
或xsl:param
主体内动态创建节点时,该xsl:variable
的类型/xsl:param
是 RTF(结果树片段)和 W3 XSLT 1.0 规范。严格限制可以根据 RTF 计算的 XPath 表达式的类型。作为一种解决方法,几乎每个 XSLT 1.0 供应商都有自己的
xxx:node-set()
扩展函数,该函数接受 RTF 并从中生成正常的节点集。对于不同的供应商,
xxx
前缀(或您选择的任何其他前缀)绑定的命名空间是不同的。对于 MSXML 和两个 .NET XSLT 处理器,它是:“urn:schemas-microsoft-com:xslt”
。 EXSLT 库使用命名空间:"http://exslt.org/common"
。此命名空间 EXSLT 在许多 XSLT 1.0 处理器上实现,如果可能,建议使用其xxx:node-set()
扩展。这是一个简单的示例:
There is a way (non-standard) in XSLT 1.0 to create temporary trees dynamically and evaluate XPath expressions on them, however this requires using the
xxx:node-set()
function.Whenever nodes are dynamically created inside the body of an
xsl:variable
or anxsl:param
, the type of thatxsl:variable
/xsl:param
is RTF (Result Tree Fragment) and the W3 XSLT 1.0 Spec. limits severyly the kind of XPath expressions that can be evaluated against an RTF.As a workaround, almost every XSLT 1.0 vendor has their own
xxx:node-set()
extension function that takes an RTF and produces a normal node-set from it.The namespace to which the
xxx
prefix (or any other prefix you choose) is bound is different for different vendors. For MSXML and the two .NET XSLT processor it is:"urn:schemas-microsoft-com:xslt"
. The EXSLT library uses the namespace:"http://exslt.org/common"
. This namespace EXSLT is implemented on many XSLT 1.0 processors and it is recommended to use itsxxx:node-set()
extension, if possible.Here is a quick example:
好吧,我设法通过以下方式解决了这个问题:
首先将自定义命名空间添加到样式表中,例如
xmlns:myns="http://my.ns.com"
然后在以下位置定义节点集样式表的顶部:
然后按以下方式引用:
这可行,但对我来说在
标记本身内定义节点集仍然是理想的,正如我给出的第一个例子......有人认为这是可能的吗?Well, I managed to get around this in the following way:
First add a custom namespace to your stylesheet, e.g.
xmlns:myns="http://my.ns.com"
Then define the nodeset at the top of the stylesheet:
Then reference in the following way:
This works, but it would still be ideal for me to define the node-set within the
<xsl:with-param>
tag itself, as in the first example I gave.. anyone think that would be possible?