title: How can I execute XPath queries on DOMElements using PHP?
body: <p>I'm trying to do Xpath queries on DOMElements but it doesn't seem to work. Here is the code</p>
  
  <pre><code>&lt;html&gt;
      &lt;div class="test aaa"&gt;
          &lt;div&gt;&lt;/div&gt;
          &lt;div class="link"&gt;contains a link&lt;/div&gt;
          &lt;div&gt;&lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="test bbb"&gt;
          &lt;div&gt;&lt;/div&gt;
          &lt;div&gt;&lt;/div&gt;
          &lt;div class="link"&gt;contains a link&lt;/div&gt;
      &lt;/div&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>What I'm doing is this:</p>
  
  <pre><code>$dom = new DOMDocument();
  $html = file_get_contents("file.html");
  @$dom-&gt;loadHTML($html);
  $xpath = new DOMXPath($dom);
  $entries = $xpath-&gt;query("//div[contains(@class,'test')]");
  if (!$entries-&gt;length &gt; 0) {
      echo "Nothing\n";
  } else {
    foreach ($entries as $entry) {
        $link = $xpath-&gt;query('/div[@class=link]',$entry);
        echo $link-&gt;item(0)-&gt;nodeValue;
        // =&gt; PHP Notice:  Trying to get property of non-object
    }
  }
  </code></pre>
  
  <p>Everything works fine up to <code>$xpath-&gt;query('/div[@class=link], $entry);</code>. I don't know how to use Xpath on a particular DOMElement ($entry).</p>
  
  <p><strong>How can I use xpath queries on DOMElement?</strong></p>