SAXON home page

Using Stylesheets


This page describes how to use SAXON XSLT Stylesheets, either from the command line, or from the Java API.

Contents
Using Stylesheets Reference
Using Saxon from the Command Line
Embedding Saxon in an Application
See also:
XSL Elements
XSL Expressions
XSL Patterns
SAXON Extensibility
SAXON XSL Conformance

Using Saxon from the Command Line

The Java class com.icl.saxon.StyleSheet has a main program that may be used to apply a given style sheet to a given source XML document. The form of command is:

java  com.icl.saxon.StyleSheet   [options]   source-document   stylesheet   [ params…]

The options must come first, then the two file names, then the params. The stylesheet is omitted if the -a option is present.

The file saxon.jar must be on your classpath. If you are running under JDK 1.2 or 1.3, the classpath must also include the file saxon-xml-apis.jar.

If you are using JDK 1.4 or later, and if your stylesheet does not use extensions functions, you can use the simpler form of command:

java  -jar dir/saxon.jar   [options]   source-document   stylesheet   [ params…]

The options are as follows (in any order):

-a Use the xml-stylesheet processing instruction in the source document to identify the stylesheet to be used. The stylesheet argument should be omitted.
-ds | -dt Selects the implementation of the internal tree model. -dt selects the "tinytree" model (the default). -ds selects the traditional tree model. See Choosing a tree model below.
-l Switches line numbering on for the source document. Line numbers are accessible through the extension function saxon:line-number(), or from a trace listener.
-m classname Use the specified Emitter to process the output from xsl:message. The class must implement the com.icl.saxon.output.Emitter class. This interface is similar to a SAX ContentHandler, it takes a stream of events to generate output. In general the content of a message is an XML fragment. By default the standard XML emitter is used, configured to write to the standard error stream, and to include no XML declaration. Each message is output as a new document.
-noext Suppress calls on extension functions, other than system-supplied Saxon and EXSLT extension functions. This option is useful when loading an untrusted stylesheet, perhaps from a remote site using an http:// URL; it ensures that the stylesheet cannot call Java methods and thereby gain privileged access to resources on your machine.
-o filename Send output to named file. In the absence of this option, output goes to standard output. If the source argument identifies a directory, this option is mandatory and must also identify a directory; on completion it will contain one output file for each file in the source directory.
-r classname Use the specified URIResolver to process all URIs. The URIResolver is a user-defined class, that extends the com.icl.saxon.URIResolver class, whose function is to take a URI supplied as a string, and return a SAX InputSource. It is invoked to process URIs used in the document() function, in the xsl:include and xsl:import elements, and (if -u is also specified) to process the URIs of the source file and stylesheet file provided on the command line.
-t Display version and timing information to the standard error output
-T Display stylesheet tracing information to the standard error output. Also switches line numbering on for the source document.
-TL classname Run the stylesheet using the specified TraceListener. The classname names a user-defined class, which must implement com.icl.saxon.trace.TraceListener
-u Indicates that the names of the source document and the style document are URLs; otherwise they are taken as filenames, unless they start with "http:" or "file:", in which case they are taken as URLs
-w0, w1, or w2 Indicates the policy for handling recoverable errors in the stylesheet: w0 means recover silently, w1 means recover after writing a warning message to the system error output, w2 means signal the error and do not attempt recovery. (Note, this does not currently apply to all errors that the XSLT recommendation describes as recoverable). The default is w1.
-x classname Use specified SAX parser for source file and any files loaded using the document() function. The parser must be the fully-qualified class name of a Java class that implements the org.xml.sax.Parser or org.xml.sax.XMLReader interface
-y classname Use specified SAX parser for stylesheet file, including any loaded using xsl:include or xsl:import. The parser must be the fully-qualified class name of a Java class that implements the org.xml.sax.Parser or org.xml.sax.XMLReader interface
-?Display command syntax
source-document Identifies the source file or directory. Mandatory. If this is a directory, all the files in the directory will be processed individually. In this case the -o option is mandatory, and must also identify a directory, to contain the corresponding output files. A directory must be specified as a filename, not as a URL.
stylesheet Identifies the stylesheet. Mandatory unless the -a option is used.

A param takes the form name=value, name being the name of the parameter, and value the value of the parameter. These parameters are accessible within the stylesheet as normal variables, using the $name syntax, provided they are declared using a top-level xsl:param element. If there is no such declaration, the supplied parameter value is silently ignored.

Under Windows it is possible to supply a value containing spaces by enclosing it in double quotes, for example name="John Smith". This is a feature of the operating system shell, not something Saxon does, so it may not work the same way under every operating system.

If the -a option is used, the name of the stylesheet is omitted. The source document must contain a <?xml-stylesheet?> processing instruction before the first element start tag; this processing instruction must have a pseudo-attribute href that identifies the relative or absolute URL of the stylesheet document, and a pseudo-attribute type whose value is "text/xml", "application/xml", or "text/xsl". For example:

<?xml-stylesheet type="text/xsl" href="../style3.xsl" ?>

It is also possible to refer to a stylesheet embedded within the source document, provided it has an id attribute and the id attribute is declared in the DTD as being of type ID. For example:

<?xml-stylesheet type="text/xsl" href="#style1" ?> <!DOCTYPE BOOKLIST SYSTEM "books.dtd" <!ATTLIST xsl:transform id ID #IMPLIED> > <BOOKLIST> ... <xsl:transform id="style1" version="1.0" xmlns:xsl="..."> ... </xsl:transform> </BOOKLIST>

Choosing a tree model

Saxon provides two implementations of the internal tree data structure (or tree model). The tree model can be chosen by an option on the command line (-dt for the tiny tree, -ds for the standard tree) or from the Java API. The default is to use the tiny tree model. The choice should make no difference to the results of a transformation (except the order of attributes and namespace declarations) but only affects performance.

Generally speaking, the tiny tree model is faster to build but slower to navigate. It therefore performs better when you visit each node on the tree once or less. The standard tree model may perform better (in rare cases, much better) when each node is visited many times, especially when you use the preceding or preceding-sibling axis.

The tiny tree model gives most benefit when you are processing a large document. It uses a lot less memory, so it can prevent thrashing when the size of document is such that the standard tree doesn't fit in real memory.

Choose the default tree model -dt unless your performance tests show that the transformation runs faster with -ds. (Use the -t option to display execution times.)


Embedding Saxon in an Application

Rather than using the XSLT processor from the command line, you may want to include it in your own application, perhaps one that enables it to be used within a servlet. If you run the processor repeatedly, this will always be much faster than running it each time from a command line. This is especially true if you use the same stylesheet repeatedly to transform different source documents.

Saxon incorporates support for the JAXP API (also known as TrAX). This is a standard API for invoking XSLT processors. Since JDK 1.4 it is included as a standard part of the JDK. Saxon 6.5.4 includes its own copy of the JAXP classes and interfaces in saxon-xml-apis.jar, so that it can be used with earlier JDK releases.

This API is described in the javadoc documentation included with the Saxon download. Look for the javax.xml.transform package. The documentation in JDK 1.4 is an improved version of this.

More information and examples relating to the JAXP API can be found in the example application found in the samples directory.

Michael H. Kay
Saxonica Limited
22 June 2005