I would like to pass a parameter containing XML content when processing XSLT. Here is my code:
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
File xmlFile = new File(xmlFilePath);
File xsltFile = new File(xslFilePath);
Source xmlSource = new StreamSource(xmlFile);
Result result = new StreamResult(System.out);
TransformerFactory transFact = TransformerFactory.newInstance();
Transformer trans = transFact.newTransformer(xsltSource);
trans.setParameter("foo", "<bar>Hello1</bar><bar>Hello2</bar>");
trans.transform(xmlSource, result);
Then I'd like to select the values contained in the 'bar' tag in my XSL file.
<xsl:param name="foo"/>
...
<xsl:value-of select="$foo//foo[1]" />
But this doesn't work, I get this error message:
org.apache.xpath.objects.XString cannot be cast to org.apache.xpath.objects.XNodeSet
So I guess I should pass an XML object to my setParameter method, but which one? I can't find a simple example how to create an XNodeSet object...
How can I do that? Thanks.