Take the 2-minute tour ×

I have a NodeList in Java and this XML file, (just a part of it)

if I have

<Book title="aaa" author="bbb" price="12" year="2011" />

I can use an XPath expression to see if some conditions are verified, for example:

local-name(.) = 'Book' and @title local-name(.) = 'Book' and @title

If the result of XPath expression execution is true...do something, otherwise do something else. In this case I built an expression directly on a Node, and that's ok.

In the case of NodeList, how can I do the same task? I would like to see if NodeList (org.w3c.NodeList in Java) has some nodes, for example, but using an XPath expression.

Thanks Luca

share|improve this question
Good question, +1. Yes, generally XPath can be used to select nodes from a given node-set, using a variable that contains the node-set or another XPath expression that selects the nodes of this node-set. –  Dimitre Novatchev Sep 14 '11 at 13:07
add comment

2 Answers

You can loop over nodes in the NodeList and apply your xpath to each node.

share|improve this answer
add comment

If the node-set is in a variable $v, then the following XPath expression:

$v[self::Book and @title]

selects all nodes from $v that are Book elements and that also have an attribute named title.

If the node-set can be obtained as the result of evaluation a particular XPath expression, then you can substitute $v with this expression:

yourExpression[self::Book and @title]

For example, if the node-set in question consists of all nodes returned by /*/*, then one expression that selects the wanted nodes from this node-set is:

/*/*[self::Book and @title]
share|improve this answer
add comment

Your Answer


