net.sf.saxon.sxpath

Class XPathEvaluator


public class XPathEvaluator
extends java.lang.Object

This class provide 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.
Author:
Michael H. Kay

Constructor Summary

XPathEvaluator()
Default constructor.
XPathEvaluator(Configuration config)
Construct an XPathEvaluator with a specified configuration.

Method Summary

NodeInfo
build(Source source)
Deprecated. since 8.9.
XPathExpression
createExpression(String expression)
Prepare (compile) an XPath expression for subsequent evaluation.
XPathVariable
declareVariable(String uri, String localName)
Declare a variable, making it available for use within a subsequently-compiled XPath expression.
Configuration
getConfiguration()
Get the Configuration in use.
Executable
getExecutable()
Get the executable
NamespaceResolver
getNamespaceResolver()
Get the external namespace resolver, if one has been set using setNamespaceResolver(NamespaceResolver)
XPathStaticContext
getStaticContext()
Get the current static context.
static void
main(String[] args)
For testing only
void
setDefaultElementNamespace(String uri)
Set the default namespace for elements and types
void
setNamespaceResolver(NamespaceResolver namespaceResolver)
Set the external namespace resolver to be used.
void
setStaticContext(XPathStaticContext context)
Set the static context for compiling XPath expressions.
void
setStripSpace(boolean strip)
Deprecated. since 8.9.

Constructor Details

XPathEvaluator

public XPathEvaluator()

XPathEvaluator

public XPathEvaluator(Configuration config)
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 of com.saxonica.validate.SchemaAwareConfiguration

Method Details

build

public NodeInfo build(Source source)
            throws XPathException

Deprecated. since 8.9. The preferred way to build a source document is to use Configuration.buildDocument(Source)

Build a source document.

This method is retained for backwards compability. The preferred way to build a document tree is to call the method Configuration.buildDocument(Source)

Parameters:
source - a JAXP Source object. This may be any implementation of Source that Saxon recognizes: not only the standard kinds of source such as StreamSource, SAXSource, and DOMSource, but also for example a JDOM or XOM DocumentWrapper. For the way in which the source document is built, see Configuration.buildDocument(Source)
Returns:
the NodeInfo representing the root of the constructed tree.
Throws:
XPathException - if, for example, XML parsing fails.

createExpression

public XPathExpression createExpression(String expression)
            throws XPathException
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.

declareVariable

public XPathVariable declareVariable(String uri,
                                     String localName)
Declare a variable, making it available for use within a subsequently-compiled XPath expression. The variable may subsequently be assigned a value using the method XPathDynamicContext.setVariable(XPathVariable,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

getConfiguration

public Configuration getConfiguration()
Get the Configuration in use.
Returns:
the Saxon configuration

getExecutable

public Executable getExecutable()
Get the executable
Returns:
the executable. This holds details of function bindings and collations.

getNamespaceResolver

public NamespaceResolver getNamespaceResolver()
Get the external namespace resolver, if one has been set using setNamespaceResolver(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 the XPathStaticContext also acts as the NamespaceResolver.

getStaticContext

public XPathStaticContext 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 of IndependentContext
Returns:
the static context object

main

public static void main(String[] args)
            throws Exception
For testing only

setDefaultElementNamespace

public void setDefaultElementNamespace(String uri)
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.

setNamespaceResolver

public void setNamespaceResolver(NamespaceResolver namespaceResolver)
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.

setStaticContext

public void setStaticContext(XPathStaticContext context)
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 the IndependentContext 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 context

Setting a new static context clears any variables and namespaces that have previously been declared.


setStripSpace

public void setStripSpace(boolean strip)

Deprecated. since 8.9. The preferred way to build a source document is to use Configuration.buildDocument(Source)

Indicate whether all whitespace text nodes in source documents are to be removed. This affects the action of the build(Source) method, and of all other methods that take a Source as input.
Parameters:
strip - True if all whitespace text nodes are to be stripped from the source document, false otherwise. The default if the method is not called is false.