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