Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
485 views
in Technique[技术] by (71.8m points)

php - context node in xpath problem

have this code:

$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 />';
}

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

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)

Any pointers muchly appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

From 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

and

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

Also see http://bugs.php.net/bug.php?id=34413


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...