简体   繁体   中英

Transforming multiple XML/XHTML/HTML file with XSLT using Java Program

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. Eg 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 {
            st.transform(inXML,inXSL,outTXT);
            } catch(TransformerConfigurationException e) {
            System.err.println("Invalid factory configuration");
            System.err.println(e);
            } catch(TransformerException e) {
            System.err.println("Error during transformation");
            System.err.println(e);
        }
    }
    public void transform(String inXML,String inXSL,String outTXT)
    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);
        transformer.transform(in,out);
        System.out.println("The generated XML file is:" + outTXT);
    }
}
class MyErrorListener implements ErrorListener {
    public void warning(TransformerException e)
    throws TransformerException {
        show("Warning",e);
        throw(e);
    }
    public void error(TransformerException e)
    throws TransformerException {
        show("Error",e);
        throw(e);
    }
    public void fatalError(TransformerException e)
    throws TransformerException {
        show("Fatal Error",e);
        throw(e);
    }
    private void show(String type,TransformerException e) {
        System.out.println(type + ": " + e.getMessage());
        if(e.getLocationAsString() != null)
        System.out.println(e.getLocationAsString());
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM