Saxonica.com

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.

This facility is not available on the .NET platform.

The actual compilation of the stylesheet can be achieved using the command java net.sf.saxon.Compile. The format of the command is:

java  net.sf.saxon.Compile   [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.

To run a compiled stylesheet from the Java API rather than from the command line, use the call:

sheet = PreparedStylesheet.loadCompiledStylesheet(config, styleFileName)

Note that the config argument identifies the run-time configuration. The NamePool used by this configuration will be changed to be a copy of the NamePool that was created at compile time and saved in the compiled stylesheet. Therefore, this method requires the run-time transformation to have its own exclusive 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.

Next