Deep Deep - 1 month ago 6
Java Question

using xpath fetch top level list of nodes having specific attribute

Please provide me X-path expression for the below scenario.

The Element Tag name are not constant they can vary , also the hierarchy may vary as in the above case the path attribute is present at level 1, there might be a case it is not present at level 1 but below that.

Expected Result : NodeList nodeList = { a,z}; list should not contain the b or y nodes

<root>
<a path="a">
<b path="b"></b>
</a>
<z path="z">
<y path="y"></y>
</z>
</root>

Answer

Simply as this:

/root/*[@path]

Update

If the root name is not a constant:

/*/*[@path]
Comments