将 xml 节点/文档/片段作为参数传递给 xslt
我尝试将 w3c.dom.Document
、Element
和 NodeList
作为参数传递给 xslt 转换。
我希望能够在 xslt 中处理它:
<xsl:param name="links" />
<xsl:template match="/">
<record>
<xsl:for-each select="$links/*">
<test />
</xsl:for-each>
</record>
</xsl:template>
我将参数传递为:
Document params = createLinksParams(links);
transformer.setParameter("links", params);
我收到此异常:
“从‘com.sun.org.apache.xerces.internal.dom.DocumentImpl’到‘节点集’的转换无效。”
我还尝试了 exslt:node-set()
、xalan:nodeset()
等,但它不起作用。
看起来 xalan 在内部除了他自己的 Node.js 实现之外。
我怎样才能做类似的事情而不遇到这个问题?
我无法使用 document($param)
因为我是动态构建文档的。
I tried to pass a w3c.dom.Document
, Element
and NodeList
as parameters to a xslt transform.
I want to be able to process it within the xslt:
<xsl:param name="links" />
<xsl:template match="/">
<record>
<xsl:for-each select="$links/*">
<test />
</xsl:for-each>
</record>
</xsl:template>
I pass the parameter as:
Document params = createLinksParams(links);
transformer.setParameter("links", params);
I get this exception:
'Invalid conversion from 'com.sun.org.apache.xerces.internal.dom.DocumentImpl' to 'node-set'.'
I tried also exslt:node-set()
, xalan:nodeset()
etc, but it doesn't work.
It seems that internally xalan excepts his own implementation of the Node.
How can I do something similar without incurring in this problem?
I cannot use document($param)
because I construct the doc on the fly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
(发布一个新答案,因为前一个答案没有解决问题,而这个新答案与前一个完全不同)
似乎是 XALAN 编译处理器的一个已知问题( XALANJ-2057,
如何将节点作为参数传递给 XSLTC 处理器的 translet )。
那么,有哪些替代方案呢?
对 如何传递节点的响应
作为 XSLTC 转换的参数
处理器
XALAN 编译处理器(XSLTC),
使用 XALAN 解释处理器。或任何其他支持此类行为的 XSLT 处理器。
相反,也在对 如何通过节点
作为 XSLTC 转换的参数
处理器后 - 但不确定它是否会工作。
(Posting a new answer, as the previous one did not solve the issue and this new one is radically different from the previous)
Seems to be a known issue with XALAN compiling processor ( XALANJ-2057,
How can I pass a node as parameter to translets for XSLTC Processor).
So, what are the alternatives?
a response to How can I pass a node
as parameter to translets for XSLTC
Processor post
XALAN compiling processor (XSLTC),
use XALAN interpretive processor. Or any other XSLT processor that supports such behavior.
instead, also outlined in a response to How can I pass a node
as parameter to translets for XSLTC
Processor post - not sure if it will work, though.
我找到了一个解决方案(此处:使用Java进行XSLT处理:传递xml参数中的内容),这也可能适合您的情况:
无需从字符串创建 uriresolver,只需从字符串读取器创建流源并将其传递到样式表即可。
之后,我能够以 XML 形式正常访问文档:
没有使用 xalan 进行测试,但也许答案会帮助其他偶然发现这个问题的人:)
I found a solution (here: XSLT Processing with Java : passing xml content in parameter) which may work for your case as well:
instead of creating a uriresolver from a string, just create a stream source from a string reader and pass it to the stylesheet.
After that, I was able to access the doc normally as XML:
Did not test with xalan, but maybe the answer will help others who stumble onto this question :)
这是 URIResolver Gambit 的一个工作示例,解决方案列表中的#1:
我还有一个工作版本,我将 xml 源放入 URI 中,
但我认为这可能会在某处遇到长度限制。
Here's a working example with the URIResolver Gambit, #1 in the list of solutions:
I also had a working version where I put the xml source in the URI like
but I figured that might run into length limits somewhere.
如果您查看文档JavaDoc,可以看到它扩展了 Node 接口,但不是 节点列表。不确定它是否有效,但您可以尝试传入 params.getChildNodes() 而不是 params。
If you look at the Document JavaDoc, you can see that it extends Node interface, but not NodeList. Not sure if it will work, but you could try to pass in params.getChildNodes() instead of params.
不管怎样,这都是一种烦恼。最后,我总是发现最简单且与其他 XSLT 处理器最兼容的方法是将 XML 片段序列化到临时文件,将该文件的 URI 作为字符串参数传递给 XSLT,然后通过 XPath 文档加载 URI( ) 功能。
It's an annoyance, one way or the other. In the end, I've always found it easiest and most compatible with other XSLT processors to serialize the XML fragment to a temp file, pass that file's URI to XSLT as a string parameter, and from there load the URI via the XPath document() function.