SAXONICA

Saxon Documentation
Full Contents
About Saxon
Changes in this Release
Conditions of Use
Using XSLT 2.0
| Using XSLT 2.0 Stylesheets
| Running XSLT from the Command Line
» | Compiling a Stylesheet
| Choosing a Tree Model
| Invoking XSLT from Java
| Performance Analysis
Using XQuery
Handling Source Documents
XML Schema Processing
The XPath API
Standards Conformance
Extensibility
Saxon Extensions
Sample Saxon Applications
The Saxon SQL Extension
XSLT Elements
XPath 2.0 Expression Syntax
Function Library
XSLT Patterns

# 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 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:

- Stylesheet compilation is not currently supported for schema-aware stylesheets, specifically, for stylesheets that contain an `<xsl:import-schema>` declaration.

- Stylesheet compilation is not currently supported for stylesheets that make calls on the extension functions `saxon:evaluate()` or `saxon:expression()`, because these require run-time access to information that is not preserved by compilation.

- Stylesheets that use a `<saxon:collation>` declaration cannot be compiled.

- Stylesheets that use a `<saxon:import-query>` declaration cannot be compiled.

- A compiled stylesheet can be executed only by the same Saxon release as was used to compile it.

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