php - xpath 问题中的上下文节点

标签 php xpath

有这个代码:

$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 '&nbsp;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

另见 http://bugs.php.net/bug.php?id=34413

关于php - xpath 问题中的上下文节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4266091/

相关文章:

java - Selenium web自动化,获取特定的HTML元素

javascript - 如何使用php将mysql查询加载到chartjs?

php - session 一直让我注销

php - Laravel 5.3 日期验证器 : equal to or after start_date

sql-server - TSQL - XML 查询帮助

c# - slowcheetah 转换配置文件中元素的值

php - 如何获取当前日期和时间 YYYY-MM-DD HH : MM? LARAVEL 4

php - Laravel Eloquent 加入奇怪的查询

xml - xpath: 从 xml 中选择多个子元素属性

xslt - XPath - 查询两个 XML 文档