Thursday, January 04, 2007

A problem with XSL transformations in Java 1.5

If you are like me, you probably tried to use javax.xml.transform.dom.DOMSource as your XSLT source, and your XML source.

If you are like me - you spent hours and hours, trying to figure out why your XML wasn't being transformed.

I don't know why. But you HAVE to use javax.xml.transform.stream.StreamSource.

Making the code:
// get the files
File xsdFile = new File("xsd/xml.xml");
File xslFile = new File("xsd/xsl.xsl");
// turn them into stream sources
StreamSource xsltSource = new StreamSource(xslFile);
StreamSource xsdSource = new StreamSource(xsdFile);
// get transformation stuff
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(xsltSource);
// set up for sending the output to screen
StreamResult scrResult = new StreamResult(System.out);
// transform
transformer.transform(xsdSource, scrResult);

No comments: