0

I'm currently trying to get my code to call in an xml file and an xsl - then perform the transformation and output multiple outcome files depending on the xml content.

import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;

public class TestTransformation {

public static void main(String[] args) throws TransformerException {

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

    Source xslt = new StreamSource(new File("transformer.xslt"));

    Transformer transformer = tFactory.newTransformer(xslt);

    Source xmlText = new StreamSource(new File("data.xml"));

    transformer.transform(xmlText, new StreamResult(new File("output.xml")));

But i want the transform to produce multiple output files.. Any ideas would be greatly appreciated!!

1
  • How do you decide what the output file should be? It might help to post a sample of the XSLT and the XML files Commented May 31, 2015 at 21:19

2 Answers 2

2

i want the transform to produce multiple output files.

You do that in the XSLT stylesheet itself: http://www.w3.org/TR/xslt20/#result-trees

This is assuming you are indeed using an XSLT 2.0 processor. In XSLT 1.0, you can use an EXSLT extension: http://exslt.org/exsl/elements/document/index.html instead - provided your processor supports it.

Sign up to request clarification or add additional context in comments.

Comments

0

You should use <xsl:result-document/> to produce multiple files (note that it's only available in version 2.0 of XSL). You also don't need to specify output file.

The following code shows how to deal with multiple output files:

public class TestTransformation {

    public static void main(String[] args) throws TransformerException {
        TransformerFactory tFactory = TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl", TestTransformation.class.getClassLoader());
        Source xslt = new StreamSource(new File("transformer.xslt"));
        Transformer transformer = tFactory.newTransformer(xslt);
        Source xmlText = new StreamSource(new File("data.xml"));
        transformer.transform(xmlText, new DOMResult());
    }
}

data.xml:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="hello.xsl"?>
<p>
    <p1>Hello world!</p1>
    <p2>Hello world!</p2>
</p>

transformer.xml:

<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="p1">
        <xsl:result-document href="foo.xml"/>
    </xsl:template>
    <xsl:template match="p2">
        <xsl:result-document href="bar.xml"/>
    </xsl:template>
</xsl:stylesheet>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.