jaxp xpath evaluate method node list parameter?
The javadoc for XPath.evaluate(String, Object, QName) says that the second parameter is the context "node or node list for example". I'm familiar with the concept of an XPath context node. What does it mean to pass a node list as the second argument?
With Xalan 2.7 and this xml:
<doc><a/></a></doc>
NodeList nl = (NodeList) xp.evaluate("//*", doc, XPathConstants.NODESET);
Number n = (Number) xp.evaluate("count(self::node())", nl, XPathConstants.NUMBER);
Results in zero.
NodeList nl2 = (NodeList) xp.evaluate("self::node()", nl, XPathConstants.NODESET);
Results in an empty node list.
Message was edited by:
queshaw
[705 byte] By [
queshawa] at [2007-11-27 7:55:22]

# 1
Specify the XML document as the context node.
InputSource inputSource =
new InputSource(new FileInputStream(xmlDocument)));
xmlDocument is the java.io.File object of the XML document.
NodeSet nodes =
(NodeSet) xPath.evaluate(xpathExpression,
inputSource, XPathConstants.NODESET);
# 3
A node list is obtained with a DOM parser, not by evaluating an XPath expression. NodeList nl = (NodeList) xp.evaluate("//*", doc, XPathConstants.NODESET);Number n = (Number) xp.evaluate("count(self::node())", nl, XPathConstants.NUMBER);
# 4
The javadoc says:
Parameters:
expression - The XPath expression.
item - The starting context (node or node list, for example).
returnType - The desired return type.
When the second parameter is a node list, how do you refer to the nodes in the node list? Or is the javadoc wrong?
# 5
Since the XPath Recommendation consistently refers to the "context node", it isn't clear what it means for the context to be a node list. (As you say.) And the API documentation doesn't give any details. (As you already found out.)
I would file a bug report against the documentation. You might at least get an explanation that way.
# 8
The XPath specification specifies context node, not node list.
"The context consists of:
a node (the context node)
a pair of non-zero positive integers (the context position and the context size)
a set of variable bindings
a function library
the set of namespace declarations in scope for the expression"
http://www.w3.org/TR/xpath