nu.xom.xslt
Class XSLTransform

java.lang.Object
  extended bynu.xom.xslt.XSLTransform

public final class XSLTransform
extends java.lang.Object

This class serves as an interface to a TrAX aware XSLT processor such as Xalan or Saxon.

The following example shows how to apply an XSL Transformation to a XOM document and get the transformation result in the form of a XOM Nodes:

 public static Nodes transform(Document in) 
   throws XSLException {
     XSLTransform stylesheet = new XSLTransform("mystylesheet.xsl");
     return stylesheet.transform(doc);
 } 

XOM relies on TrAX to perform the transformation. The javax.xml.transform.TransformerFactory Java system property determines which XSLT engine TrAX uses. Its value should be the fully qualified name of the implementation of the abstract javax.xml.transform.TransformerFactory class. Values of this property for popular XSLT processors include:

This property can be set in all the usual ways a Java system property can be set. TrAX picks from them in this order:

  1. Invoking System.setProperty( "javax.xml.transform.TransformerFactory", "classname")
  2. The value specified at the command line using the -Djavax.xml.transform.TransformerFactory=classname option to the java interpreter
  3. The class named in the lib/jaxp.properties properties file in the JRE directory, in a line like this one:
    javax.xml.parsers.DocumentBuilderFactory=classname
  4. The class named in the META-INF/services/javax.xml.transform.TransformerFactory file in the JAR archives available to the runtime
  5. Finally, if all of the above options fail, a default implementation is chosen. In Sun's JDK 1.4.0 and 1.4.1, this is Xalan 2.2d10. In Java 1.4.2, this is Xalan 2.4.

Version:
1.0d25
Author:
Elliotte Rusty Harold

Constructor Summary
XSLTransform(Document stylesheet)
           Creates a new XSLTransform by reading the stylesheet from the specified Document.
XSLTransform(java.io.File stylesheet)
           Creates a new XSLTransform by reading the stylesheet from the specified File.
XSLTransform(java.io.InputStream stylesheet)
           Creates a new XSLTransform by reading the stylesheet from the specified InputStream.
XSLTransform(java.io.Reader stylesheet)
           Creates a new XSLTransform by reading the stylesheet from the specified Reader.
XSLTransform(java.lang.String systemID)
           Creates a new XSLTransform by reading the stylesheet from the specified URL.
 
Method Summary
 void setNodeFactory(NodeFactory factory)
           Sets the factory used to construct nodes in the output tree.
static Document toDocument(Nodes nodes)
           Builds a Document object from a Nodes object.
 java.lang.String toString()
           Returns a string form of this XSLTransform, suitable for debugging.
 Nodes transform(Document in)
           Creates a new Nodes from the input Document by applying this object's stylesheet.
 Nodes transform(Nodes in)
           Creates a new Nodes from the input Nodes by applying this object's stylesheet.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

XSLTransform

public XSLTransform(java.io.InputStream stylesheet)
             throws XSLException

Creates a new XSLTransform by reading the stylesheet from the specified InputStream.

Parameters:
stylesheet - input stream from which the stylesheet is read
Throws:
XSLException - when an IOException, format error, or something else prevents the stylesheet from being compiled

XSLTransform

public XSLTransform(java.io.Reader stylesheet)
             throws XSLException

Creates a new XSLTransform by reading the stylesheet from the specified Reader.

Parameters:
stylesheet - character stream from which the stylesheet is read
Throws:
XSLException - when an IOException, format error, or something else prevents the stylesheet from being compiled

XSLTransform

public XSLTransform(java.io.File stylesheet)
             throws XSLException

Creates a new XSLTransform by reading the stylesheet from the specified File.

Parameters:
stylesheet - file from which the stylesheet is read
Throws:
XSLException - when an IOException, format error, or something else prevents the stylesheet from being compiled

XSLTransform

public XSLTransform(Document stylesheet)
             throws XSLException

Creates a new XSLTransform by reading the stylesheet from the specified Document.

Parameters:
stylesheet - Document containing the stylesheet
Throws:
XSLException - when the supplied Document is not syntactically correct XSLT

XSLTransform

public XSLTransform(java.lang.String systemID)
             throws XSLException

Creates a new XSLTransform by reading the stylesheet from the specified URL.

Parameters:
systemID - URL from which the stylesheet is read
Throws:
XSLException - when an IOException, format error, or something else prevents the stylesheet from being compiled
Method Detail

transform

public Nodes transform(Document in)
                throws XSLException

Creates a new Nodes from the input Document by applying this object's stylesheet. The original Document is not changed.

Parameters:
in - document to transform
Returns:
a Nodes containing the result of the transformation
Throws:
XSLException - if the transformation fails, normally due to an XSLT error

transform

public Nodes transform(Nodes in)
                throws XSLException

Creates a new Nodes from the input Nodes by applying this object's stylesheet. The original Nodes is not changed.

Parameters:
in - Document to transform
Returns:
a Nodes containing the result of the transformation
Throws:
XSLException - if the transformation fails, normally due to an XSLT error

setNodeFactory

public void setNodeFactory(NodeFactory factory)

Sets the factory used to construct nodes in the output tree. Passing null uses the default factory.

Parameters:
factory - the NodeFactory used to construct nodes in the output tree

toDocument

public static Document toDocument(Nodes nodes)

Builds a Document object from a Nodes object. This is useful when the stylesheet is known to produce a well-formed document with a single root element and no text or attribute nodes. If the stylesheet produces anything else, an XMLException is thrown.

Parameters:
nodes - the nodes to be placed in the new document
Returns:
a document containing the nodes
Throws:
XMLException - if nodes does not contain exactly one element or if it contains any text nodes or attributes

toString

public java.lang.String toString()

Returns a string form of this XSLTransform, suitable for debugging.

Returns:
debugging string


Copyright 2002-2004 Elliotte Rusty Harold
elharo@metalab.unc.edu