Compiling a Stylesheet

Saxon allows a compiled stylesheet (a Templates object) to be saved to disk as a file. The transformation can then be run using this compiled stylesheet by using the -c option to the command java net.sf.saxon.Transform.

The facility to compile the stylesheet is available only under Saxon-EE, but the compiled stylesheet can be executed under Saxon-HE. The facility is not available on the .NET platform.

The actual compilation of the stylesheet can be achieved using the command java com.saxonica.CompileStylesheet. The format of the command is:

java  com.saxonica.CompileStylesheet   [options]   stylesheet   output   [ params...]

The options available are a subset of the options for running a transformation, described above. The relevant options are -t (give progress messages), -u (stylesheet argument is a URI, not a filename), -r (specify a URIResolver for use at compile time), -y (specify an XML parser for parsing the source stylesheet), -1.1 (enable XML 1.1)

There are some restrictions that apply to stylesheet compilation:

You can use any file extension for the compiled stylesheet, I generally use .sxx (Saxon XSLT executable). The file actually contains the Java serialization of a data structure that is then used at run-time to drive the transformation process. As an alternative to using the net.sf.saxon.Compile command from the command line, you can use the Java serialization API directly, to write the net.sf.saxon.Templates object to an ObjectOutputStream.

Running a compiled stylesheet is directly supported only from the command line. If you want to embed this capability in your own application, you can copy the relevant code from the source of the net.sf.saxon.Transform command. You need to be aware, however, that this code creates a new NamePool and modifies the Configuration to use it, which will disrupt any other applications sharing the same Configuration. It is the responsibility of the application to ensure that a compiled stylesheet runs in a dedicated Configuration, rather than sharing a Configuration object with other transformations.

The term "compile" is stretching a point. The executable that is produced does not contain machine instructions, or even interpreted Java bytecode. It contains instructions in the form of a data structure that Saxon itself can interpret. Note that the format of compiled stylesheets is unlikely to be stable from one Saxon release to the next.