3

I am setting the parameter like this:

Document doc_23 = createDocument(doc_bytes);
XPathExpression xpe = XPathFactory.newInstance().newXPath().compile("/");
transformer.setParameter("document23",xpe.evaluate(doc_23, XPathConstants.NODESET));

I also tried this:

transformer.setParameter("document23",new StreamSource(new StringReader(xml_text)));

In my xslt i am getting the variable like this:

<xsl:variable name="document23" select="/.."></xsl:variable>

And try to use it:

 <xsl:for-each select="$document23//Product">
                     <xsl:message>Test<xsl:value-of select="generalDetails/productCode"/></xsl:message>
 </xsl:for-each>

But i dont work (the for-each never get enterd).

The document have the elements speciffied beacasue using 'document(document23.xml)//Product' does work.

Thanks for the help.

2 Answers 2

1

You're using the DOM and JAXP APIs, which isn't an ideal way of using Saxon: the DOM is very slow with Saxon, and the JAXP XPath API is very weakly-typed so you need to have the interface specification and the Saxon-specific details both to hand in order to use it successfully. So my first recommendation would be, if you're committed to Saxon then you would be better off using the s9api API in preference.

In fact I don't understand why you are using XPath interfaces at all. You seem to be trying to run the XPath expression "/", which returns whatever you supply as the input. That's completely pointless.

If you do want to use the JAXP transformation API (and therefore setParameter()), the kind of things you can supply are described here:

http://www.saxonica.com/documentation/index.html#!using-xsl/embedding/jaxp-transformation

In particular see the paragraph that starts "The types of object that can be supplied as stylesheet parameters ..." This links to the "Extensibility" section, which tells you

"If the [...] value is an instance of javax.xml.transform.Source (other than a NodeInfo), a tree is built from the specified Source object, and the root node of this tree is returned as the result of the function."

So you can supply a StreamSource or a DOMSource to the setParameter() method as in your example.

If the path expression in an xsl:for-each appears to be selecting nothing, use xsl:message or xsl:copy-of to display the document you are trying to select into; this will often give you a clue what is wrong.

Sign up to request clarification or add additional context in comments.

Comments

1

I see one problem, mainly that you have used <xsl:variable name="document23" select="/.."></xsl:variable>, if you want to define an external parameter then you need to use <xsl:param name="document23" select="/.."/>, not xsl:variable.

4 Comments

Thanks, but after fixing it i still can get it to show the parameter
Start debugging it with <xsl:copy-of select="$document23"/>, that should output the complete document passed in, if you pass in a DOM node, as far as I understand it.
Yes, that is what i am trying but it wont print anything. i also confirmed that the document passed as parameter is not empty andd that it can queried using xpath
Edit your post with details of the Saxon version you use and a small but complete sample allowing others to reproduce the problem, then hopefully someone from Saxonica can tell you what is missing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.