amuru
amuru

Reputation: 11

XSLT 2 (Saxon): How to read multiple files into memory

How do I read multiple xml files into memory/stream?

Using <xsl:result-document> I am able to split xml into multiple xmls onto the directory. I want to read the multiple result files into memory

XSL :

<xsl:template match="/testdata">
            <xsl:for-each select="trd">
            <xsl:result-document href="result_{position()}.xml">
                <abc>
                    <xyz>
                        <xsl:copy-of select="*"/>
                    </xyz>
                </abc>
            </xsl:result-document>
            </xsl:for-each>
        
    </xsl:template>

With below I am able to read one resulting xml into memory (after removing <xsl:result-document>). I want read multiple output xmls into memory

 System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
            TransformerFactory tFactory = TransformerFactory.newInstance();

            Source xslt = new StreamSource(new File("testxsl.xsl"));

            Transformer transformer = null;

            transformer = tFactory.newTransformer(xslt);

            Source xmlInput = new StreamSource(new File("test.xml"));
            StreamResult standardResult = new StreamResult(new ByteArrayOutputStream());
            transformer.transform(xmlInput, standardResult);

Upvotes: 0

Views: 283

Answers (1)

Michael Kay
Michael Kay

Reputation: 163458

This can't be done using the standard JAXP API (which was designed for XSLT 1.0 and has never been upgraded). Use Saxon's s9api API, and call Xslt30Transformer.setResultDocumentHandler() to supply a destination for result documents. This can be an XdmDestination if you want the result as an XdmNode object, or it can be a Serializer writing to an in-memory OutputStream or StringWriter if you want to capture serialized results in memory.

Upvotes: 1

Related Questions