Passing a XML File (InputStream) to XSLT avoid using Document in XSLT javax.xml



I am new to XML, XSLT and javax.xml


Currently my objective is to merge two XML files using XSLT Version 1.0 and everything works fine.


But I feel that there is a limitation in my code and I would like to get rid of it, if possible.


These are my resources: 'file1.xml' 'file2.xml' 'merge.xslt'


This is my merge method:



public ByteArrayOutputStream merge(final InputStream file1) {
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer;
try {
transformer = tFactory.newTransformer(new StreamSource("merge.xslt"));
transformer.transform(new StreamSource(file1), new StreamResult(outputStream));
} catch (final TransformerConfigurationException e) {
LOG.warn("Problem occurred transforming files configuration issue", e);
} catch (final TransformerException e) {
LOG.warn("Problem occurred transforming files", e);
}
return outputStream;
}


This is how I am passing file2.xml inside the XSLT



<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://ift.tt/tCZ8VR">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:param name="lookup" select="document('/file2.xml')"/>

<xsl:template match="/">
Do the processing how I want
</xsl:template>


What I want to achieve is that, I would like to modify my merge method to pass file1.xml and file2.xml.



public ByteArrayOutputStream merge(final InputStream file1,final InputStream file2)


And I want to somehow pass this InputStream file2 to the XSLT, so that the limitation of reading the file from file system is eliminated.


Can someone guide me if this is possible and how to achieve it, I would really appreciate all the help.


Thank you.


No comments:

Post a Comment