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 {
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());
}
}