public class XsltCompiler extends Object
To construct an XsltCompiler, use the factory method Processor.newXsltCompiler()
on the Processor object.
An XsltCompiler may be used repeatedly to compile multiple queries. Any changes made to the XsltCompiler (that is, to the static context) do not affect queries that have already been compiled. An XsltCompiler may be used concurrently in multiple threads, but it should not then be modified once initialized.
Modifier | Constructor and Description |
---|---|
protected |
XsltCompiler(Processor processor)
Protected constructor.
|
Modifier and Type | Method and Description |
---|---|
XsltExecutable |
compile(Source source)
Compile a stylesheet.
|
XsltPackage |
compilePackage(Source source)
Compile a library package.
|
Iterable<XsltPackage> |
compilePackages(Iterable<Source> sources)
Compile a list of packages.
|
void |
declareCollation(String uri,
Collator collation)
Deprecated.
since 9.6. Collations are now held globally. If this method is called, the effect
is to update the pool of collations held globally by the Processor.
|
void |
declareDefaultCollation(String uri)
Declare the default collation
|
Source |
getAssociatedStylesheet(Source source,
String media,
String title,
String charset)
Get the stylesheet associated
via the xml-stylesheet processing instruction (see
http://www.w3.org/TR/xml-stylesheet/) with the document
document specified in the source parameter, and that match
the given criteria.
|
ErrorListener |
getErrorListener()
Get the ErrorListener being used during this compilation episode
|
Processor |
getProcessor()
Get the Processor from which this XsltCompiler was constructed
|
CompilerInfo |
getUnderlyingCompilerInfo()
Get the underlying CompilerInfo object, which provides more detailed (but less stable) control
over some compilation options
|
URIResolver |
getURIResolver()
Get the URIResolver to be used during stylesheet compilation.
|
String |
getXsltLanguageVersion()
Get the XSLT (and XPath) language level supported by the processor.
|
void |
importPackage(XsltPackage thePackage)
Import a library package.
|
boolean |
isCompileWithTracing()
Ask whether trace hooks are included in the compiled code.
|
boolean |
isGenerateByteCode()
Ask whether bytecode is to be generated in the compiled code.
|
boolean |
isSchemaAware()
Ask whether schema-awareness has been requested by means of a call on
setSchemaAware(boolean) |
void |
setCompileWithTracing(boolean option)
Set whether trace hooks are to be included in the compiled code.
|
void |
setErrorList(List<StaticError> errorList)
Supply a List object which will be populated with information about any static errors
encountered during the transformation.
|
void |
setErrorListener(ErrorListener listener)
Set the ErrorListener to be used during this compilation episode
|
void |
setGenerateByteCode(boolean option)
Set whether bytecode should be generated for the compiled stylesheet.
|
void |
setParameter(QName name,
XdmValue value)
Set the value of a stylesheet parameter.
|
void |
setSchemaAware(boolean schemaAware)
Say that the stylesheet must be compiled to be schema-aware, even if it contains no
xsl:import-schema declarations.
|
void |
setURIResolver(URIResolver resolver)
Set the URIResolver to be used during stylesheet compilation.
|
void |
setXsltLanguageVersion(String version)
Set the XSLT (and XPath) language level to be supported by the processor.
|
protected XsltCompiler(Processor processor)
Processor.newXsltCompiler()
.processor
- the Saxon processorpublic Processor getProcessor()
public void setURIResolver(URIResolver resolver)
Source
representing the
location where a stylesheet module can be found.
This URIResolver is used to dereference the URIs appearing in xsl:import
,
xsl:include
, and xsl:import-schema
declarations. It is not used
for resolving the URI supplied for the main stylesheet module (as supplied to the
compile(javax.xml.transform.Source)
or compilePackage(javax.xml.transform.Source)
methods.
It is not used at run-time for resolving requests to the document()
or similar functions.
resolver
- the URIResolver to be used during stylesheet compilation.public void setParameter(QName name, XdmValue value)
name
- the StructuredQName of the parametervalue
- as a XdmValue of the parameterpublic URIResolver getURIResolver()
public void setErrorListener(ErrorListener listener)
listener
- The error listener to be used. This is notified of all errors detected during the
compilation.public ErrorListener getErrorListener()
public void setSchemaAware(boolean schemaAware)
schemaAware
- If true, the stylesheet will be compiled with schema-awareness
enabled even if it contains no xsl:import-schema declarations. If false, the stylesheet
is treated as schema-aware only if it contains one or more xsl:import-schema declarations.public boolean isSchemaAware()
setSchemaAware(boolean)
public void declareCollation(String uri, Collator collation)
uri
- the absolute collation URIcollation
- a Collator
object that implements the required collationIllegalArgumentException
- if an attempt is made to rebind the standard URI
for the Unicode codepoint collationpublic void declareDefaultCollation(String uri)
uri
- the absolute URI of the default collation. This URI must have been bound to a collation
using the method declareCollation(String, java.text.Collator)
, or it must be a
collation that is recognized implicitly, such as a UCA collationIllegalStateException
- if the collation URI has not been registered, unless it is the standard
Unicode codepoint collation which is registered implicitlypublic void setXsltLanguageVersion(String version)
version
- the language level to be supported. The value 2.0 requests a processor conforming to the
XSLT 2.0 specification; 3.0 requests an XSLT 3.0 processor, while the value "0.0" (which is the
default setting in the absence of a call on this method) gets a processor according to the
value of the version attribute on the xsl:stylesheet element.
Although this interface can be used to run a 1.0 stylesheet, it is not possible to request a 1.0 processor; Saxon will handle it as a 2.0 processor in backwards-compatibility mode, which is not quite the same thing.
Note that Saxon-HE does not support XSLT 3.0. However, requesting 3.0 via this interface does not cause an error; an error only occurs if the stylesheet actually uses XSLT 3.0 features.
IllegalArgumentException
- if the value is not numerically equal to 0.0, 2.0, or 3.0public String getXsltLanguageVersion()
setXsltLanguageVersion(java.lang.String)
represents a processor
selected according to the value of the version attribute on the xsl:stylesheet element.public void setCompileWithTracing(boolean option)
option
- true if trace code is to be compiled in, false otherwisepublic boolean isCompileWithTracing()
public void setGenerateByteCode(boolean option)
option
- true if bytecode is to be generated, false otherwisepublic boolean isGenerateByteCode()
public Source getAssociatedStylesheet(Source source, String media, String title, String charset) throws SaxonApiException
The returned Source will have an absolute URI, created by resolving
any relative URI against the base URI of the supplied source document,
and redirected if necessary by using the URIResolver associated with this
XsltCompiler
.
source
- The XML source document. Note that if the source document
is available as an instance of XdmNode
, a corresponding Source
can be obtained using the method XdmNode.asSource()
.
If the source is a StreamSource or SAXSource, it will be read only as far as the
xml-stylesheet processing instruction (but the Source will be consumed and must not
be re-used).media
- The media attribute to be matched. May be null, in which
case the prefered templates will be used (i.e. alternate = no).title
- The value of the title attribute to match. May be null.charset
- The value of the charset attribute to match. May be null.compile(javax.xml.transform.Source)
.SaxonApiException
- if any problems occur, including the case where no matching
xml-stylesheet processing instruction is found.public XsltPackage compilePackage(Source source) throws SaxonApiException
The source argument identifies an XML file containing an <xsl:package> element. Any packages
on which this package depends must have been made available to the XsltCompiler
by importing them using importPackage(net.sf.saxon.s9api.XsltPackage)
.
source
- identifies an XML document holding the the XSLT package to be compiledXsltCompiler
; if the package is required
for use in subsequent compilations then it must be explicitly imported.SaxonApiException
- if the source cannot be read or if static errors are found during the
compilation. Any such errors will have been notified to the registered ErrorListener
if there is one, or reported on the System.err
output stream otherwise.public Iterable<XsltPackage> compilePackages(Iterable<Source> sources) throws SaxonApiException, XPathException
sources
- the collection of packages to be compiled, in the form of an IterableSaxonApiException
- if the source cannot be read or if static errors are found during the
compilation. Any such errors will have been notified to the registered ErrorListener
if there is one, or reported on the System.err
output stream otherwise.XPathException
public void importPackage(XsltPackage thePackage) throws SaxonApiException
xsl:use-package
declaration of subsequent compilations performed using this
XsltCompiler
.thePackage
- the package to be importedSaxonApiException
- if the imported package was created under a different Processor
public XsltExecutable compile(Source source) throws SaxonApiException
Note: the term "compile" here indicates that the stylesheet is converted into an executable form. There is no implication that this involves code generation.
The source argument identifies the XML document holding the principal stylesheet module. Other modules will be located relative to this module by resolving against the base URI that is defined as the systemId property of the supplied Source.
The following kinds of Source
are recognized:
StreamSource
, allowing the stylesheet to be supplied as a
URI, as a File
, as an InputStream
, or as a Reader
SAXSource
, allowing the stylesheet to be supplied as a stream
of SAX events from a SAX2-compliant XML parser (or any other source of SAX events)DOMSource
, allowing the stylesheet to be supplied as a
DOM tree. This option is available only if saxon9-dom.jar is on the classpath.XdmNode
representing the document node of the stylesheet modulesource
- Source object representing the principal stylesheet module to be compiledSaxonApiException
- if the stylesheet contains static errors or if it cannot be read. Note that
the exception that is thrown will not contain details of the actual errors found in the stylesheet. These
will instead be notified to the registered ErrorListener. The default ErrorListener displays error messages
on the standard error output.public CompilerInfo getUnderlyingCompilerInfo()
public void setErrorList(List<StaticError> errorList)
errorList
- a List (typically empty) to which information will be appended about
static errors found during the compilation. Each such error is represented by a
StaticError
object.Copyright (c) 2004-2014 Saxonica Limited. All rights reserved.