TrAX

Unfortunately, there is no standard API for XSLT that works across languages and engines: each vendor provides its own unique API. The closest thing to a standard XSLT API is the Transformations API for XML (TrAX), included in JAXP. However, this is limited to Java and is not even supported by all Java-based XSLT engines. Nonetheless, since it is the closest thing to a standard there is, we will discuss it here.

Code that transforms an XML document using an XSLT stylesheet through TrAX follows these six steps. All of the classes mentioned are in the javax.xml.transform package, a standard part of Java 1.4 and later and a separately installable option in earlier versions.

  1. Call the TransformerFactory.newInstance( ) factory method to create a new TransformerFactory object.

  2. Construct a Source object from the XSLT stylesheet.

  3. Pass this Source object to the TransformerFactory object's newTransform( ) method to create a Transform object.

  4. Construct a Source object from the input XML document you wish to transform.

  5. Construct a Result object into which the transformed XML document will be output.

  6. Pass the Source and the Result to the Transform object's transform( ) method.

The source can be built from a DOM Document object, a SAX InputSource, or an InputStream—represented by the javax.xml.transform.dom.DOMSource, javax.xml.transform.sax.SAXSource, and javax.xml.transform.stream.StreamSource classes, respectively. The result of the transform can be a DOM Document object, a SAX ContentHandler, or an OutputStream. These are represented by the javax.xml.transform.dom.DOMResult, javax.xml.transform.sax.SAXResult, and javax.xml.transform.stream.StreamResult classes, respectively.

For example, this code fragment uses the XSLT stylesheet found at http://www.cafeconleche.org/books/xian/examples/08/8-8.xsl to transform the file people.xml in the current working directory onto System.out:

TransformerFactory factory = TransformerFactory.newInstance( );
URL u = new URL(
 "http://www.cafeconleche.org/books/xian/examples/08/8-8.xsl");
InputStream in     = u.openStream( );
Source stylesheet  = new StreamSource(in);
Transformer xform  = factory.newTransformer(stylesheet);
InputStream people = new FileInputStream("people.xml");
Source original    = new StreamSource(people);
Result transformed = new StreamResult(System.out);
xform.transform(original, transformed);

The procedure is much the same when the source or result is a DOM Document object or a SAX event stream. Just use the DOMSource, SAXSource, DOMResult, and/or SAXResult classes as appropriate. For example, this code fragment transforms the DOM Document object doc according to the stylesheet at http://www.cafeconleche.org/books/xian/examples/08/8-8.xsl and passes the result through the SAX ContentHandler object named handler:

Document doc;
// Build the doc object in the usual way...
TransformerFactory factory = TransformerFactory.newInstance( );
URL u = new URL(
 "http://www.cafeconleche.org/books/xian/examples/08/8-8.xsl");
InputStream in     = u.openStream( );
Source stylesheet  = new StreamSource(in);
Transformer xform  = factory.newTransformer(stylesheet);
ContentHandler handler = new XMLCounter( ); // From Chapter 19
Source original    = new DOMSource(doc);
Result transformed = new SAXResult(handler);
xform.transform(original, transformed);