XPath search under given element

The only way I know in PHP to execute an XPath query in a DOM, DOMXPath , works only with DOMDocument :

 public __construct ( DOMDocument $doc ) 

Is there a similar search mechanism for DOMElement ?

The problem is that I need to look for abritrary XPath (which I do not control) regarding DOMElement .

I've tried:

 $domElement->getNodePath() . '/' . $xPath; 

But if XPath contains | (or symbol), this approach does not work.

+8
dom php xpath
source share
1 answer

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 request, there is a second parameter, which is the context node, to which the request in the first parameter refers to

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

See DOMXpath::query() .

+15
source share

All Articles