public class XPathEvaluator extends Object implements XPath
XPathEvaluator provides a simple API for standalone XPath processing (that is, executing XPath expressions in the absence of an XSLT stylesheet). It is an implementation of the JAXP 1.3 XPath interface, with additional methods provided (a) for backwards compatibility (b) to give extra control over the XPath evaluation, and (c) to support XPath 2.0.
| Constructor and Description |
|---|
XPathEvaluator()
Default constructor.
|
XPathEvaluator(Configuration config)
Construct an XPathEvaluator with a specified configuration.
|
XPathEvaluator(Source source)
Construct an XPathEvaluator to process a particular source document.
|
| Modifier and Type | Method and Description |
|---|---|
XPathExpression |
compile(String expr)
Compile an XPath 2.0 expression
|
XPathExpressionImpl |
createExpression(String expression)
Prepare an XPath expression for subsequent evaluation.
|
List |
evaluate(String expression)
Prepare and execute an XPath expression, supplied as a string, and returning the results
as a List.
|
String |
evaluate(String expr,
InputSource inputSource)
Single-shot method to parse and build a source document, and
compile an execute an XPath 2.0 expression, against that document,
returning the result as a string
|
Object |
evaluate(String expr,
InputSource inputSource,
QName qName)
Single-shot method to parse and build a source document, and
compile an execute an XPath 2.0 expression, against that document
|
String |
evaluate(String expr,
Object node)
Single-shot method to compile an execute an XPath 2.0 expression, returning
the result as a string.
|
Object |
evaluate(String expr,
Object node,
QName qName)
Single-shot method to compile and execute an XPath 2.0 expression.
|
Object |
evaluateSingle(String expression)
Prepare and execute an XPath expression, supplied as a string, and returning the first
item in the result.
|
NamespaceContext |
getNamespaceContext()
Get the namespace context, if one has been set using
setNamespaceContext(javax.xml.namespace.NamespaceContext) |
StandaloneContext |
getStaticContext()
Get the current static context
|
XPathFunctionResolver |
getXPathFunctionResolver()
Get the resolver for XPath functions
|
XPathVariableResolver |
getXPathVariableResolver()
Get the resolver for XPath variables
|
void |
importSchema(Source source)
Import a schema.
|
boolean |
isBackwardsCompatible()
Get the value of XPath 1.0 compatibility mode
|
static void |
main(String[] args)
A simple command-line interface for the XPathEvaluator (not documented).
|
void |
reset() |
void |
setBackwardsCompatible(boolean compatible)
Set XPath 1.0 compatibility mode on or off (by default, it is false)
|
void |
setContextNode(NodeInfo node)
Set the context node.
|
void |
setNamespaceContext(NamespaceContext namespaceContext)
Set the namespace context to be used.
|
NodeInfo |
setSource(Source source)
Supply the document against which XPath expressions are to be executed.
|
void |
setStaticContext(StandaloneContext context)
Set the static context for compiling XPath expressions.
|
void |
setStripSpace(boolean strip)
Indicate whether all whitespace text nodes in the source document are to be
removed.
|
void |
setXPathFunctionResolver(XPathFunctionResolver xPathFunctionResolver)
Set the resolver for XPath functions
|
void |
setXPathVariableResolver(XPathVariableResolver xPathVariableResolver)
Set the resolver for XPath variables
|
public XPathEvaluator()
public XPathEvaluator(Configuration config)
config - the configuration to be used. If schema-aware XPath expressions are to be used,
this must be a SchemaAwareConfiguration.public XPathEvaluator(Source source) throws XPathException
source - The source document (or a specific node within it).XPathExceptionpublic void setStripSpace(boolean strip)
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.public NodeInfo setSource(Source source) throws XPathException
source - Any javax.xml.transform.Source object representing the document against
which XPath expressions will be executed. Note that a Saxon DocumentInfo
(indeed any NodeInfo)
can be used as a Source. To use a third-party DOM Document as a source, create an instance of
DOMSource to wrap it.
The Source object supplied also determines the initial setting of the context item. In most cases the context node will be the root of the supplied document; however, if a NodeInfo or DOMSource is supplied it can be any node in the document.
XPathExceptionpublic void setStaticContext(StandaloneContext context)
public StandaloneContext getStaticContext()
public XPathExpressionImpl createExpression(String expression) throws XPathException
expression - The XPath expression to be evaluated, supplied as a string.XPathException - if the syntax of the expression is wrong, or if it references namespaces,
variables, or functions that have not been declared.public void setContextNode(NodeInfo node)
node - The node to be used as the context node. This must
be a node within the context document (the document supplied using the setSource() method).NullPointerException - if the argument is nullIllegalArgumentException - if the supplied node is not a node in the context documentpublic List evaluate(String expression) throws XPathException
expression - The XPath expression to be evaluated, supplied as a string.
XPathExceptionpublic void setBackwardsCompatible(boolean compatible)
compatible - true if XPath 1.0 compatibility mode is to be set to true, false
if it is to be set to false.public boolean isBackwardsCompatible()
public void setXPathVariableResolver(XPathVariableResolver xPathVariableResolver)
setXPathVariableResolver in interface XPathxPathVariableResolver - public XPathVariableResolver getXPathVariableResolver()
getXPathVariableResolver in interface XPathpublic void setXPathFunctionResolver(XPathFunctionResolver xPathFunctionResolver)
setXPathFunctionResolver in interface XPathxPathFunctionResolver - public XPathFunctionResolver getXPathFunctionResolver()
getXPathFunctionResolver in interface XPathpublic void setNamespaceContext(NamespaceContext namespaceContext)
setNamespaceContext in interface XPathnamespaceContext - The namespace contextpublic NamespaceContext getNamespaceContext()
setNamespaceContext(javax.xml.namespace.NamespaceContext)getNamespaceContext in interface XPathpublic void importSchema(Source source) throws SchemaException
source - A Source object identifying the schema document to be loadedSchemaException - if the schema contained in this document is invalidUnsupportedOperationException - if the configuration is not schema-awarepublic XPathExpression compile(String expr) throws XPathExpressionException
compile in interface XPathexpr - the XPath 2.0 expression to be compiled, as a stringXPathExpressionException - if there are any static errors in the expression.
Note that references to undeclared variables are not treated as static errors, because
variables are not pre-declared using this API.public Object evaluate(String expr, Object node, QName qName) throws XPathExpressionException
evaluate in interface XPathexpr - The XPath 2.0 expression to be compiled and executednode - The context node for evaluation of the expressionqName - The type of result required. For details, see
XPathExpressionImpl.evaluate(Object, javax.xml.namespace.QName)XPathExpressionImpl.evaluate(Object, javax.xml.namespace.QName)XPathExpressionException - if any static or dynamic error occurs
in evaluating the expression.public String evaluate(String expr, Object node) throws XPathExpressionException
evaluate in interface XPathexpr - The XPath 2.0 expression to be compiled and executednode - The context node for evaluation of the expressionXPathExpressionException - if any static or dynamic error occurs
in evaluating the expression.public Object evaluate(String expr, InputSource inputSource, QName qName) throws XPathExpressionException
evaluate in interface XPathexpr - The XPath 2.0 expression to be compiled and executedinputSource - The source document: this will be parsed and built into a tree,
and the XPath expression will be executed with the root node of the tree as the
context nodeqName - The type of result required. For details, see
XPathExpressionImpl.evaluate(Object, javax.xml.namespace.QName)XPathExpressionImpl.evaluate(Object, javax.xml.namespace.QName)XPathExpressionException - if any static or dynamic error occurs
in evaluating the expression.public String evaluate(String expr, InputSource inputSource) throws XPathExpressionException
evaluate in interface XPathexpr - The XPath 2.0 expression to be compiled and executedinputSource - The source document: this will be parsed and built into a tree,
and the XPath expression will be executed with the root node of the tree as the
context nodeXPathExpressionException - if any static or dynamic error occurs
in evaluating the expression.public Object evaluateSingle(String expression) throws XPathException
expression - The XPath expression to be evaluated, supplied as a string.XPathException