Why does //div["someClass"]/p[1]
also return p
-tags not wrapped in the requested div-element using XPath? Only //*/div["someClass"]/p[1]
returns the expected result with the example HTML:
<p>Foo</p>
<div class="someClass">
<p>Bar</p>
</div>
//div["someClass"]/p[1]
also returns the first Foo p-tag.//*/div["someClass"]/p[1]
only returns the expected Bar.If it matters: I use PHP's DOMXPath
.
XPath is only defined over well-formed XML, and your posted example is not well-formed because it has multiple roots. Perhaps your actual XML/HTML has a single root and other unposted markup; then there's also another consideration...
See How to select the first element via XPath? to avoid a common gotcha when trying to select the first element in XPath:
//x[1]
means to select the x
elements that are the first child of its parent, not the first x
in the document; (//x)[1]
will select the first x
element in the document.