Take the 2-minute tour ×

I want to take multiple input files (XML/HTML/XHTML) and corresponding XSLT to produce the output file for the corresponding Input file. If there is only one input XML file and one input XSLT file then from the following program I'm able to transform it successfully. E.g In the give program my input (X)HTML file is temp.html, input XSLT is temp.xsl which produces the output as temp_copy.html. What would be the best way if I have two or more input files temp1.html and temp2.html and corresponding XSLT temp1.xsl and temp2.xsl, then how can I generate the output temp1_copy.html and temp2_copy.html by using the corresponding input files? Thanking you!

My Current Java Code:

 public class SimpleXSLT {
    public static void main(String[] args) {

        String inXML = "C:/tmp/temp.html";
        String inXSL = "C:/tmp/temp.xsl";
        String outTXT = "C:/tmp/temp_copy.html";
        SimpleXSLT st = new SimpleXSLT();
        try {
            } catch(TransformerConfigurationException e) {
            System.err.println("Invalid factory configuration");
            } catch(TransformerException e) {
            System.err.println("Error during transformation");
    throws TransformerConfigurationException,
    TransformerException {
        TransformerFactory factory = TransformerFactory.newInstance();
        StreamSource xslStream = new StreamSource(inXSL);
        Transformer transformer = factory.newTransformer(xslStream);
        transformer.setErrorListener(new MyErrorListener());
        StreamSource in = new StreamSource(inXML);
        StreamResult out = new StreamResult(outTXT);
        System.out.println("The generated XML file is:" + outTXT);
class MyErrorListener implements ErrorListener {
    public void warning(TransformerException e)
    throws TransformerException {
    public void error(TransformerException e)
    throws TransformerException {
    public void fatalError(TransformerException e)
    throws TransformerException {
        show("Fatal Error",e);
    private void show(String type,TransformerException e) {
        System.out.println(type + ": " + e.getMessage());
        if(e.getLocationAsString() != null)
share|improve this question
Is this a strictly XSLT 1.0 question or are XSLT 2.0 solutions also acceptable? –  Dimitre Novatchev Aug 1 '12 at 1:27
@DimitreNovatchev Thanks for the reply. Well in my XSLT files declaration is like xsl:stylesheet version="1.0", So I think it should be XSLT 1.0 but when I changed it to xsl:stylesheet version="2.0" in one file then it didn't make any difference. I don't know in deep about XSLT so I'm not sure if XSLT 2.0 will work but still if you can then please provide me the solution, so that I could try. Thanking you! –  RahulD Aug 1 '12 at 5:17
@DimitreNovatchev Also this java program works perfectly with .xml files but when it comes to .html file (XHTML), then it becomes very slow and sometimes even connection gets timed-out. –  RahulD Aug 1 '12 at 5:20
add comment

1 Answer

up vote 1 down vote accepted

You're using the JAXP transformation API, also known as TrAX, so let's stick with that. If you moved to XSLT 2.0 and Saxon then using Saxon's s9api API would be better, but Saxon also supports TrAX so long as you don't need advanced 2.0 features.

In TrAX, the object representing a "compiled" stylesheet is the Templates object. So you use TransformerFactory.newTemplates() to compile your stylesheet, and then you can use this Templates object repeatedly to transform multiple source documents. Templates.newTransformer() gives you a transformer that you can use to perform the transformation. In fact you can use the transformer repeatedly (in series but not in parallel) to perform multiple transformations, but my recommendation is to use a new Transformer each time. Reusing the Templates is where you get the performance benefit; the Templates object is also thread-safe.

As for 1.0 versus 2.0. For many simple transformations 1.0 is quite adequate, but you will soon find yourself doing things such a grouping or string manipulation that are far easier to accomplish in 2.0. Since there's a widely used open-source XSLT 2.0 processor available for the Java platform (namely Saxon), with a compatible API, there's really very little reason to be using XSLT 1.0 these days.

A lot of people wrongly assume that the version attribute on the stylesheet is used somehow to select an XSLT 1.0 or 2.0 processor. That isn't usually the case (except possibly in some IDEs such as XML Spy). Rather, it's there to inform the processor what version of the XSLT specification the author was writing to. If you say version="1.0" and select a 2.0 processor, then it will run in a kind of "quirks" mode that tries to be maximally compatible with XSLT 1.0, for example it will use floating point arithmetic even when you are adding two integers. Conversely, when you say version="2.0" and select a 1.0 processor, the processor will attempt to take fallback action when it encounters language constructs it doesn't understand, rather than rejecting these as compile-time errors.

share|improve this answer
Thanks for the reply. Now, I'm trying as you have suggested. –  RahulD Aug 3 '12 at 20:32
add comment

Your Answer


