如何将 XML 文档作为参数传递给 java (oracle xdk) 中的 XSL 转换?

发布于 2024-08-17 03:29:58 字数 349 浏览 7 评论 0原文

我正在尝试将 XML 文档作为参数传递给 XSLT 样式表。我相信代码正在使用 oracle XDK 进行转换(它使用 JDK 1.4.2 和 Spring,而且我是代码库的新手,所以我不确定最终加载的是什么)。在我的第一次尝试中,我只是创建了一个文档对象并将其设置为变压器上的参数,但尝试将变量复制到树中却没有结果。我想到的问题是:

  1. 这在 XSLT 转换器的一般情况下是否可能? (看起来应该如此,因为通常 XSLT 变量/参数可以包含节点集)

  2. 是否可以专门使用 oracle XDK(或 xalan,它也在类路径中)?

  3. 如果是这样,我该如何让它发挥作用?

I'm trying to pass an XML document to an XSLT stylesheet as a parameter. I believe the code is using the oracle XDK for transformations (it's using JDK 1.4.2, and Spring, and I'm new to the codebase, so I'm not sure what is getting loaded in the end). In my first attempt, I just created a document object and set this as the parameter on the transformer, but attempts to copy the variable into the tree give no result. Questions that come to mind are:

  1. is this even possible in the general case of XSLT transformers? (it seems like it should be, as generally XSLT variables/parameters can contain nodesets)

  2. is it possible specifically with the oracle XDK (or xalan, which is also in the classpath)?

  3. If so, how do I make it work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

你的心境我的脸 2024-08-24 03:29:58

答案是这是可能的,但是,它是不直观的,至少对于 Oracle XSL 处理器来说是这样。我尝试了以下(无效)调用(名称已更改以保护无辜者):

Document x = createDocumentForMe();
transformer.addParameter("param",x);

以及

Document x = createDocumentForMe();
transformer.addParameter("param",new DOMSource(x));

(第二个是基于 DOMSource 可能会工作,因为它是 DOM 的 java.xml.transform 接口)。最终对我有用的调用是了解 XSL 使用 XPath,变量的有效类型本质上是字符串或节点集,并且 XPath 返回节点集。以下内容对我有用:

Document x = createDocumentForMe();
XPathExpression xpe = XPathFactory.newInstance().newXPath().compile("/");
transformer.addParameter("param",xpe.evaluate(x, XPathConstants.NODESET));

它基本上使用 XPath 来获取仅包含传入 DOM 对象的根文档的节点集。然而,这看起来有点像黑客,并且可能不适用于其他 XSL 处理器,所以 YMMV...

The answer is that this is possible, however, it is non-intuitive, at least for the Oracle XSL processor. I tried the following (non-working) invocations (names changed to protect the innocent):

Document x = createDocumentForMe();
transformer.addParameter("param",x);

and

Document x = createDocumentForMe();
transformer.addParameter("param",new DOMSource(x));

(this second on the basis that maybe DOMSource would work because it was the java.xml.transform interface to the DOM). The invocation that worked for me in the end was to take the insight that XSL uses XPath, and the valid types for the variable are essentially strings or nodesets, and XPath returns nodesets. The following works for me:

Document x = createDocumentForMe();
XPathExpression xpe = XPathFactory.newInstance().newXPath().compile("/");
transformer.addParameter("param",xpe.evaluate(x, XPathConstants.NODESET));

Which basically uses XPath to get a nodeset containing only the root document of the passed in DOM object. However, this seems like a bit of a hack, and may not work with other XSL processors, so YMMV...

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