public class XPathCompiler
extends java.lang.Object
To construct an XPathCompiler, use the factory method
Processor.newXPathCompiler()
.
An XPathCompiler may be used repeatedly to compile multiple queries. Any changes made to the XPathCompiler (that is, to the static context) do not affect queries that have already been compiled. An XPathCompiler may be used concurrently in multiple threads, but it should not then be modified once initialized.
Changes to an XPathCompiler are cumulative. There is no simple way to reset the XPathCompiler to its initial state; instead, simply create a new XPathCompiler.
The XPathCompiler
has the ability to maintain a cache of compiled
expressions. This is active only if enabled by calling setCaching(boolean)
.
If caching is enabled, then the compiler will recognize an attempt to compile
the same expression twice, and will avoid the cost of recompiling it. The cache
is emptied by any method that changes the static context for subsequent expressions,
for example, setBaseURI(java.net.URI)
. Unless the cache is emptied,
it grows indefinitely: compiled expressions are never discarded.
Modifier | Constructor and Description |
---|---|
protected |
XPathCompiler(Processor processor)
Protected constructor
|
Modifier and Type | Method and Description |
---|---|
XPathExecutable |
compile(java.lang.String source)
Compile an XPath expression, supplied as a character string.
|
XPathExecutable |
compilePattern(java.lang.String source)
Compile an XSLT 2.0 pattern, supplied as a character string.
|
void |
declareDefaultCollation(java.lang.String uri)
Declare the default collation
|
void |
declareNamespace(java.lang.String prefix,
java.lang.String uri)
Declare a namespace binding as part of the static context for XPath expressions compiled using this
XPathCompiler
|
void |
declareVariable(QName qname)
Declare a variable as part of the static context for XPath expressions compiled using this
XPathCompiler . |
void |
declareVariable(QName qname,
ItemType itemType,
OccurrenceIndicator occurrences)
Declare a variable as part of the static context for XPath expressions compiled using this
XPathCompiler . |
XdmValue |
evaluate(java.lang.String expression,
XdmItem contextItem)
Compile and evaluate an XPath expression, supplied as a character string, with a given
context item.
|
XdmItem |
evaluateSingle(java.lang.String expression,
XdmItem contextItem)
Compile and evaluate an XPath expression whose result is expected to be
a single item, with a given context item.
|
java.net.URI |
getBaseURI()
Get the static base URI for XPath expressions compiled using this XPathCompiler.
|
java.lang.String |
getLanguageVersion()
Ask whether an XPath 2.0, XPath 3.0 or XPath 3.1 processor is being used
|
Processor |
getProcessor()
Get the Processor from which this XPathCompiler was constructed
|
ItemType |
getRequiredContextItemType()
Get the required type of the context item.
|
StaticContext |
getUnderlyingStaticContext()
Escape-hatch method to get the underlying static context object used by the implementation.
|
void |
importSchemaNamespace(java.lang.String uri)
Import a schema namespace: that is, add the element and attribute declarations and type definitions
contained in a given namespace to the static context for the XPath expression.
|
boolean |
isAllowUndeclaredVariables()
Ask whether undeclared variables are allowed.
|
boolean |
isBackwardsCompatible()
Ask whether XPath 1.0 backwards compatibility mode is in force.
|
boolean |
isCaching()
Ask whether the compiler is maintaining a cache of compiled expressions
|
boolean |
isFastCompilation()
Ask if fast compilation has been enabled.
|
boolean |
isSchemaAware()
Ask whether XPath expressions compiled using this XPathCompiler are
schema-aware.
|
void |
setAllowUndeclaredVariables(boolean allow)
Say whether undeclared variables are allowed.
|
void |
setBackwardsCompatible(boolean option)
Say whether XPath 1.0 backwards compatibility mode is to be used.
|
void |
setBaseURI(java.net.URI uri)
Set the static base URI for XPath expressions compiled using this XPathCompiler.
|
void |
setCaching(boolean caching)
Say whether the compiler should maintain a cache of compiled expressions.
|
void |
setDecimalFormatProperty(QName format,
java.lang.String property,
java.lang.String value)
Registers the required decimal format properties
|
void |
setFastCompilation(boolean fast)
Request fast compilation.
|
void |
setLanguageVersion(java.lang.String value)
Say whether an XPath 2.0, XPath 3.0 or XPath 3.1 processor is required.
|
void |
setRequiredContextItemType(ItemType type)
Declare the static type of the context item.
|
void |
setSchemaAware(boolean schemaAware)
Say whether XPath expressions compiled using this
XPathCompiler are
schema-aware. |
protected XPathCompiler(Processor processor)
processor
- the s9api Processorpublic Processor getProcessor()
public void setBackwardsCompatible(boolean option)
option
- true if XPath 1.0 backwards compatibility is to be enabled, false if it is to
be disabled.public boolean isBackwardsCompatible()
public void setSchemaAware(boolean schemaAware)
XPathCompiler
are
schema-aware. They will automatically be schema-aware if the method
importSchemaNamespace(String)
is called. An XPath expression
must be marked as schema-aware if it is to handle typed (validated)
input documents.schemaAware
- true if expressions are to be schema-aware, false otherwisejava.lang.UnsupportedOperationException
- if schema-awareness is requested when this is
not a licensed Saxon-EE configuration.public boolean isSchemaAware()
importSchemaNamespace(String)
is called. An XPath expression
must be marked as schema-aware if it is to handle typed (validated)
input documents.public void setLanguageVersion(java.lang.String value)
value
- One of the values 1.0, 2.0, 3.0, 3.05, 3.1.
Setting the option to 1.0 requests an XPath 2.0 processor running in 1.0 compatibility mode; this is equivalent to setting the language version to 2.0 and backwards compatibility mode to true. Requesting "3.05" gives XPath 3.0 plus the extensions defined in the XSLT 3.0 specification (map types and map constructors).
java.lang.IllegalArgumentException
- if the version is not numerically equal to 1.0, 2.0, 3.0, 3.05, or 3.1.public java.lang.String getLanguageVersion()
public void setBaseURI(java.net.URI uri)
uri
- the base URI to be set in the static context. This must be an absolute URI, or null
to indicate that no static base URI is available.public java.net.URI getBaseURI()
public void declareNamespace(java.lang.String prefix, java.lang.String uri)
prefix
- The namespace prefix. If the value is a zero-length string, this method sets the default
namespace for elements and types.uri
- The namespace URI. It is possible to specify a zero-length string to "undeclare" a namespace;
in this case the prefix will not be available for use, except in the case where the prefix
is also a zero length string, in which case the absence of a prefix implies that the name
is in no namespace.java.lang.NullPointerException
- if either the prefix or uri is null.public void importSchemaNamespace(java.lang.String uri)
This method will not cause the schema to be loaded. That must be done separately, using the
SchemaManager
. This method will not fail if the schema has not been loaded (but in that case
the set of declarations and definitions made available to the XPath expression is empty). The schema
document for the specified namespace may be loaded before or after this method is called.
This method does not bind a prefix to the namespace. That must be done separately, using the
declareNamespace(String, String)
method.
uri
- The schema namespace to be imported. To import declarations in a no-namespace schema,
supply a zero-length string.public void setAllowUndeclaredVariables(boolean allow)
allow
- true if undeclared variables are allowed, false if they are not allowed.public boolean isAllowUndeclaredVariables()
public void declareVariable(QName qname)
XPathCompiler
. It is an error for the XPath expression to refer to a variable unless it has been
declared, unless the method setAllowUndeclaredVariables(boolean)
has been called to permit
undeclared variables. This method declares the existence of the variable, but it does not
bind any value to the variable; that is done later, when the XPath expression is evaluated.
The variable is allowed to have any type (that is, the required type is item()*
).qname
- The name of the variable, expressions as a QNamepublic void declareVariable(QName qname, ItemType itemType, OccurrenceIndicator occurrences)
XPathCompiler
. It is an error for the XPath expression to refer to a variable unless it has been
declared, unless the method setAllowUndeclaredVariables(boolean)
has been called to permit
undeclared variables. This method declares the existence of the variable, and defines the required type
of the variable, but it does not bind any value to the variable; that is done later,
when the XPath expression is evaluated.qname
- The name of the variable, expressed as a QName
itemType
- The required item type of the value of the variable, for example ItemType.BOOLEAN
occurrences
- The allowed number of items in the sequence forming the value of the variablepublic void setRequiredContextItemType(ItemType type)
type
- the required type of the context item, for example, ItemType.ANY_NODE
.public ItemType getRequiredContextItemType()
ItemType.ANY_ITEM
(representing the type item()
) is returned.public void declareDefaultCollation(java.lang.String uri)
uri
- the absolute URI of the default collation. This URI must identify a known collation;
either one that has been explicitly declared, or one that is recognized implicitly,
such as a UCA collationjava.lang.IllegalStateException
- if the collation URI is not recognized as a known collationpublic void setCaching(boolean caching)
caching
- if set to true, caching of compiled expressions is enabled.
If set to false, any existing cache is cleared, and future compiled expressions
will not be cached until caching is re-enabled. The cache is also cleared
(but without disabling future caching)
if any method is called that changes the static context for compiling
expressions, for example declareVariable(QName)
or
declareNamespace(String, String)
.public boolean isCaching()
public void setFastCompilation(boolean fast)
fast
- set to true to request fast compilation; set to false to revert to the optimization options
defined in the Configuration.public boolean isFastCompilation()
public XPathExecutable compile(java.lang.String source) throws SaxonApiException
source
- A non-null string containing the source text of the XPath expressionSaxonApiException
- if any static error is detected while analyzing the expression.
Note: prior to Saxon 9.7, static errors were also notified to the ErrorListener associated with the containing Processor/Configuration. This is no longer the case.
public XdmValue evaluate(java.lang.String expression, XdmItem contextItem) throws SaxonApiException
expression
- A non-null string containing the source text of the XPath expressioncontextItem
- The context item to be used for evaluating the expression. This
may be null if the expression is to be evaluated with no context item.Iterable
, so that it can be used in a "for-each" loop such as
for (XdmItem item : xpath.evaluate("//x", doc) {...}
SaxonApiException
- if any static error is detected while analyzing the expression,
or if any dynamic error is detected while evaluating it.public XdmItem evaluateSingle(java.lang.String expression, XdmItem contextItem) throws SaxonApiException
expression
- A non-null string containing the source text of the XPath expressioncontextItem
- The context item to be used for evaluating the expression. This
may be null if the expression is to be evaluated with no context item.SaxonApiException
- if any static error is detected while analyzing the expression,
or if any dynamic error is detected while evaluating it.public XPathExecutable compilePattern(java.lang.String source) throws SaxonApiException
source
- A non-null string conforming to the syntax of XSLT 2.0 patternsSaxonApiException
- if the pattern contains static errors: for example, if its syntax is incorrect,
or if it refers to undeclared variables or namespacespublic void setDecimalFormatProperty(QName format, java.lang.String property, java.lang.String value) throws SaxonApiException
format
- The name of the decimal formatproperty
- The decimal symbols name to update, for example "grouping-separator"
value
- The value to update the decimal symbol propertySaxonApiException
- if there are two conflicting definitions of the named decimal-formatpublic StaticContext getUnderlyingStaticContext()
IndependentContext
.
This method provides an escape hatch to internal Saxon implementation objects that offer a finer and lower-level degree of control than the s9api classes and methods. Some of these classes and methods may change from release to release.
Copyright (c) 2004-2020 Saxonica Limited. All rights reserved.