Search by XPath under a given element

11,151

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().

Share:
11,151

Related videos on Youtube

BenMorel
Author by

BenMorel

Author of Brick, a collection of open-source libraries for PHP applications: brick/math : an arbitrary-precision arithmetic library brick/money : a money and currency library brick/date-time : a date and time library brick/phonenumber : a phone number library brick/geo : a GIS geometry library brick/varexporter : a powerful alternative to var_export() ... and a few others.

Updated on September 28, 2022

Comments

  • BenMorel
    BenMorel over 1 year

    The only way I know in PHP to perform an XPath query on the DOM is DOMXPath, which only works on a DOMDocument:

    public __construct ( DOMDocument $doc )
    

    Is there a similar mechanism to search relatively to a DOMElement?

    The problem being, I need to search an abritrary XPath (that I have no control over) relatively to a DOMElement.

    I've tried to do:

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

    But if the XPath contains a | (or character), this approach doesn't work.

    • Twisted1919
      Twisted1919 almost 11 years
      I think this is something you will like: querypath.org It's the only way i use to manipulate the DOM in php. It's very neat ;)
    • BenMorel
      BenMorel almost 11 years
      I know QueryPath, but I'd like to avoid using it if it's possible to do it with the built-in DOM only.
    • Twisted1919
      Twisted1919 almost 11 years
      QueryPath uses PHP's SimpleXML, so it's okay, go ahead and use it.
    • BenMorel
      BenMorel almost 11 years
      No, it's not ok, I know there are external libraries that do the job, I'm just trying to limit the number of dependencies!
    • BenMorel
      BenMorel almost 11 years
      Also, I need to get back an array of DOMElements that are the same instances as in the document I'm working on, not pale copies. So I can't mix DOM and another library, especially if it's based on SimpleXML.
    • hakre
      hakre almost 11 years
      @Twisted1919: This is more a comment than a real answer. The library you suggest is a good suggestion, no question, but it's just not an answer to suggest it. Turn it into a comment instead.
  • hakre
    hakre almost 11 years
    @Benjamin: No problemo, there are some catches, the path should be written relative, that is why I put the dot in front. Should give you a good start with context based xpath queries. In SimpleXML it is automatically relative to the element where the xpath method is called on.
  • BenMorel
    BenMorel almost 11 years
    That should be all right, I'm mostly using XPath expressions coming from a CSS-selector-to-XPath library, which basically all begin with something like descendant-or-self::
  • codtex
    codtex about 7 years
    @hakre my dear friend the path should be written relative this is the statement that I was search for and is really important! I was so confused why I always get all the elements when I use something like //div[@class='some row'] and now i know that to search in a specific DOMElement you need to specify relative path like ./div[@class='some row']. Thanks !