创建节点集并作为参数传递

发布于 2024-09-26 19:28:50 字数 600 浏览 0 评论 0原文

使用 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 技术交流群。

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

发布评论

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

评论(2

能怎样 2024-10-03 19:28:50

XSLT 1.0 中有一种方法(非标准)动态创建临时树并在其上计算 XPath 表达式,但这需要使用 xxx:node-set()< /code> 函数

每当在 xsl:variablexsl: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() 扩展。

这是一个简单的示例

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:msxsl="urn:schemas-microsoft-com:xslt"
 xmlns:ext="http://exslt.org/common"
  exclude-result-prefixes="ext msxsl"
 >
 <xsl:template match="/">
  <xsl:variable name="vTempRTF">
   <a>
    <b/>
   </a>
  </xsl:variable>

  <xsl:copy-of select="ext:node-set($vTempRTF)/a/*"/>
 </xsl:template>
</xsl:stylesheet>

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 an xsl:param, the type of that xsl: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 its xxx:node-set() extension, if possible.

Here is a quick example:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:msxsl="urn:schemas-microsoft-com:xslt"
 xmlns:ext="http://exslt.org/common"
  exclude-result-prefixes="ext msxsl"
 >
 <xsl:template match="/">
  <xsl:variable name="vTempRTF">
   <a>
    <b/>
   </a>
  </xsl:variable>

  <xsl:copy-of select="ext:node-set($vTempRTF)/a/*"/>
 </xsl:template>
</xsl:stylesheet>
初见 2024-10-03 19:28:50

好吧,我设法通过以下方式解决了这个问题:

首先将自定义命名空间添加到样式表中,例如 xmlns:myns="http://my.ns.com"

然后在以下位置定义节点集样式表的顶部:

<myns:recent-posts-flags>
    <item>widget.recent-posts.trim-length=100</item>
    <item>widget.recent-posts.how-many=3</item>
    <item>widget.recent-posts.show-excerpt</item>
</myns:recent-posts-flags>

然后按以下方式引用:

<xsl:call-template name="widget">
    <xsl:with-param name="flags" select="document('')/*/myns:recent-posts-flags" />
</xsl:call-template>

这可行,但对我来说在 标记本身内定义节点集仍然是理想的,正如我给出的第一个例子......有人认为这是可能的吗?

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:

<myns:recent-posts-flags>
    <item>widget.recent-posts.trim-length=100</item>
    <item>widget.recent-posts.how-many=3</item>
    <item>widget.recent-posts.show-excerpt</item>
</myns:recent-posts-flags>

Then reference in the following way:

<xsl:call-template name="widget">
    <xsl:with-param name="flags" select="document('')/*/myns:recent-posts-flags" />
</xsl:call-template>

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?

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