public interface XPath
Most of the evaluation methods take a context object. This is typically a
node or node-set object (which is typically a List
of node objects) or a Jaxen Context
object.
A null context is allowed, meaning that
there are no XML nodes on which to evaluate.
XPath for dom4j
,
XPath for JDOM
,
XPath for W3C DOM
Modifier and Type | Method and Description |
---|---|
void |
addNamespace(String prefix,
String uri)
Add a namespace prefix-to-URI mapping for this XPath
expression.
|
boolean |
booleanValueOf(Object context)
Retrieve the boolean value of the first node in document order
returned by this XPath expression when evaluated in
the given context.
|
Object |
evaluate(Object context)
Evaluate this XPath against the given context.
|
FunctionContext |
getFunctionContext()
Retrieve the
FunctionContext used by this XPath
expression. |
NamespaceContext |
getNamespaceContext()
Retrieve the
NamespaceContext used by this XPath
expression. |
Navigator |
getNavigator()
Retrieve the XML object-model-specific
Navigator
used to evaluate this XPath expression. |
VariableContext |
getVariableContext()
Retrieve the
VariableContext used by this XPath
expression. |
Number |
numberValueOf(Object context)
Retrieve the number-value of the first node in document order
returned by this XPath expression when evaluated in
the given context.
|
List |
selectNodes(Object context)
Select all nodes that are selectable by this XPath
expression.
|
Object |
selectSingleNode(Object context)
Return the first node in document order that is selected by this
XPath expression.
|
void |
setFunctionContext(FunctionContext functionContext)
Set a
FunctionContext for this XPath
expression. |
void |
setNamespaceContext(NamespaceContext namespaceContext)
Set a
NamespaceContext for this
XPath expression. |
void |
setVariableContext(VariableContext variableContext)
Set a
VariableContext for this XPath
expression. |
String |
stringValueOf(Object context)
Retrieve a string-value interpretation of this XPath
expression when evaluated against the given context.
|
Object evaluate(Object context) throws JaxenException
The context of evaluation may be a document, an element, or a set of elements.
If the expression evaluates to an XPath string, number, or boolean
type, then the equivalent Java object type is returned.
Otherwise, if the result is a node-set, then the returned value is a
List
.
When using this method, one must be careful to
test the class of the returned objects, and of
each of the composite members if a List
is returned. If the returned members are XML nodes,
they will be the actual Document
,
Element
or Attribute
objects
as defined by the concrete XML object-model implementation,
directly from the context document. This method does not
return copies of anything. It merely returns
references to nodes within the source document.
context
- the node, node-set or Context object for evaluation.
This value can be null.JaxenException
- if an error occurs while attempting
to evaluate the expressionString stringValueOf(Object context) throws JaxenException
The string-value of the expression is determined per
the string(..)
core function as defined
in the XPath specification. This means that an expression
that selects more than one nodes will return the string value
of the first node in the node set..
context
- the node, node-set or Context object for evaluation.
This value can be nullJaxenException
- if an error occurs while attempting
to evaluate the expressionboolean booleanValueOf(Object context) throws JaxenException
The boolean-value of the expression is determined per
the boolean()
function defined
in the XPath specification. This means that an expression
that selects zero nodes will return false
,
while an expression that selects one or more nodes will
return true
. An expression that returns a string
returns false for empty strings and true for all other strings.
An expression that returns a number
returns false for zero and true for non-zero numbers.
context
- the node, node-set or Context object for evaluation. This value can be null.JaxenException
- if an error occurs while attempting
to evaluate the expressionNumber numberValueOf(Object context) throws JaxenException
The number-value of the expression is determined per
the number(..)
core function as defined
in the XPath specification. This means that if this
expression selects multiple nodes, the number-value
of the first node is returned.
context
- the node, node-set or Context object for evaluation. This value can be null.JaxenException
- if an error occurs while attempting
to evaluate the expressionList selectNodes(Object context) throws JaxenException
NOTE: In most cases, nodes will be returned
in document-order, as defined by the XML Canonicalization
specification. The exception occurs when using XPath
expressions involving the union
operator
(denoted with the pipe '|' character).
context
- the node, node-set or Context object for evaluation.
This value can be null.JaxenException
- if an error occurs while attempting
to evaluate the expressionselectSingleNode(java.lang.Object)
Object selectSingleNode(Object context) throws JaxenException
Return the first node in document order that is selected by this XPath expression.
context
- the node, node-set or Context object for evaluation.
This value can be null.JaxenException
- if an error occurs while attempting
to evaluate the expressionselectNodes(java.lang.Object)
void addNamespace(String prefix, String uri) throws JaxenException
Namespace prefix-to-URI mappings in an XPath are independent of those used within any document. Only the mapping explicitly added to this XPath will be available for resolving the XPath expression.
This is a convenience method for adding mappings to the
default NamespaceContext
in place for this XPath.
If you have installed a specific custom NamespaceContext
,
then this method will throw a JaxenException
.
prefix
- the namespace prefixuri
- the namespace URIJaxenException
- if a NamespaceContext
used by this XPath has been explicitly installedvoid setNamespaceContext(NamespaceContext namespaceContext)
NamespaceContext
for this
XPath expression.
A NamespaceContext
is responsible for translating
namespace prefixes within the expression into namespace URIs.
namespaceContext
- the NamespaceContext
to
install for this expressionNamespaceContext
,
NamespaceContext.translateNamespacePrefixToUri(java.lang.String)
void setFunctionContext(FunctionContext functionContext)
FunctionContext
for this XPath
expression.
A FunctionContext
is responsible for resolving
all function calls used within the expression.
functionContext
- the FunctionContext
to
install for this expressionFunctionContext
,
FunctionContext.getFunction(java.lang.String, java.lang.String, java.lang.String)
void setVariableContext(VariableContext variableContext)
VariableContext
for this XPath
expression.
A VariableContext
is responsible for resolving
all variables referenced within the expression.
variableContext
- the VariableContext
to
install for this expression.VariableContext
,
VariableContext.getVariableValue(java.lang.String, java.lang.String, java.lang.String)
NamespaceContext getNamespaceContext()
NamespaceContext
used by this XPath
expression.
A FunctionContext
is responsible for resolving
all function calls used within the expression.
If this XPath expression has not previously had a NamespaceContext
installed, a new default NamespaceContext
will be created,
installed and returned.
NamespaceContext
used by this expressionNamespaceContext
FunctionContext getFunctionContext()
FunctionContext
used by this XPath
expression.
A FunctionContext
is responsible for resolving
all function calls used within the expression.
If this XPath expression has not previously had a FunctionContext
installed, a new default FunctionContext
will be created,
installed and returned.
FunctionContext
used by this expressionFunctionContext
VariableContext getVariableContext()
VariableContext
used by this XPath
expression.
A VariableContext
is responsible for resolving
all variables referenced within the expression.
If this XPath expression has not previously had a VariableContext
installed, a new default VariableContext
will be created,
installed and returned.
VariableContext
used by this expressionVariableContext
Copyright 2001–2022 The Jaxen Project. All rights reserved.