在通过 DOMNodes 循环执行 XPath 查询时,始终获取相同的节点


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

我不知道为什么下面的脚本不起作用!
循环中返回的 $_node 始终是我的根内容的第一个子节点,而它应该是第一个、第二个等......为什么?

<?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/>';
?>

此脚本将输出:

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

根据 http://php.net/manual/en/domxpath.query.php 中的第二条注释,您必须使用相对路径,例如 .//span .否则,您将获得文档中的所有匹配项(在这种情况下item(0) cell1)。