Package net.sf.saxon.sxpath
Class XPathEvaluator
java.lang.Object
net.sf.saxon.sxpath.XPathEvaluator
This class provides a native Saxon API for free-standing evaluation of XPath expressions. Unlike the
JAXP API offered by
XPathEvaluator
it exposes Saxon classes and interfaces
and thus provides a more strongly-typed interface with greater control over the detailed behaviour.
However, the preferred API for user applications calling XPath is the s9api XPathCompiler
interface.
- Since:
- 8.4
- Author:
- Michael H. Kay
-
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor.XPathEvaluator
(Configuration config) Construct an XPathEvaluator with a specified configuration. -
Method Summary
Modifier and TypeMethodDescriptioncreateExpression
(String expression) Prepare (compile) an XPath expression for subsequent evaluation.createPattern
(String pattern) Prepare (compile) an XSLT pattern for subsequent evaluation.declareVariable
(String uri, String localName) Declare a variable, making it available for use within a subsequently-compiled XPath expression.Get the Configuration in use.Get the external namespace resolver, if one has been set usingsetNamespaceResolver(net.sf.saxon.om.NamespaceResolver)
Get the current static context.void
Set the default namespace for elements and typesvoid
setNamespaceResolver
(NamespaceResolver namespaceResolver) Set the external namespace resolver to be used.void
setStaticContext
(XPathStaticContext context) Set the static context for compiling XPath expressions.
-
Constructor Details
-
XPathEvaluator
public XPathEvaluator()Default constructor. Creates an XPathEvaluator with a default configuration and name pool. Note that any source documents used by an XPath expression under this XPathEvaluator must be built using theConfiguration
that is implicitly created by this constructor, which is accessible using thegetConfiguration()
method. -
XPathEvaluator
Construct an XPathEvaluator with a specified configuration.- Parameters:
config
- the configuration to be used. If the XPathEvaluator is to be used to run schema-aware XPath expressions this must be an instance ofcom.saxonica.config.EnterpriseConfiguration
-
-
Method Details
-
getConfiguration
Get the Configuration in use.- Returns:
- the Saxon configuration
-
declareVariable
Declare a variable, making it available for use within a subsequently-compiled XPath expression. The variable may subsequently be assigned a value using the methodXPathDynamicContext.setVariable(XPathVariable, net.sf.saxon.om.ValueRepresentation)
. Any external variable used in an XPath expression must be declared before the XPath expression is compiled.- Parameters:
uri
- The namespace URI of the variable name. Use "" for the null namespace.localName
- The local part of the variable name.- Returns:
- an object representing the variable
-
setStaticContext
Set the static context for compiling XPath expressions. This provides more detailed control over the environment in which the expression is compiled, for example it allows namespace prefixes to be declared, variables to be bound and functions to be defined. For most purposes, the static context can be defined by providing and tailoring an instance of theIndependentContext
class. Until this method is called, a default static context is used, in which no namespaces are defined other than the standard ones (xml, xslt, and saxon), and no variables or functions (other than the core XPath functions) are available.- Parameters:
context
- the XPath static contextSetting a new static context clears any variables and namespaces that have previously been declared.
-
getStaticContext
Get the current static context. This will always return a value; if no static context has been supplied by the user, the system will have created its own. A system-created static context will always be an instance ofIndependentContext
- Returns:
- the static context object
-
createExpression
Prepare (compile) an XPath expression for subsequent evaluation.- Parameters:
expression
- The XPath expression to be compiled, supplied as a string.- Returns:
- an XPathExpression object representing the prepared expression
- Throws:
XPathException
- if the syntax of the expression is wrong, or if it references namespaces, variables, or functions that have not been declared.
-
createPattern
Prepare (compile) an XSLT pattern for subsequent evaluation. The result is an XPathExpression object representing a (pseudo-) expression that when evaluated returns a boolean result: true if the context node matches the pattern, false if it does not.- Parameters:
pattern
- the XSLT pattern to be compiled, supplied as a string- Returns:
- an XPathExpression object representing the pattern, wrapped as an expression
- Throws:
XPathException
- if the syntax of the expression is wrong, or if it references namespaces, variables, or functions that have not been declared.- Since:
- 9.1
-
setNamespaceResolver
Set the external namespace resolver to be used. The NamespaceResolver is stored as part of the static context. It overrides any namespaces declared directly using declareNamespace on the staticContext object- Parameters:
namespaceResolver
- The namespace resolver, which maintains a mapping of prefixes to URIs. Any namespace prefix used in the XPath expression is resolved using this namespaceResolver.
-
getNamespaceResolver
Get the external namespace resolver, if one has been set usingsetNamespaceResolver(net.sf.saxon.om.NamespaceResolver)
- Returns:
- the namespace resolver supplied by the user if set, or a system-defined namespace resolver
otherwise. By default, the
IndependentContext
object used as theXPathStaticContext
also acts as theNamespaceResolver
.
-
setDefaultElementNamespace
Set the default namespace for elements and types- Parameters:
uri
- The namespace to be used to qualify unprefixed element names and type names appearing in the XPath expression.
-