Archive for the ‘xslt’ tag
XSL Transform using Java
import javax.xml.transform.Templates; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; ... // The XSL source: StreamSource xsl = new StreamSource(...); // Create the transformer: TransformerFactory factory = TransformerFactory.newInstance(); Templates template = factory.newTemplates(xsl); // can throw TransformerConfigurationException (parent exception is: TransformerException) Transformer transformer = template.newTransformer(); // can throw TransformerConfigurationException // The data XML source StreamSource data = new StreamSource(...); // The transformed output: StreamResult out = new StreamResult(...); // Transform using the Transformer instance: transformer.transform(data, out); // can throw TransformerException
Have a look at the StreamSource API and StreamResult API to understand the various ways by which it can be instantiated.
Linux command-line XSLT
To convert an XML based on a XSL, use this command:
$ xsltproc /path/to/xsl.xsl /path/to/xml.xml
The converted document will be written to STDOUT. To write to a particular file, you may use the -o parameter:
$ xsltproc /path/to/xsl.xsl /path/to/xml.xml -o out.html
Check the info/man pages for additional information.