Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php - context node in xpath problem

Tags:

php

xpath

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.

like image 984
Ian Wood Avatar asked Nov 24 '10 11:11

Ian Wood


1 Answers

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

like image 52
Gordon Avatar answered Oct 20 '22 08:10

Gordon