public class XsltCompiler
extends java.lang.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 in principle be used concurrently in multiple threads, but in practice this is best avoided because all instances will share the same ErrorListener and it may therefore be difficult to establish which error messages are associated with each compilation.
Modifier | Constructor and Description |
---|---|
protected |
XsltCompiler(Processor processor)
Protected constructor.
|
Modifier and Type | Method and Description |
---|---|
java.lang.Iterable<XsltPackage> |
addCompilePackages(java.lang.Iterable<javax.xml.transform.Source> sources,
boolean link)
Deprecated.
since 9.8. Multiple packages may be supplied in the form of a
PackageLibrary registered
with the underlying CompilerInfo , or may be listed in the configuration file. Alternatively they
can be imported explicitly (taking care over the order of importing) using importPackage(XsltPackage) . |
void |
clearParameters()
Clear the values of all stylesheet parameters previously set using
setParameter(QName, XdmValue) . |
XsltExecutable |
compile(javax.xml.transform.Source source)
Compile a stylesheet.
|
XsltPackage |
compilePackage(javax.xml.transform.Source source)
Compile a library package.
|
java.lang.Iterable<XsltPackage> |
compilePackages(java.lang.Iterable<javax.xml.transform.Source> sources)
Deprecated.
since 9.8. Multiple packages may be supplied in the form of a
PackageLibrary registered
with the underlying CompilerInfo , or may be listed in the configuration file. Alternatively they
can be imported explicitly (taking care over the order of importing) using importPackage(XsltPackage) . |
void |
declareCollation(java.lang.String uri,
java.text.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(java.lang.String uri)
Declare the default collation
|
javax.xml.transform.Source |
getAssociatedStylesheet(javax.xml.transform.Source source,
java.lang.String media,
java.lang.String title,
java.lang.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.
|
java.lang.String |
getDefaultCollation()
Get the default collation
|
javax.xml.transform.ErrorListener |
getErrorListener()
Get the ErrorListener being used during this compilation episode
|
Processor |
getProcessor()
Get the Processor from which this XsltCompiler was constructed
|
java.lang.String |
getTargetEdition()
Get the target edition under which the stylesheet will be executed.
|
CompilerInfo |
getUnderlyingCompilerInfo()
Get the underlying CompilerInfo object, which provides more detailed (but less stable) control
over some compilation options
|
javax.xml.transform.URIResolver |
getURIResolver()
Get the URIResolver to be used during stylesheet compilation.
|
java.lang.String |
getXsltLanguageVersion()
Get the XSLT (and XPath) language level supported by the processor.
|
void |
importPackage(XsltPackage thePackage)
Import a library package.
|
void |
importPackage(XsltPackage thePackage,
java.lang.String packageName,
java.lang.String version)
Import a library package, changing the package name and/or version.
|
void |
importXQueryEnvironment(XQueryCompiler queryCompiler)
Import a compiled XQuery library.
|
boolean |
isAssertionsEnabled()
Ask whether assertions (xsl:assert instructions) should be enabled.
|
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 |
isJustInTimeCompilation()
Ask whether just-in-time compilation of template rules should be used.
|
boolean |
isRelocatable()
Ask whether any package produced by this compiler can be deployed to a different location, with a different base URI
|
boolean |
isSchemaAware()
Ask whether schema-awareness has been requested by means of a call on
setSchemaAware(boolean) |
XsltExecutable |
loadExecutablePackage(java.net.URI location)
Load a compiled package from a file or from a remote location, with the intent to use this as a complete
executable stylesheet, not as a library package.
|
XsltPackage |
loadLibraryPackage(java.net.URI location)
Load a compiled package from a file or from a remote location.
|
XsltPackage |
obtainPackage(java.lang.String packageName,
java.lang.String versionRange)
Import a named package, together with all the packages on which it depends, recursively.
|
XsltPackage |
obtainPackageWithAlias(java.lang.String alias)
Import a package from the configuration file (or more generally, from the packageLibrary
of this XsltCompiler) given an alias used to identify it
|
void |
setAssertionsEnabled(boolean enabled)
Say whether assertions (xsl:assert instructions) should be enabled.
|
void |
setCompileWithTracing(boolean option)
Set whether trace hooks are to be included in the compiled code.
|
void |
setErrorList(java.util.List<StaticError> errorList)
Supply a List object which will be populated with information about any static errors
encountered during the transformation.
|
void |
setErrorListener(javax.xml.transform.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 |
setJustInTimeCompilation(boolean jit)
Say whether just-in-time compilation of template rules should be used.
|
void |
setParameter(QName name,
XdmValue value)
Set the value of a stylesheet parameter.
|
void |
setRelocatable(boolean relocatable)
Say whether any package produced by this compiler can be deployed to a different location, with a different base URI
|
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 |
setTargetEdition(java.lang.String edition)
Set the target edition under which the stylesheet will be executed.
|
void |
setURIResolver(javax.xml.transform.URIResolver resolver)
Set the URIResolver to be used during stylesheet compilation.
|
void |
setXsltLanguageVersion(java.lang.String version)
Deprecated.
Has no effect from Saxon 9.8.
|
protected XsltCompiler(Processor processor)
Processor.newXsltCompiler()
.processor
- the Saxon processorpublic Processor getProcessor()
public void setURIResolver(javax.xml.transform.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 void clearParameters()
setParameter(QName, XdmValue)
.
This resets the parameters to their initial ("undeclared") statepublic javax.xml.transform.URIResolver getURIResolver()
public void setErrorListener(javax.xml.transform.ErrorListener listener)
listener
- The error listener to be used. This is notified of all errors detected during the
compilation.public javax.xml.transform.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 boolean isRelocatable()
public void setRelocatable(boolean relocatable)
relocatable
- if true then static-base-uri() represents the deployed location of the package,
rather than its compile time locationpublic void setTargetEdition(java.lang.String edition)
edition
- the Saxon edition for the run-time environment. One of "EE", "PE", "HE", "JS", or "JS2".public java.lang.String getTargetEdition()
public void declareCollation(java.lang.String uri, java.text.Collator collation)
uri
- the absolute collation URIcollation
- a Collator
object that implements the required collationjava.lang.IllegalArgumentException
- if an attempt is made to rebind the standard URI
for the Unicode codepoint collationpublic void declareDefaultCollation(java.lang.String uri)
uri
- the absolute URI of the default collation. Either this URI must have been bound to a collation
using the method Configuration.registerCollation(String, StringCollator)
Collation(String, java.text.Collator)}, or it must be a
collation that is recognized implicitly, such as a UCA collationjava.lang.IllegalStateException
- if the collation URI has not been registered, unless it is the standard
Unicode codepoint collation which is registered implicitlypublic java.lang.String getDefaultCollation()
public void setXsltLanguageVersion(java.lang.String version)
version
- the language level to be supported. The value is ignored.java.lang.IllegalArgumentException
- if the value is not equal to 0.0, 2.0, or 3.0public java.lang.String getXsltLanguageVersion()
public boolean isAssertionsEnabled()
public void setAssertionsEnabled(boolean enabled)
enabled
- true if assertions are enabled at compile timepublic 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 void importXQueryEnvironment(XQueryCompiler queryCompiler)
saxon:import-query
declaration.queryCompiler
- An XQueryCompiler that has been used to compile a library of XQuery functions
(by using one of the overloaded methods named compileLibrary
).public javax.xml.transform.Source getAssociatedStylesheet(javax.xml.transform.Source source, java.lang.String media, java.lang.String title, java.lang.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).
Note that Saxon does not implement the complex CSS3-based syntax for
media queries. By default, the media value is simply ignored. An algorithm for
comparing the requested media with the declared media can be defined using
the method Configuration.setMediaQueryEvaluator(Comparator)
.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(javax.xml.transform.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 java.lang.Iterable<XsltPackage> compilePackages(java.lang.Iterable<javax.xml.transform.Source> sources) throws SaxonApiException
PackageLibrary
registered
with the underlying CompilerInfo
, or may be listed in the configuration file. Alternatively they
can be imported explicitly (taking care over the order of importing) using importPackage(XsltPackage)
.sources
- the collection of packages to be compiled, in the form of an Iterablejava.lang.UnsupportedOperationException
- - always. This method is no longer available
from Saxon 9.8SaxonApiException
public java.lang.Iterable<XsltPackage> addCompilePackages(java.lang.Iterable<javax.xml.transform.Source> sources, boolean link) throws SaxonApiException, XPathException
PackageLibrary
registered
with the underlying CompilerInfo
, or may be listed in the configuration file. Alternatively they
can be imported explicitly (taking care over the order of importing) using importPackage(XsltPackage)
.sources
- Sources for the packageslink
- Link each of the packages when loaded if trueSaxonApiException
- 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
- if there are XPath errors in the stylesheetpublic XsltPackage loadLibraryPackage(java.net.URI location) throws SaxonApiException
The supplied URI represents the location of a resource which must have been originally
created using XsltPackage.save(java.io.File)
.
The result of loading the package is returned as an XsltPackage
object.
Note that this package is not automatically imported to this XsltCompiler
;
if the package is required for use in subsequent compilations then it must be explicitly
imported.
location
- the location from which the package is to be loaded, as a URISaxonApiException
- if no resource can be loaded from the supplied location or if the
resource that is loaded is not a compiled package, or if the compiled package is not
consistent with this XsltCompiler
(for example, if it was created using an
incompatible Saxon version).public XsltExecutable loadExecutablePackage(java.net.URI location) throws SaxonApiException
The supplied URI represents the location of a resource which must have been originally
created using XsltPackage.save(java.io.File)
.
The result of loading the package is returned as an XsltExecutable
object.
location
- the location from which the package is to be loaded, as a URISaxonApiException
- if no resource can be loaded from the supplied location or if the
resource that is loaded is not a compiled package, or if the compiled package is not
consistent with this XsltCompiler
(for example, if it was created using an
incompatible Saxon version).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 void importPackage(XsltPackage thePackage, java.lang.String packageName, java.lang.String version) throws SaxonApiException
xsl:use-package
declaration of subsequent compilations performed using the XsltCompiler
. The
supplied package name and version are used in place of the name and version used in the XSLT
source code. This provides a level of indirection: for example the same source package can
be compiled twice, with different settings for the static parameter values, and the two
different compiled versions can then be selected from xsl:use-package declarations. distinguishing
them by the new package name and/or version.thePackage
- the package to be importedpackageName
- the new package name to be used. If null, the original package name is used
unchangedversion
- the new package version number to be used. If null, the original package version
number is used unchangedSaxonApiException
- if the imported package was created under a different Processor
,
or if the supplied version number is invalidpublic XsltPackage obtainPackage(java.lang.String packageName, java.lang.String versionRange) throws SaxonApiException
packageName
- the name of the required package. This is the name under which it is registered
in the package library, which is not necessarily the same as the name appearing
in the XSLT source code.versionRange
- the required version of the package, or range of versions, in the format
of the package-version attribute of xsl:use-package.SaxonApiException
public XsltPackage obtainPackageWithAlias(java.lang.String alias) throws SaxonApiException
alias
- the alias of the package/version in the configuration fileSaxonApiException
- the package does not exist, or if compiling the package failspublic XsltExecutable compile(javax.xml.transform.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 treeXdmNode
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(java.util.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.public void setJustInTimeCompilation(boolean jit)
jit
- true if just-in-time compilation is to be enabled. With this option enabled,
static analysis of a template rule is deferred until the first time that the
template is matched. This can improve performance when many template
rules are rarely used during the course of a particular transformation; however,
it means that static errors in the stylesheet may go undetected.public boolean isJustInTimeCompilation()
Copyright (c) 2004-2018 Saxonica Limited. All rights reserved.