superfive33
superfive33

Reputation: 1174

Always getting the same node when doing a XPath query in a loop through DOMNodes

I don't know why the following script doesn't work !
The $_node returned within the loop is always the first child node of my root content, whereas it should be the first, the second, etc... WHY ?

<?php
// utility function
function saveNodeAsHTML($node)
{
    $dom  = new DOMDocument('1.0');
    $node = $dom->importNode($node, true);
    $dom->appendChild($node);
    return $dom->saveHTML();
}

//  root content
$content = '<table><tbody><tr><td class="item"><span>cell1</span></td><td class="item"><span>cell2</span></td></tr></tbody></table>';
echo 'XML content : ' . htmlspecialchars($content);
$dom                     = new DOMDocument();
$dom->loadHTML( $content );
$xpath = new DOMXPath( $dom );
// XPath to get all td.item nodes
$query = "//td[contains(concat(' ', @class, ' '), ' item ')]";
$nodes = $xpath->query( $query );
echo '<br/><br/>LOOP START<br/>';
// looping through td.item nodes
foreach ( $nodes as $node ) {
    echo '<br/><br/>node being parsed : ' . htmlspecialchars(saveNodeAsHTML($node));
    // looking for the span tag inside the $node context
    $_query = "//span";
    $_nodes = $xpath->query( $_query, $node );
    $_node  = $_nodes->item( 0 );
    // $_node is alwayd the first item of the loop !!!
    echo '<br/>--node value : ' . saveNodeAsHTML($_node);
}
echo '<br/><br/>LOOP END<br/>';
?>

This script will output :

XML content : <table><tbody><tr><td class="item"><span>cell1</span></td><td class="item"><span>cell2</span></td></tr></tbody></table>

LOOP START

node being parsed : <td class="item"><span>cell1</span></td> 
--node value : cell1 

node being parsed : <td class="item"><span>cell2</span></td> 
--node value : cell1

LOOP END

Upvotes: 0

Views: 254

Answers (1)

VolenD
VolenD

Reputation: 3692

As per the second comment in http://php.net/manual/en/domxpath.query.php, you have to use relative path, e.g. .//span. Otherwise, you will get all matches in the document (and item(0) is cell1 in this case).

Upvotes: 2

Related Questions