indiWiz.com

Subhash's Tech Log

Archive for the ‘xslt’ tag

XSL Transform using Java

without comments

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.

Written by Subhash Chandran

September 14th, 2009 at 4:56 pm

Posted in Java

Tagged with , , , ,

Linux command-line XSLT

without comments

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.

Written by Subhash Chandran

February 2nd, 2009 at 5:28 pm

Posted in Linux

Tagged with , ,