title: DomXPath: Search inside DomXPath query result?
body: <p>How can I query an xpath result for a sub element, without querying the entire document again?</p>
  
  <p>I query my document to find the last <code>&lt;li&gt;</code> with the class <code>menu-item</code>:</p>
  
  <pre><code>$doc = new DomDocument();
  @$doc-&gt;loadHTML( $html );
  // $html is invalid (and should be at this point),
  // so use @ sign to suppress errors
  $xpath = new DomXPath( $doc );
  $li = $xpath-&gt;query( "(//li[contains(concat(' ', normalize-space(@class), ' '), 'menu-item')])[last()]" )-&gt;item( 0 );
  </code></pre>
  
  <p>The above works just fine, now I want to query <code>$li</code> to find the following:</p>
  
  <pre><code>$p = $xpath-&gt;query( "//p[contains(concat(' ', normalize-space(@class), ' '), 'field-description')]" )-&gt;item( 0 );
  </code></pre>
  
  <p>A <code>p</code> tag with class <code>field-description</code>. This does not work and returns the first instance found, if I modify it and use the <code>[last()]</code> (whatever this is) then it works, but it's not necessarily the optimal solution here:</p>
  
  <pre><code>$p = $xpath-&gt;query( "(//p[contains(concat(' ', normalize-space(@class), ' '), 'field-description')])[last()]" )-&gt;item( 0 );
  </code></pre>
  
  <p>The above line works, but I want to query inside <code>$li</code> not the entire document again.</p>