Class XsltCompiler
- java.lang.Object
-
- net.sf.saxon.s9api.XsltCompiler
-
public class XsltCompiler extends java.lang.Object
An XsltCompiler object allows XSLT 2.0 and XSLT 3.0 stylesheets to be compiled. The compiler holds information that represents the static context for the compilation.To construct an
XsltCompiler
, use the factory methodProcessor.newXsltCompiler()
on the Processor object.An
XsltCompiler
may be used repeatedly to compile multiple queries. Any changes made to theXsltCompiler
(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.If JIT compilation is enabled (this is the default for Saxon-EE), then static errors in template rules might be detected not during execution of the
compile()
method, but rather when the relevant code is first executed. In this situation thecompile()
method will not throw an exception, but the errors will still (eventually) be notified to theErrorListener
associated with the compiler.To avoid problems with error reporting, it is recommended that:
- When code is under development and static errors are therefore likely, a new
XsltCompiler
should be created for each compilation (and JIT should preferably be disabled, so that all static errors are detected.) - When production code is being loaded, and errors are therefore unlikely,
it is safe to use the same
XsltCompiler
for all compilations, and to enable JIT for maximum performance.
- Since:
- 9.0
- When code is under development and static errors are therefore likely, a new
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
XsltCompiler(Processor processor)
Protected constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
clearParameters()
Clear the values of all stylesheet parameters previously set usingsetParameter(QName, XdmValue)
.XsltExecutable
compile(javax.xml.transform.Source source)
Compile a stylesheet.XsltPackage
compilePackage(javax.xml.transform.Source source)
Compile a library package.void
declareDefaultCollation(java.lang.String uri)
Declare the default collationjavax.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 collationjava.lang.String
getDefaultElementNamespace()
Get the value of the default namespace for elements and typesjavax.xml.transform.ErrorListener
getErrorListener()
Deprecated.since 10.0.ErrorReporter
getErrorReporter()
Get the recipient of error information previously registered usingsetErrorReporter(ErrorReporter)
.Processor
getProcessor()
Get the Processor from which this XsltCompiler was constructedResourceResolver
getResourceResolver()
Get the ResourceResolver to be used during stylesheet compilation.java.lang.String
getTargetEdition()
Get the target edition under which the stylesheet will be executed.CompilerInfo
getUnderlyingCompilerInfo()
Get the underlyingCompilerInfo
object, which provides more detailed (but less stable) control over some compilation optionsUnprefixedElementMatchingPolicy
getUnprefixedElementMatchingPolicy()
Get the policy for handling of unprefixed element names in path expressions and match patterns.javax.xml.transform.URIResolver
getURIResolver()
Deprecated.since 11.1 - use aResourceResolver
insteadjava.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
isFastCompilation()
Ask if fast compilation has been enabled.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 URIboolean
isSchemaAware()
Ask whether schema-awareness has been requested by means of a call onsetSchemaAware(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.XsltExecutable
loadExecutablePackage(javax.xml.transform.Source source)
Load a compiled package from a supplied source, 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
loadLibraryPackage(javax.xml.transform.Source input)
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 itvoid
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
setDefaultElementNamespace(java.lang.String defaultNS)
Set the value of the default namespace for elements and typesvoid
setErrorList(java.util.List<? super XmlProcessingError> errorList)
Supply a (typically empty)List
which will be populated with information about any static errors encountered during the transformation.void
setErrorListener(javax.xml.transform.ErrorListener listener)
Deprecated.since 10.0.void
setErrorReporter(ErrorReporter reporter)
Supply a callback which will be notified of all static errors and warnings encountered during a compilation carried out using thisXsltCompiler
.void
setFastCompilation(boolean fast)
Request fast compilation.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 URIvoid
setResourceResolver(ResourceResolver resolver)
Set theResourceResolver
to be used during stylesheet compilation.void
setSchemaAware(boolean schemaAware)
Say that the stylesheet must be compiled to be schema-aware, even if it contains noxsl:import-schema
declarations.void
setTargetEdition(java.lang.String edition)
Set the target edition under which the stylesheet will be executed.void
setUnprefixedElementMatchingPolicy(UnprefixedElementMatchingPolicy unprefixedElementMatchingPolicy)
Set the policy for handling of unprefixed element names in path expressions and match patterns.void
setURIResolver(javax.xml.transform.URIResolver resolver)
Deprecated.since 11.1.void
setXsltLanguageVersion(java.lang.String version)
Set the XSLT (and XPath) language level to be supported by the processor.
-
-
-
Constructor Detail
-
XsltCompiler
protected XsltCompiler(Processor processor)
Protected constructor. The public way to create an XsltCompiler is by using the factory methodProcessor.newXsltCompiler()
.- Parameters:
processor
- the Saxon processor
-
-
Method Detail
-
getProcessor
public Processor getProcessor()
Get the Processor from which this XsltCompiler was constructed- Returns:
- the Processor to which this XsltCompiler belongs
- Since:
- 9.3
-
setURIResolver
public void setURIResolver(javax.xml.transform.URIResolver resolver)
Deprecated.since 11.1. UsesetResourceResolver(net.sf.saxon.lib.ResourceResolver)
Set the URIResolver to be used during stylesheet compilation. This interface is retained for backwards compatibility reasons. The suppliedURIResolver
is wrapped in aResourceResolver
. TheResourceResolver
interface is preferred because more information is made available; most obviously, the absolute URI formed by combining the base URI and relative URI.- Parameters:
resolver
- the URIResolver to be used during stylesheet compilation.
-
getURIResolver
public javax.xml.transform.URIResolver getURIResolver()
Deprecated.since 11.1 - use aResourceResolver
insteadGet the URIResolver to be used during stylesheet compilation.- Returns:
- the URIResolver used during stylesheet compilation. Returns null if no user-supplied URIResolver has been set.
-
setResourceResolver
public void setResourceResolver(ResourceResolver resolver)
Set theResourceResolver
to be used during stylesheet compilation. TheResourceResolver
is used for dereferencing an absolute URI (after URI resolution) to return aSource
representing the location where a stylesheet module can be found.This
ResourceResolver
is used to dereference the URIs appearing inxsl:import
,xsl:include
, andxsl:import-schema
declarations. It is not used for resolving the URI supplied for the main stylesheet module (as supplied to thecompile(javax.xml.transform.Source)
orcompilePackage(javax.xml.transform.Source)
methods. It is not used at run-time for resolving requests to thedocument()
or similar functions. (Instead, such functions use the run-timeResourceResolver
passed to theXsltTransformer
orXslt30Transformer
).- Parameters:
resolver
- theResourceResolver
to be used during stylesheet compilation.
-
setParameter
public void setParameter(QName name, XdmValue value)
Set the value of a stylesheet parameter. Static (compile-time) parameters must be provided using this method on the XsltCompiler object, prior to stylesheet compilation. Non-static parameters may also be provided using this method if their values will not vary from one transformation to another.- Parameters:
name
- the qualified name identifying the parameter, as declared in a global<xsl:param>
element in the stylesheetvalue
- the value of the parameter- Throws:
SaxonApiUncheckedException
- if the value is lazily evaluated, and evaluation fails
-
clearParameters
public void clearParameters()
Clear the values of all stylesheet parameters previously set usingsetParameter(QName, XdmValue)
. This resets the parameters to their initial ("undeclared") state
-
getResourceResolver
public ResourceResolver getResourceResolver()
Get the ResourceResolver to be used during stylesheet compilation.- Returns:
- the ResourceResolver used during stylesheet compilation. Returns null if no user-supplied ResourceResolver has been set.
-
setErrorListener
@Deprecated public void setErrorListener(javax.xml.transform.ErrorListener listener)
Deprecated.since 10.0. UsesetErrorReporter(ErrorReporter)
Set the ErrorListener to be used during this compilation episode- Parameters:
listener
- The error listener to be used. This is notified of all errors detected during the compilation.In earlier releases, contrary to the documentation, the supplied
ErrorListener
was also notified of run-time errors, unless a differentErrorListener
was supplied at run-time. This is no longer the case from Saxon 10.0
-
getErrorListener
@Deprecated public javax.xml.transform.ErrorListener getErrorListener()
Deprecated.since 10.0. UsegetErrorReporter()
Get the ErrorListener being used during this compilation episode- Returns:
- listener The error listener in use. This is notified of all errors detected during the compilation. Returns null if no user-supplied ErrorListener has been set.
-
setErrorList
public void setErrorList(java.util.List<? super XmlProcessingError> errorList)
Supply a (typically empty)List
which will be populated with information about any static errors encountered during the transformation.Calling this method overwrites the effect of any previous call on
setErrorListener(ErrorListener)
as well as previous calls onsetErrorList
.- Parameters:
errorList
- a List (typically empty) to which information will be appended about static errors found during the compilation. Each such error is represented by aXmlProcessingError
object.- Since:
- 9.9.
-
setErrorReporter
public void setErrorReporter(ErrorReporter reporter)
Supply a callback which will be notified of all static errors and warnings encountered during a compilation carried out using thisXsltCompiler
.Calling this method overwrites the effect of any previous call on
setErrorListener(ErrorListener)
orsetErrorList
.Note that if multiple compilations are carried out concurrently in different threads using the same
XsltCompiler
, then theErrorReporter
must be thread-safe; messages from different compilations will be interleaved, and there is no obvious way of determining which message originated from which compilation. In practice, it is only sensible to do this in an environment where the stylesheets being compiled are known to be error-free.- Parameters:
reporter
- a Consumer which will be notified of all Static errors and warnings encountered during a compilation episode.- Since:
- 10.0
-
getErrorReporter
public ErrorReporter getErrorReporter()
Get the recipient of error information previously registered usingsetErrorReporter(ErrorReporter)
.- Returns:
- the consumer previously registered explicitly using
setErrorReporter(ErrorReporter)
, or implicitly usingsetErrorListener(ErrorListener)
orsetErrorList(List)
. If no error reporter has been registered, the result may be null, or may return a system supplied error reporter. - Since:
- 10.0
-
setSchemaAware
public void setSchemaAware(boolean schemaAware)
Say that the stylesheet must be compiled to be schema-aware, even if it contains noxsl:import-schema
declarations. By default a stylesheet is treated as schema-aware only if it contains one or morexsl:import-schema
declarations. If it is not schema-aware, then all input documents must be untyped, and validation of temporary trees is disallowed (though validation of the final result tree is permitted). Setting the argument to true means that schema-aware code will be compiled regardless.Setting the value to true has no effect (it is not an error) if the configuration does not support schema processing. (Schema processing requires a Saxon-EE license.)
- Parameters:
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.- Since:
- 9.2
-
isSchemaAware
public boolean isSchemaAware()
Ask whether schema-awareness has been requested by means of a call onsetSchemaAware(boolean)
- Returns:
- true if schema-awareness has been requested
- Since:
- 9.2
-
isRelocatable
public boolean isRelocatable()
Ask whether any package produced by this compiler can be deployed to a different location, with a different base URI- Returns:
- if true then static-base-uri() represents the deployed location of the package, rather than its compile time location
- Since:
- 9.8
-
setRelocatable
public void setRelocatable(boolean relocatable)
Say whether any package produced by this compiler can be deployed to a different location, with a different base URI- Parameters:
relocatable
- if true then static-base-uri() represents the deployed location of the package, rather than its compile time location- Since:
- 9.8
-
setTargetEdition
public void setTargetEdition(java.lang.String edition)
Set the target edition under which the stylesheet will be executed.- Parameters:
edition
- the Saxon edition for the run-time environment. One of "EE", "PE", "HE", or "JS".- Since:
- 9.7.0.5. Experimental and subject to change.
-
getTargetEdition
public java.lang.String getTargetEdition()
Get the target edition under which the stylesheet will be executed.- Returns:
- the Saxon edition for the run-time environment. One of "EE", "PE", "HE", or "JS".
- Since:
- 9.7.0.5. Experimental and subject to change.
-
declareDefaultCollation
public void declareDefaultCollation(java.lang.String uri)
Declare the default collation- Parameters:
uri
- the absolute URI of the default collation. Either this URI must have been bound to a collation using the methodConfiguration.registerCollation(String, StringCollator)
, or it must be a collation that is recognized implicitly, such as a UCA collation- Throws:
java.lang.IllegalStateException
- if the collation URI is not a recognized collation- Since:
- 9.5
-
getDefaultCollation
public java.lang.String getDefaultCollation()
Get the default collation- Returns:
- the URI of the default collation if one has been set, or the URI of the codepoint collation otherwise
- Since:
- 9.7.0.2
-
setXsltLanguageVersion
public void setXsltLanguageVersion(java.lang.String version)
Set the XSLT (and XPath) language level to be supported by the processor. Set the value to "4.0" to enable support for experimental features defined in the XSLT 4.0 proposal (which is likely to change before it stabilizes).- Parameters:
version
- the language level to be supported. The values "3.0" and "4.0" are recognized- Throws:
java.lang.IllegalArgumentException
- if the value is not equal to 3.0 or 4.0- Since:
- 9.3. From 11.0, accepts the values "3.0" and "4.0"
-
getXsltLanguageVersion
public java.lang.String getXsltLanguageVersion()
Get the XSLT (and XPath) language level supported by the processor.- Returns:
- the language level supported. In Saxon 11 this returns either "3.0" or "4.0".
- Since:
- 9.3
-
isAssertionsEnabled
public boolean isAssertionsEnabled()
Ask whether assertions (xsl:assert instructions) should be enabled. By default they are disabled. If assertions are enabled at compile time, then by default they will also be enabled at run time; but they can be disabled at run time by specific request- Returns:
- true if assertions are enabled at compile time
- Since:
- 9.7
-
setAssertionsEnabled
public void setAssertionsEnabled(boolean enabled)
Say whether assertions (xsl:assert instructions) should be enabled. By default they are disabled. If assertions are enabled at compile time, then by default they will also be enabled at run time; but they can be disabled at run time by specific request- Parameters:
enabled
- true if assertions are enabled at compile time- Since:
- 9.7
-
setFastCompilation
public void setFastCompilation(boolean fast)
Request fast compilation. Fast compilation will generally be achieved at the expense of run-time performance and quality of diagnostics. Fast compilation is a good trade-off if (a) the stylesheet is known to be correct, and (b) once compiled, it is only executed once against a document of modest size.Fast compilation may result in static errors going unreported, especially if they occur in code that is never executed.
The current implementation is equivalent to switching off all optimizations other than just-in-time compilation of template rules. Setting this option, however, indicates an intent rather than a mechanism, and the implementation details may change in future to reflect the intent.
- Parameters:
fast
- set to true to request fast compilation; set to false to revert to the optimization options defined in the Configuration.- Since:
- 9.9
-
isFastCompilation
public boolean isFastCompilation()
Ask if fast compilation has been enabled.- Returns:
- true if fast compilation has been enabled (technically, this returns true if the only optimizer option that has been enabled is JIT compilation of template rules).
- Since:
- 9.9
-
setCompileWithTracing
public void setCompileWithTracing(boolean option)
Set whether trace hooks are to be included in the compiled code. To use tracing, it is necessary both to compile the code with trace hooks included, and to supply a TraceListener at run-timeSetting tracing to true suppresses those optimizations that cause major reorganisation of the code, such as function inlining and loop-lifting. These optimizations can be reinstated, but this will tend to make trace output harder to interpret.
- Parameters:
option
- true if trace code is to be compiled in, false otherwise- Since:
- 9.3
-
isCompileWithTracing
public boolean isCompileWithTracing()
Ask whether trace hooks are included in the compiled code.- Returns:
- true if trace hooks are included, false if not.
- Since:
- 9.3
-
setGenerateByteCode
public void setGenerateByteCode(boolean option)
Set whether bytecode should be generated for the compiled stylesheet. This option is available only with Saxon-EE. The default depends on the setting in the configuration at the time the XsltCompiler is instantiated, and by default is true for Saxon-EE.- Parameters:
option
- true if bytecode is to be generated, false otherwise- Since:
- 9.6
-
isGenerateByteCode
public boolean isGenerateByteCode()
Ask whether bytecode is to be generated in the compiled code.- Returns:
- true if bytecode is to be generated, false if not.
- Since:
- 9.6
-
importXQueryEnvironment
public void importXQueryEnvironment(XQueryCompiler queryCompiler)
Import a compiled XQuery library. This makes pre-compiled XQuery library modules available to thesaxon:import-query
declaration.- Parameters:
queryCompiler
- An XQueryCompiler that has been used to compile a library of XQuery functions (by using one of the overloaded methods namedcompileLibrary
).
-
getAssociatedStylesheet
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
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. If there are several suitable xml-stylesheet processing instructions, then the returned Source will identify a synthesized stylesheet module that imports all the referenced stylesheet module.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
.- Parameters:
source
- The XML source document. Note that if the source document is available as an instance ofXdmNode
, a correspondingSource
can be obtained using the methodXdmNode.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 methodConfiguration.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.- Returns:
- A Source object suitable for passing to
compile(javax.xml.transform.Source)
. - Throws:
SaxonApiException
- if any problems occur, including the case where no matching xml-stylesheet processing instruction is found.- Since:
- 9.6
-
compilePackage
public XsltPackage compilePackage(javax.xml.transform.Source source) throws SaxonApiException
Compile a library package.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 usingimportPackage(net.sf.saxon.s9api.XsltPackage)
.- Parameters:
source
- identifies an XML document holding the the XSLT package to be compiled- Returns:
- the XsltPackage that results from the compilation. 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. - Throws:
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 registeredErrorListener
if there is one, or reported on theSystem.err
output stream otherwise.- Since:
- 9.6
- See Also:
- especially the notes regarding error handling and just-in-time compilation.
-
loadLibraryPackage
public XsltPackage loadLibraryPackage(java.net.URI location) throws SaxonApiException
Load a compiled package from a file or from a remote location.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 thisXsltCompiler
; if the package is required for use in subsequent compilations then it must be explicitly imported.- Parameters:
location
- the location from which the package is to be loaded, as a URI- Returns:
- the compiled package loaded from the supplied file or remote location
- Throws:
SaxonApiException
- 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 thisXsltCompiler
(for example, if it was created using an incompatible Saxon version).- Since:
- 9.7
-
loadLibraryPackage
public XsltPackage loadLibraryPackage(javax.xml.transform.Source input) throws SaxonApiException
Load a compiled package from a file or from a remote location.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 thisXsltCompiler
; if the package is required for use in subsequent compilations then it must be explicitly imported.- Parameters:
input
- the source from which the package is to be loaded, as a JAXP Source object- Returns:
- the compiled package loaded from the supplied file or remote location
- Throws:
SaxonApiException
- 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 thisXsltCompiler
(for example, if it was created using an incompatible Saxon version).- Since:
- 10.0
-
loadExecutablePackage
public XsltExecutable loadExecutablePackage(java.net.URI location) throws SaxonApiException
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.The supplied URI represents the location of a resource which must have been originally created using
XsltPackage.save(java.io.File)
or some equivalent.The result of loading the package is returned as an
XsltExecutable
object.- Parameters:
location
- the location from which the package is to be loaded, as a URI- Returns:
- the compiled package loaded from the supplied file or remote location
- Throws:
SaxonApiException
- 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 thisXsltCompiler
(for example, if it was created using an incompatible Saxon version).- Since:
- 9.7
-
loadExecutablePackage
public XsltExecutable loadExecutablePackage(javax.xml.transform.Source source) throws SaxonApiException
Load a compiled package from a supplied source, with the intent to use this as a complete executable stylesheet, not as a library package.The supplied source represents the location of a resource which must have been originally created using
XsltPackage.save(java.io.File)
or some equivalent.The result of loading the package is returned as an
XsltExecutable
object.- Parameters:
source
- the source from which the package is to be loaded- Returns:
- the compiled package loaded from the supplied file or remote location
- Throws:
SaxonApiException
- 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 thisXsltCompiler
(for example, if it was created using an incompatible Saxon version).- Since:
- 11
-
importPackage
public void importPackage(XsltPackage thePackage) throws SaxonApiException
Import a library package. Calling this method makes the supplied package available for reference in thexsl:use-package
declarations of subsequent compilations performed using thisXsltCompiler
.- Parameters:
thePackage
- the package to be imported- Throws:
SaxonApiException
- if the imported package was created under a differentProcessor
- Since:
- 9.6
-
importPackage
public void importPackage(XsltPackage thePackage, java.lang.String packageName, java.lang.String version) throws SaxonApiException
Import a library package, changing the package name and/or version. Calling this method makes the supplied package available for reference in thexsl:use-package
declaration of subsequent compilations performed using theXsltCompiler
. 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.- Parameters:
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 unchanged- Throws:
SaxonApiException
- if the imported package was created under a differentProcessor
, or if the supplied version number is invalid- Since:
- 9.8
-
obtainPackage
public XsltPackage obtainPackage(java.lang.String packageName, java.lang.String versionRange) throws SaxonApiException
Import a named package, together with all the packages on which it depends, recursively. The package must be identified in the package library for thisXsltCompiler
, which defaults to the package library defined in theConfiguration
, typically set up by loading a configuration file.- Parameters:
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.- Returns:
- the best matching package if there is one, or null otherwise. The name of the package must match; if there are multiple versions, then the version chosen is based first on the priority attached to this package/version in the library, and if the priorities are equal (or there are no explicit priorities) then the one with highest version number is taken.
- Throws:
SaxonApiException
- if an error is detected- Since:
- 9.8
-
obtainPackageWithAlias
public XsltPackage obtainPackageWithAlias(java.lang.String alias) throws SaxonApiException
Import a package from the configuration file (or more generally, from the packageLibrary of this XsltCompiler) given an alias used to identify it- Parameters:
alias
- the alias of the package/version in the configuration file- Returns:
- the referenced package
- Throws:
SaxonApiException
- the package does not exist, or if compiling the package fails
-
compile
public XsltExecutable compile(javax.xml.transform.Source source) throws SaxonApiException
Compile a stylesheet.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 relative URIs found in
<xsl:include>
and<xsl:import>
declarations against the base URI that is defined as thesystemId
property of the suppliedSource
.The following kinds of
Source
are recognized:StreamSource
, allowing the stylesheet to be supplied as a URI, as aFile
, as anInputStream
, or as aReader
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- Document wrappers for XOM, JDOM, DOM4J, or AXIOM trees
- A Saxon
NodeInfo
, representing the root of a tree in any of the native tree formats supported by Saxon
The compilation uses a snapshot of the properties of the
XsltCompiler
at the time this method is invoked. This is relevant when JIT compilation is enabled (which is the default under Saxon-EE). Under JIT compilation, templates are compiled when first executed, which will typically be after thecompile()
method has returned. Subsequent changes to the properties of theXsltCompiler
, for example, setting a newErrorListener
, have no effect on the delayed compilation of template rules at execution time.If JIT compilation is enabled (this is the default for Saxon-EE), then static errors in template rules might be detected not during execution of the
compile()
method, but rather when the relevant code is first executed. In this situation thecompile()
method will not throw an exception, but the errors will still (eventually) be notified to theErrorListener
orErrorList
associated with the compiler: more specifically, theErrorListener
orErrorList
that was associated with theXsltCompiler
at the timecompile()
was invoked.- Parameters:
source
- Source object representing the principal stylesheet module to be compiled. Must not be null. If theSource
wraps a resource such as aReader
orInputStream
then the resource will be consumed by the method.- Returns:
- an
XsltExecutable
, which represents the compiled stylesheet. TheXsltExecutable
is immutable and thread-safe; it may be used to run multiple transformations, in series or concurrently. - Throws:
SaxonApiException
- 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 registeredErrorListener
orErrorList
. The defaultErrorListener
displays error messages on the standard error output.
-
getUnderlyingCompilerInfo
public CompilerInfo getUnderlyingCompilerInfo()
Get the underlyingCompilerInfo
object, which provides more detailed (but less stable) control over some compilation options- Returns:
- the underlying
CompilerInfo
object, which holds compilation-time options. The methods on theCompilerInfo
object are not guaranteed stable from release to release.
-
setJustInTimeCompilation
public void setJustInTimeCompilation(boolean jit)
Say whether just-in-time compilation of template rules should be used.- Parameters:
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 will not necessarily cause thecompile(Source)
method to throw an exception (errors in code that is actually executed will still be notified to the registeredErrorListener
orErrorList
, but this may happen after thecompile(Source)
method returns). This option is enabled by default in Saxon-EE, and is not available in Saxon-HE or Saxon-PE.Recommendation: disable this option unless you are confident that the stylesheet you are compiling is error-free.
- Throws:
java.lang.UnsupportedOperationException
- if the argument is set totrue
and the configuration is not a licensed Saxon-EE configuration.
-
isJustInTimeCompilation
public boolean isJustInTimeCompilation()
Ask whether just-in-time compilation of template rules should be used.- Returns:
- true if just-in-time compilation is 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.
-
getDefaultElementNamespace
public java.lang.String getDefaultElementNamespace()
Get the value of the default namespace for elements and types- Returns:
- the value that was set using the method
getDefaultElementNamespace()
, if any; otherwise, the default value which is an empty string, representing "no namespace".
-
setDefaultElementNamespace
public void setDefaultElementNamespace(java.lang.String defaultNS)
Set the value of the default namespace for elements and types- Parameters:
defaultNS
- the value to be used as the default namespace for elements and types. This provides a default for the[xsl:]xpath-default-namespace
attribute in the stylesheet, and it has no effect in any part of the stylesheet where an explicit value for[xsl:]xpath-default-namespace
is in force. The default value is a zero-length string, representing "no namespace".
-
getUnprefixedElementMatchingPolicy
public UnprefixedElementMatchingPolicy getUnprefixedElementMatchingPolicy()
Get the policy for handling of unprefixed element names in path expressions and match patterns.- Returns:
- the policy previously set using
setUnprefixedElementMatchingPolicy(UnprefixedElementMatchingPolicy)
, or its default, which isUnprefixedElementMatchingPolicy.DEFAULT_NAMESPACE
.
-
setUnprefixedElementMatchingPolicy
public void setUnprefixedElementMatchingPolicy(UnprefixedElementMatchingPolicy unprefixedElementMatchingPolicy)
Set the policy for handling of unprefixed element names in path expressions and match patterns. By default, such names are expanded using the default namespace for elements and types, which can be set either using the[xsl:]default-xpath-namespace
attribute in the stylesheet, or programmatically using the methodsetDefaultElementNamespace(String)
. The default policy isUnprefixedElementMatchingPolicy.DEFAULT_NAMESPACE
, which causes such names to be expanded using the default namespace for elements and types, which itself defaults to "no namespace", but which can be changed either using the[xsl:]xpath-default-namespace
attribute in the stylesheet, or using thesetDefaultElementNamespace(String)
method.Note that any setting other than the default causes the stylesheet to behave in a way that is not conformant with the W3C XSLT 3.0 specifications.
The chosen policy affects:
- Any NCName used as a node-test in an axis step (production
ForwardStep
orReverseStep
) in an XPath expression within the stylesheet, other than an axis step using the attribute or namespace axis - Any NCName used as a node-test in an axis step (production
ForwardStepP
in a pattern within the stylesheet, other than an axis step using the attribute or namespace axis
It does not affect names appearing in other contexts (for example, names used in
xsl:strip-space/@elements
), and it does not affect name tests expressed in a form other than a simple NCName (for example tests of the formQ{}local
, or*:local
, orelement(local)
).It does not affect XPath expressions evaluated dynamically using
xsl:evaluate
.- Parameters:
unprefixedElementMatchingPolicy
- the policy for handling unprefixed elements.
- Any NCName used as a node-test in an axis step (production
-
-