Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top