XPath is becoming an important part of a variety of many specifications including XForms, XPointer, XSL, CSS, and so on. It is also a clear advantage for user applications which use DOM to be able to use XPath expressions to locate nodes automatically and declaratively. But liveness issues have plagued each attempt to get a list of DOM nodes matching specific criteria, as would be expected for an XPath API. There have also traditionally been model mismatches between DOM and XPath. This proposal specifies new interfaces and approaches to resolving these issues.
This section considers the differences between the Document Object Model and the XPath model as defined by [XPath 1.0].
The XPath model relies on the XML Information Set [XML Information set]
ands represents Character Information Items in a single logical
text node where DOM may have multiple fragmented Text
nodes due to cdata sections, entity references, etc. Instead of
returning multiple nodes where XPath sees a single logical text
node, only the first non-empty DOM Text node of any
logical XPath text will be returned in the node set. Applications
using XPath in an environment with fragmented text nodes must
manually gather the text of a single logical text node from
multiple nodes beginning with the first Text node
identified by the implementation.
Note: In an attempt to better implement the XML
Information Set, DOM Level 3 Core [DOM Level 3 Core] adds the attribute
wholeText on the Text interface for
retrieving the whole text for logically-adjacent Text
nodes and the method replaceWholeText for replacing
those nodes.
The XPath model expects namespace nodes for each in-scope
namespace to be attached to each element. DOM and certain
other W3C Information Set conformant implementations only maintain
the declaration of namespaces instead of replicating them on each
Element where they are in-scope. The DOM
implementation of XPath returns a new node of type
XPATH_NAMESPACE_NODE to properly preserve identity and
ordering. This node type is only visible using the XPath evaluation
methods.
The document order of nodes in the DOM Core has been defined to
be compatible with the XPath document order. According to XPath,
namespace nodes are unordered with respect to other namespace nodes
of the same element and precede attribute and child nodes of the
same element. The DOM core must order the non-core XPathNamespace nodes
in this way if the XPath DOM module is supported.
A DOM application may use the hasFeature(feature,
version) method of the DOMImplementation
interface with parameter values "XPath" and "3.0" (respectively) to
determine whether or not the event module is supported by the
implementation. In order to fully support this module, an
implementation must also support the "Core" feature defined in the
DOM Level 3 Core specification [DOM Level 3 Core]. Please, refer to
additional information about conformance in the DOM Level 3 Core
specification [DOM
Level 3 Core].
A new exception has been created for exceptions specific to these XPath interfaces.
exception XPathException {
unsigned short code;
};
// XPathExceptionCode
const unsigned short INVALID_EXPRESSION_ERR = 1;
const unsigned short TYPE_ERR = 2;
INVALID_EXPRESSION_ERRXPathEvaluator or
contains namespace
prefixes which are not in scope according to the specified
XPathNSResolver. If
the XPathEvaluator was
obtained by casting the document, the expression must be XPath 1.0
with no special extension functions.TYPE_ERRThe evaluation of XPath expressions is performed by
XPathEvaluator, which will provide evaluation of XPath
1.0 expressions with no specialized extension functions or
variables. It is expected that the XPathEvaluator
interface will be implemented on the same object which implements
the Document interface in an implementation which
supports the XPath DOM module. XPathEvaluator
implementations may be available from other sources that may
provide support for new versions of XPath or special extension
functions or variables which are not defined in this
specification.
interface XPathEvaluator {
XPathExpression createExpression(in DOMString expression,
in XPathNSResolver resolver)
raises(XPathException);
XPathResult createResult();
XPathNSResolver createNSResolver(in Node nodeResolver);
XPathResult evaluate(in DOMString expression,
in Node contextNode,
in XPathNSResolver resolver,
in unsigned short type,
in XPathResult result)
raises(XPathException);
XPathResult evaluateExpression(in XPathExpression expression,
in Node contextNode,
in unsigned short type,
in XPathResult result)
raises(XPathException);
};
createExpressionexpression of type
DOMStringresolver of type XPathNSResolverresolver permits translation of prefixes
within the XPath expression into appropriate namespace URIs. If
this is specified as null, any namespace prefix
within the expression will result in XPathException being
thrown with the code INVALID_EXPRESSION_ERR.|
The compiled form of the XPath expression. |
|
INVALID_EXPRESSION_ERR: Raised if the expression is not legal
according to the rules of the |
createNSResolvernodeResolver of type
Node|
|
createResultXPathEvaluator so that a new one is not created on
each call to an evaluation method.
|
An empty |
evaluateexpression of type
DOMStringcontextNode of type
Nodecontext is context node for the evaluation of
this XPath expression.resolver of type XPathNSResolverresolver permits translation of prefixes
within the XPath expression into appropriate namespace URIs. If
this is specified as null, any namespace prefix
within the expression will result in XPathException being
thrown with the code INVALID_EXPRESSION_ERR.type of type unsigned
shorttype is specified, then the result
will be coerced to return the specified type relying on XPath
conversions and fail if the desired coercion is not possible. This
must be one of the type codes of XPathResult.result of type XPathResultresult specifies a specific XPathResult to be
reused and returned by this method. If this is specified as null, a
new XPathResult will be
constructed and returned. Any XPathResult which was not
created by this XPathEvaluator may be ignored as
though a null were passed as the parameter.|
The result of the evaluation of the XPath expression. |
|
INVALID_EXPRESSION_ERR: Raised if the expression is not legal
according to the rules of the TYPE_ERR: Raised if the result cannot be converted to return the specified type. |
evaluateExpressionexpression of type XPathExpressioncontextNode of type
Nodecontext is context node for the evaluation of
this XPath expression.type of type unsigned
shorttype is specified, then the result
will be coerced to return the specified type relying on XPath
conversions and fail if the desired coercion is not possible. This
must be one of the type codes of XPathResult.result of type XPathResultresult specifies a specific XPathResult to be
reused and returned by this method. If this is specified as null, a
new XPathResult will be
constructed and returned. Any XPathResult which was not
created by this XPathEvaluator may be ignored as
though a null were passed as the parameter.|
The result of the evaluation of the XPath expression. |
|
TYPE_ERR: Raised if the result cannot be converted to return the specified type. |
The XPathExpression interface represents a parsed
and resolved XPath expression.
interface XPathExpression {
};
The XPathNSResolver interface permit
prefix strings in the expression to be properly bound
to namespaceURI strings. XPathEvaluator can
construct an implementation of XPathNSResolver from a
node, or the interface may be implemented by any application.
interface XPathNSResolver {
DOMString lookupNamespaceURI(in DOMString prefix);
};
lookupNamespaceURIprefix of type
DOMString|
|
Returns the associated namespace URI or
|
The XPathResult interface represents the result of
the evaluation of an XPath expression within the context of a
particular node. Since evaluation of an XPath expression can result
in various result types, this object makes it possible to discover
and manipulate the type and value of the result.
interface XPathResult {
// XPathResultType
const unsigned short ANY_TYPE = 0;
const unsigned short NUMBER_TYPE = 1;
const unsigned short STRING_TYPE = 2;
const unsigned short BOOLEAN_TYPE = 3;
const unsigned short NODE_SET_TYPE = 4;
const unsigned short SINGLE_NODE_TYPE = 5;
readonly attribute unsigned short resultType;
readonly attribute double numberValue;
// raises(XPathException) on retrieval
readonly attribute DOMString stringValue;
// raises(XPathException) on retrieval
readonly attribute boolean booleanValue;
// raises(XPathException) on retrieval
readonly attribute Node singleNodeValue;
// raises(XPathException) on retrieval
XPathSetIterator getSetIterator(in boolean ordered)
raises(XPathException,
DOMException);
XPathSetSnapshot getSetSnapshot(in boolean ordered)
raises(XPathException,
DOMException);
};
An integer indicating what type of result this is.
ANY_TYPEBOOLEAN_TYPENODE_SET_TYPENUMBER_TYPESINGLE_NODE_TYPENODE_SET_TYPE).NODE_SET_TYPE
should be processed using an ordered iterator, because there is no
order guarantee for a single node.STRING_TYPEbooleanValue of type
boolean, readonly|
TYPE_ERR: raised if |
numberValue of type
double, readonly|
TYPE_ERR: raised if |
resultType of type
unsigned short, readonlysingleNodeValue of type
Node, readonly|
TYPE_ERR: raised if |
stringValue of type
DOMString, readonly|
TYPE_ERR: raised if |
getSetIteratorXPathSetIterator
which may be used to iterate over the nodes of the set of this
result.
ordered of type
boolean|
An |
|
TYPE_ERR: raised if |
|
|
|
INVALID_STATE_ERR: The document has been mutated since the result was returned. |
getSetSnapshotXPathSetSnapshot
which lists the nodes of the set of this result. Unlike an
iterator, after the snapshot has been requested, document mutation
does not invalidate it.
ordered of type
boolean|
An |
|
TYPE_ERR: raised if |
|
|
|
INVALID_STATE_ERR: The document has been mutated since the result was returned. |
The XPathSetIterator interface iterates the node
set resulting from evaluation of an XPath expression.
interface XPathSetIterator {
Node nextNode()
raises(DOMException);
};
nextNodeXPathResult node set. If
there are no more nodes in the set to be returned by the iterator,
this method returns null.
|
|
Returns the next node. |
|
|
INVALID_STATE_ERR: The document has been mutated since the node set result was returned. |
The XPathSetSnapshot interface lists the node set
resulting from an evaluation of an XPath expression as a static
list that is not invalidated or changed by document mutation.
The individual nodes of a XPathSetSnapshot may be
manipulated in the hierarchy and these changes are seen immediately
by users referencing the nodes through the snapshot.
interface XPathSetSnapshot {
Node item(in unsigned long index);
readonly attribute unsigned long length;
};
length of type
unsigned long, readonly0 to length-1 inclusive.itemindexth item in the
collection. If index is greater than or equal to the
number of nodes in the list, this method returns null.
index of type
unsigned long|
|
The node at the |
The XPathNamespace interface is returned by XPathResult
interfaces to represent the XPath namespace node type that DOM
lacks. There is no public constructor for this node type. Attempts
to place it into a hierarchy or a NamedNodeMap result in a
DOMException with the code
HIERARCHY_REQUEST_ERR. This node is read only, so methods
or setting of attributes that would mutate the node result in a
DOMException with the code
NO_MODIFICATION_ALLOWED_ERR.
The core specification describes attributes of the
Node interface that are different for different node
node types but does not describe XPATH_NAMESPACE_NODE,
so here is a description of those attributes for this node type.
All attributes of Node not described in this section
have a null or false value.
ownerDocument matches the
ownerDocument of the ownerElement even if
the element is later adopted.
prefix is the prefix of the namespace represented
by the node.
NodeName is the same as prefix.
NodeType is equal to
XPATH_NAMESPACE_NODE.
namespaceURI is the namespace URI of the namespace
represented by the node.
adoptNode, cloneNode, and
importNode fail on this node type by raising a
DOMException with the code
NOT_SUPPORTED_ERR.
interface XPathNamespace : Node {
// XPathNodeType
const unsigned short XPATH_NAMESPACE_NODE = 13;
readonly attribute Element ownerElement;
};
An integer indicating which type of node this is.
Note: There is currently only one type of node which is specific to XPath. The numbers in this list must not collide with the values assigned to core node types.
XPATH_NAMESPACE_NODENamespace.ownerElement of type
Element, readonlyElement on which the namespace was in scope
when it was requested. This does not change on a returned namespace
node even if the document changes such that the namespace goes out
of scope on that element and this node is no
longer found there by XPath.