有这个代码:
$products = $feed->_xpath->query( "//cf:vehicle" );
foreach( $products as $product )
{
echo $product->nodeName . ': ' . $product->getAttribute('code') . '<br />';
$imgs = $feed->_xpath->query( "//cf:image" , $product );
echo ' Imgs: ' . $imgs->length . '<br />';
}
在 xmlfeed 中找到的产品节点数是 103 - 这是正确的。
在该节点内定位图像的查询并没有在当前节点上下文中这样做——它找到了 116 个图像节点,这是 feed 中图像节点的总数,因为它应该只选择当前产品中的图像(大多数情况下在 0 到 3 之间)
非常感谢任何指点。
最佳答案
您必须使用 .//cf:image
使其相对于上下文节点。
来自 http://www.w3.org/TR/xpath/#path-abbrev :
//para selects all the para descendants of the document root and thus selects all para elements in the same document as the context node
和
.//para selects the para element descendants of the context node
关于php - xpath 问题中的上下文节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4266091/