php-xpath问题中的上下文节点 [英] php - context node in xpath problem

查看:93
本文介绍了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-是正确的.

the number of product nodes found in the xmlfeed is 103 - that is correct.

查询该节点内的图像的查询,但是在当前节点上下文中没有这样做-它找到116个图像节点,这是Feed中图像节点的总数,因为它应该只选择当前产品中的图像(大多数情况下,介于0和3之间)

the query to locat images within that node however is NOT doing so within the current node context - it finds 116 image nodes which is the total number of image nodes within the feed where as it should only select the images within the current product (between 0 and 3 in most cases)

非常感谢任何指针.

推荐答案

您必须使用.//cf:image使其相对于上下文节点.

You have to use .//cf:image to make it relative to the context node.

来自 http://www.w3.org/TR/xpath/#path -abbrev :

//para选择文档根目录的所有para后代,并因此选择同一文档中的所有para元素作为上下文节点

//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选择上下文节点的para元素后代

.//para selects the para element descendants of the context node

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

这篇关于php-xpath问题中的上下文节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆