Hi all,
It took me a while to find the right way of doing this, and I thought I would share it since it has saved me quite a lot of time and effort. To transform XML to HTML using XSL using a
servlet is surprisingly simple. (You need xalan.jar in your path):
(1) import org.apache.xalan.xslt.*;
(2) In your service method:
String sheet = "myStyleSheet.xsl";
String
doc = "myXML.xml";
XSLTProcessor processor = XSLTProcessorFactory.getProcessor();
processor.process(new XSLTInputSource(new FileInputStream(doc)), new XSLTInputSource(new FileInputStream(sheet)), new XSLTResultTarget(res.getOutputStream()));
I have left out the try/catch block to save space. This example processes a XML file and stylesheet which are both files, but you can just as easily process using InputStreams from a URL, or some other source. In my application, I call a URL of another servelt on the same server, which returns back a dynamic XML document each time, based un URL parameters.
Have fun!
Bill