# 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).

Stylesheet compilation is not currently supported for schema-aware stylesheets.

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.

Stylesheet compilation remains a little fragile. It has proved difficult to test it comprehensively. One known restriction is that stylesheets containing `saxon:collation` declarations cannot be compiled (because it uses Java classes that are not serializable). There may be other restrictions: please let me know if you find any.

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.

Expand | Next