XSLT transformations from a Java application

Rather than running XSLT transformations from the command line, you may want to invoke transformations from your own application, perhaps one that runs within a web-based application server. If you use Saxon repeatedly to perform multiple transformations, this will always be much faster than running it each time from a command line, because of the overheads of starting up the Java Virtual Machine.

There are two APIs you can use to invoke Saxon's XSLT processor from an application:

  • The JAXP interface is a standard interface that also works with other Java-based XSLT processors, but has not been upgraded to support new language features in XSLT 2.0 and 3.0, which restricts its capability.
  • The s9api interface is particular to Saxon, but offers full access to Saxon's capabilities, including integrating XSLT transformation with other XML processing tasks.

These are described in the following sections.

The JAXP interface

SaxonJ incorporates support for the standard JAXP transformation API (originally known as TrAX). This is compatible with the API for invoking other XSLT processors such as Xalan and Oracle.

Although the JAXP interface is convenient, it has several significant drawbacks:

  1. It was defined for XSLT 1.0, and does not directly expose the new features exposed by XSLT 2.0 and 3.0, such as the ability to use a named template as the entry point to a transformation.
  2. The interface is not integrated with other XML capabilities, for example schema processing, making it cumbersome to combine XSLT processing with other tasks.
  3. The mechanism for locating an XSLT processor is slow and unreliable; applications that were tested to work with one XSLT processor can find themselves executing with another, which can cause bugs if the code is not fully portable.
  4. It does not take advantage of modern Java language features such as generics and streams.
  5. It is biased towards use of DOM as the tree model; while Saxon supports DOM, it is much slower (sometimes by a factor of 10) than other tree models.

This API is described in the documentation provided with JDK 1.5 and later. It is available online at http://docs.oracle.com/javase/8/docs/api/. Look for the javax.xml.transform package.

For more information on using this API to invoke the Saxon XSLT processor, see Using JAXP for transformations.

The s9api interface (Java)

An alternative is to use Saxon's own s9api interface. This is designed to provide an integrated approach to XML processing across the different range of languages supported by Saxon; unlike JAXP, it includes support for XSLT 2.0 and 3.0 capabilities, and it also takes advantage of Java generics, thus giving a more strongly typed and therefore more robust interface.

Saxon 9.9 introduced enhancements to the s9api interface to take advantage of Java 8 streams and functions, providing a native Java interface for navigating XML trees that is comparable with XPath in its power and expressiveness, while being much better integrated with the Java host language.

Saxon 10 introduced further enhancements to provide programmatic tree construction (see Building XML trees programmatically).

For more information on using this API to invoke the Saxon XSLT processor, see Using s9api for transformations.