htmlxmlxpathdomxpath

Why does XPath return more elements than I expect?


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>

If it matters: I use PHP's DOMXPath.


Solution

    1. 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...

    2. 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.