I am using javax.xml.transform
API to do XSL transformation . The API only allows one XML document as an input to apply transformation as below .
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
StringWriter stringWriter = new StringWriter();
File xml = new File("C:\\abc");
File xsl = new File("C:\\def.xsl");
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(xml);
TransformerFactory transformerFactory =
TransformerFactory.newInstance();
StreamSource style = new StreamSource(xsl);
Transformer transformer = transformerFactory.newTransformer(style);
DOMSource source = new DOMSource(document);
Also , can pass simple String params as below , without any issue as below :
transformer.setParameter("mode", "CREATE");
But , i want to pass an XML Document as a parameter to the XSL file . I tried below code as suggested on one of SO pages , as below :
DocumentBuilder builder = factory.newDocumentBuilder();
final Document documentFile = builder.parse(xml2);
Map<String, Document> docs = new HashMap<String, Document>();
docs.put("lookup", documentFile);
transformer.setURIResolver(new DocumentURIResolver(docs));
And i set , the tag in XML to receive value as below :
<xsl:variable name="lookup" select="('documentFile')/> .
But its not working for me . Can anyone help me out with the correct pay to pass multiple XML documents to any XSL file via javax.xml.transform API ?
Update
Still stuck with the issue ,can any one let me how can i pass XML object into a XSLT 2.0 stylesheet as a param . I have tried different approaches but no luck still . I need to know a way out via JAVA xsl transform API .
java.xml.tansform
but tag the question as xslt-2.0? As you aware that XSLT since version 1.0 has the powerfuldocument
function that allows loading/acessing additional XML documents directly from within XSLT, even multiple in one step? – Racewaytransformer.setParameter("doc1", yourDocumentNode)
on the Java side and then with XSLT the global parameter<xsl:param name="doc1"/>
represents an XSLT/XPath document node e.g. you can use<xsl:copy-of select="$doc1"/>
or path expressions on it like<xsl:copy-of select="$doc1/root/foo/bar"/>
. – Raceway<xsl:variable name="lookup" select="('documentFile')/>
to<xsl:variable name="lookup" select="document('lookup')/>
? See my answer. – Krutz<xsl:variable name="lookup" select="('documentFile')/>
is not a correct xml element, so maybe this is your main issue. – Proparoxytone