How to pass an XML document as a parameter to an XSL transform in java (oracle xdk)?

若如初见. 提交于 2019-12-11 05:26:02

问题


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?


回答1:


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...



来源:https://stackoverflow.com/questions/2001548/how-to-pass-an-xml-document-as-a-parameter-to-an-xsl-transform-in-java-oracle-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!