xpath search on subtree

.//nodeName will search for a nodeName element anywhere within the given context node.


You need to distinguish between an absolute and relative XPath expression.

Good question +1.

In XPath, any expression that starts with / is absolute XPath expression. An absolute XPath expression is evaluated on the complete current document.

By contrast, a relative XPath expression is evaluated off the current (context) node.

This explains the reported problem: //nodeName is an absolute XPath expression.

What you want is a relative XPath expression, such as:

.//nodeName

You're looking on the // axis which means 'any descendant node of the document root',

Change it to .// axis (descendands of the context node) and it will work as expected.

Tags:

Java

Xml

Xpath