XSLT 2.0 函数可以返回任意类型吗?
我正在尝试编写一个 XSLT 2.0 函数,该函数返回特定类型的结果——比方说一个或多个元素。这是我尝试过的,但无济于事:
<xsl:function name="util:find-parents2" as="element(parent)*">
<xsl:variable name="output" as="element(parent)*">
<xsl:for-each select="('one','two')">
<parent>
<xsl:sequence select="."/>
</parent>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="$output"/>
</xsl:function>
这是我从 Saxon 处理器得到的错误:
Error at xsl:function on line 192 column 65 of file:/e:/mlsh/recursive.xsl:
XTTE0780: Required item type of result of function util:find-parents2() is element(parent,
xs:anyType); supplied value has item type text() Failed to compile stylesheet. 1 error detected.
但我期望得到这样的结果:
<parent>one</parent>
<parent>two</parent>
我在这里缺少什么?我以为我已经为 $output 指定了适当的类型(一个或多个
元素),但处理器显然没有收到消息并且只看到文本。如何在此处返回
元素列表?提前致谢...
更新:
总结一下,标题中问题的答案是“是”。原始示例中的关键是
和
之间的区别。用后者替换前者是获得所需行为的一种方法。另一种方法是从
元素中“解开”内容,这会使事情变得更整洁:
<xsl:function name="util:find-parents2" as="element(parent)*">
<xsl:for-each select="('one','two')">
<parent>
<xsl:sequence select="."/>
</parent>
</xsl:for-each>
</xsl:function>
感谢 Michael 和 Jim 发现了我最初的疏忽......
I am trying to write an XSLT 2.0 function that returns a result of a specific type--let's say one or more elements. Here's what I've tried, to no avail:
<xsl:function name="util:find-parents2" as="element(parent)*">
<xsl:variable name="output" as="element(parent)*">
<xsl:for-each select="('one','two')">
<parent>
<xsl:sequence select="."/>
</parent>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="$output"/>
</xsl:function>
Here's the error I get from the Saxon processor:
Error at xsl:function on line 192 column 65 of file:/e:/mlsh/recursive.xsl:
XTTE0780: Required item type of result of function util:find-parents2() is element(parent,
xs:anyType); supplied value has item type text() Failed to compile stylesheet. 1 error detected.
But I expected to get something like this:
<parent>one</parent>
<parent>two</parent>
What am I missing here? I thought I had specified the appropriate type for $output (one or more <parent>
elements), but the processor clearly isn't getting the message and is seeing only text. How can I return a list of <parent>
elements here? Thanks in advance...
UPDATE:
Summarizing, the answer to the question in the title is 'yes'. The key in the original example is the difference between <xsl:value-of>
and <xsl:sequence>
. Replacing the former with the latter is one way to get the desired behavior. Another way would be to 'unwrap' the contents from the <xsl:variable>
element, which makes things a bit tidier:
<xsl:function name="util:find-parents2" as="element(parent)*">
<xsl:for-each select="('one','two')">
<parent>
<xsl:sequence select="."/>
</parent>
</xsl:for-each>
</xsl:function>
Thanks to Michael and Jim for catching my original oversight...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题在于您应该删除最后的
。value-of
生成一个文本节点,这不是您想要的。序列构造函数已经包含您想要输出的节点。I believe the problem is that you should remove the final
<xsl:value-of...>
.value-of
produces a text node, which is not what you want. The sequence constructor already contains the nodes you want output.