I have a set of xml files (each has a different structure and their number can vary) which I need to combine to form one xml file.
I generated an xslt file (used MapForce) which takes multiple xml files as input . Following snippet shows the input to the xslt file -
<xsl:param name="input" select="()"/>
<xsl:for-each select="$input/@multiplier">
<xsl:attribute name="multiplier" select="fn:string(.)"/>
</xsl:for-each>
<xsl:sequence select="fn:string($input)"/>
I am using s9API to transform my input to the required output xml file -
InputStream stylesheetInputStream = new FileInputStream("xsltFile.xslt");
StreamSource input = new StreamSource(new FileInputStream("input.xml"));
StreamResult output = new StreamResult(new FileOutputStream("output.xml"));
TransformerFactory factory = net.sf.saxon.TransformerFactoryImpl.newInstance();
Transformer t = factory.newTransformer(new StreamSource(stylesheetInputStream));
t.setParameter("input", new File[]{new File("source_1.xml"), new File("source_2.xml"), new File("source_3.xml")});
t.transform(input, output);
I keep getting an exception and for some reason, it is actually the output file that's mentioned in the error which is quite strange (and this file is empty, of course) -
Error on line 1 column 1 of output.xml:
SXXP0003: Error reported by XML parser: Premature end of file.
; SystemID: ; Line#: 90; Column#: -1
net.sf.saxon.trans.XPathException: org.xml.sax.SAXParseException: Premature end of file.
at net.sf.saxon.event.Sender.sendSAXSource(Sender.java:425)
at net.sf.saxon.event.Sender.send(Sender.java:178)
at net.sf.saxon.Configuration.buildDocument(Configuration.java:3516)
at net.sf.saxon.lib.StandardCollectionURIResolver$FileExpander.map(StandardCollectionURIResolver.java:441)
at net.sf.saxon.lib.StandardCollectionURIResolver$FileExpander.map(StandardCollectionURIResolver.java:321)
at net.sf.saxon.expr.MappingIterator.next(MappingIterator.java:56)
at net.sf.saxon.expr.ItemMappingIterator.next(ItemMappingIterator.java:83)
at net.sf.saxon.expr.ContextMappingIterator.next(ContextMappingIterator.java:52)
at net.sf.saxon.value.SequenceExtent.<init>(SequenceExtent.java:105)
at net.sf.saxon.expr.sort.DocumentOrderIterator.<init>(DocumentOrderIterator.java:31)
at net.sf.saxon.expr.sort.DocumentSorter.iterate(DocumentSorter.java:101)
at net.sf.saxon.expr.instruct.ForEach.processLeavingTail(ForEach.java:414)
at net.sf.saxon.expr.instruct.Block.processLeavingTail(Block.java:615)
at net.sf.saxon.expr.instruct.Instruction.process(Instruction.java:131)
at net.sf.saxon.expr.instruct.ElementCreator.processLeavingTail(ElementCreator.java:301)
at net.sf.saxon.expr.instruct.ElementCreator.processLeavingTail(ElementCreator.java:254)
at net.sf.saxon.expr.instruct.Instruction.process(Instruction.java:131)
at net.sf.saxon.expr.instruct.ElementCreator.processLeavingTail(ElementCreator.java:301)
at net.sf.saxon.expr.instruct.ElementCreator.processLeavingTail(ElementCreator.java:254)
at net.sf.saxon.expr.instruct.Block.processLeavingTail(Block.java:615)
at net.sf.saxon.expr.instruct.Instruction.process(Instruction.java:131)
at net.sf.saxon.expr.instruct.ElementCreator.processLeavingTail(ElementCreator.java:301)
at net.sf.saxon.expr.instruct.ElementCreator.processLeavingTail(ElementCreator.java:254)
at net.sf.saxon.expr.LetExpression.processLeavingTail(LetExpression.java:586)
at net.sf.saxon.expr.instruct.Template.applyLeavingTail(Template.java:212)
at net.sf.saxon.trans.Mode.applyTemplates(Mode.java:1034)
at net.sf.saxon.Controller.transformDocument(Controller.java:1957)
at net.sf.saxon.Controller.transform(Controller.java:1803)
at com.test.Transform.performTransform(Transform.java:33)
at com.test.Transform.main(Transform.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)
does my xslt file specify input correctly? If not, how to specify multiple files as input? Or am I incorrectly setting the parameters in my code?
I found a related query here - Transformation of multiple input files
One of the post mentions using <xsl:param name="f2" as="document-node()"/> but how to specify multiple files?
Update: I am unable to post an answer so just editing my question -
Finally figured it out.....
I use MapForce and am not terribly familiar with xslt so I can't explain the expression below too much but conceptually I did this -
I used a comma separated string (containing list of files) as input which is then tokenized.
<xsl:param name="listOfFiles" as="xs:string" required="yes"/>
<xsl:for-each select="tokenize($listOfFiles, replace(',', '(\.|\$|\^|\{|\[|\(|\||\)|\*|\+|\?|\\)', '\\$1'))">
for my java code -
t.setParameter("listOfFiles", "file1.xml,file2.xml");