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);

dvohra09a at 2007-7-12 19:36:44 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
Thanks. But, my question is specifically about passing a NodeList as the second parameter. The second parameter is an Object, and the javadoc says it is the context which is a node or node list (as examples). How is it used when the second parameter is a node list?
queshawa at 2007-7-12 19:36:44 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 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);
dvohra09a at 2007-7-12 19:36:44 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 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?

queshawa at 2007-7-12 19:36:44 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 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.

DrClapa at 2007-7-12 19:36:44 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6
I see that the javadoc in SE 6 now says "a node, for example". So they seem to have removed the wording "node or node list".
queshawa at 2007-7-12 19:36:44 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7
Hmm. So my "bug" is fixed. Still pretty shoddy design, though.
DrClapa at 2007-7-12 19:36:44 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 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

dvohra09a at 2007-7-12 19:36:44 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...