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.
Call the
TransformerFactory.newInstance( )
factory method to create a newTransformerFactory
object.Construct a
Source
object from the XSLT stylesheet.Pass this
Source
object to theTransformerFactory
object’snewTransform( )
method to create aTransform
object.Construct a
Source
object from the input XML document you wish to transform.Construct a
Result
object into which the transformed XML document will be output.Pass the
Source
and theResult
to theTransform
object’stransform( )
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 ...
Get XML in a Nutshell, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.