XML technologies / XSLT / XSLT Cookbook / Extending and embedding XSLT / XSLT in Java: usage

(Excerpt from “XSLT Kochbuch” by Sal Mangano)

Problem

You want to invoke XSLT processing from a Java application.

Solution

There are three ways to invoke XSLT from Java:

  • the native interface of your preferred Java-based XSLT implementation,

  • the more portable TrAX API, or the interface API

  • of JAXP 1.2 or 1.3, a superset of TrAX.

Familiarity with a particular Java-based XSLT implementation may tempt you to use its API directly, but this makes the code nonportable.

An alternative is TrAX (Transformation API for XML), an initiative initially sponsored by Apache.org. This quotation from the TrAX site explains its philosophy:

“The Java community will benefit greatly from a common API that lets developers understand and use one model, write code against consistent interfaces, and apply transformations polymorphically. TrAX seeks a clean, generic model that still meets common application needs across many domains.”

TrAX entered JAXP 1.1, later 1.2 and 1.3 for J2SE 5.0. Java-XSLT interfaces are therefore now either portable or nonportable. Neither is simply right or wrong: processors have special features that may be needed, and portability may not matter. This section nevertheless covers only the portable JAXP 1.2 API.

This example implements a simple JAXP 1.1 command-line processor, borrowed from Java und XSLT by Eric M. Burke (O’Reilly, 2002):

public class Transform
{

  public static void main(String[  ] args) throws Exception
  {
    if (args.length != 2)
    {
      System.err.println(
        "Usage: java Transform [xmlfile] [xsltfile]");
      System.exit(1);
    }

    // Öffne Quell- und Stylesheet-Datei
    File xmlFile = new File(args[0]);
    File xsltFile = new File(args[1]);

    // JAXP benutzt eine Source-Schnittstelle, um Daten zu lesen
    Source xmlSource = new StreamSource(xmlFile);
    Source xsltSource = new StreamSource(xsltFile);
    // Factory-Klassen ermöglichen es, dass der genaue XSLT-Prozessor vor der Anwendung versteckt bleibt, indem eine Standard-Transformer-Schnittstelle zurückgegeben wird
    TransformerFactory transFact = TransformerFactory.newInstance( );
    Transformer trans = transFact.newTransformer(xsltSource);

    // Wende das Stylesheet auf das Quelldokument an
    trans.transform(xmlSource, new StreamResult(System.out));
  }
}

Besides a StreamResult, a DOMResult can retain the result as a DOM tree for further processing, or a SAXResult can receive results as events.

For DOM, the result may be a DOM Document, DocumentFragment or Element, depending on the node type passed to the DOMResult constructor.

For SAXResult, pass a custom ContentHandler to the SaxResult constructor, which also receives the actual SAX events. Recall that a SAX ContentHandler receives callbacks for events such as startDocument( ), startElement( ), characters( ), endElement( ) and endDocument( ). See saxproject.org for more about SAX.

Discussion

The benefit of calling XSLT from Java is not writing your own processor front end as above, but adding transformation capabilities to Java’s already impressive abilities.

Imagine a Java server handling changing XML files from a database or XML arriving in SOAP messages. Backward compatibility may require several schema versions or SOAP clients, so multiple schemas must be handled transparently. Converting old schemas to new ones greatly simplifies server code.

With JAXP, transformer instances can be reused, so parse the stylesheet only once when the server loads it. If the server is multithreaded and every thread must transform, each needs its own instance for thread safety.

See also

The book Java und XSLT by Eric M. Burke (O’Reilly, 2002) thoroughly covers Java-XSLT integration, especially JAXP 1.1, with complete applications including discussion forums and WML applications (Wireless Markup Language).

  

 

 

 

back to top