Search by XPath under a given element Search by XPath under a given element php php

Search by XPath under a given element


Yes there is. The element is also part of the document, so you use the xpath object of the document, but when you run the query, there is a second parameter which is the context-node to which the query in the first parameter is resolved to:

// query all child-nodes of $domElement$result = $xpath->query('./*', $domElement);

Resolved means, that if the xpath is:

  1. relative, it is relative to that $domElement context-node.
  2. absolute, it resolves to the document node (still/again).

Only relative path applies in context here, which is why I prefixed it with a dot in front: ./*. A simple * would work, too, just would not make this specifically visible.

See DOMXpath::query().