Xpath actually knows which element is root regardless of element on which you execute it. Example:
<?php
$string = <<<XML
<a>
<b>
<c>text</c>
<c>stuff</c>
</b>
<b>
<c>code</c>
</b>
</a>
XML;
header('content-type: text/plain');
$xml = new SimpleXMLElement($string);
$b0=$xml->b[0]->xpath('//c');
while(list( , $node) = each($b0)) {
echo 'b[0]: //c: ',$node,"\n";
}
$b1=$xml->b[1]->xpath('//c');
while(list( , $node) = each($b1)) {
echo 'b[1]: //c: ',$node,"\n";
}
echo "\n";
$b0=$xml->b[0]->xpath('.//c');
while(list( , $node) = each($b0)) {
echo 'b[0]: .//c: ',$node,"\n";
}
$b1=$xml->b[1]->xpath('.//c');
while(list( , $node) = each($b1)) {
echo 'b[1]: .//c: ',$node,"\n";
}
?>
Will return:
b[0]: //c: text
b[0]: //c: stuff
b[0]: //c: code
b[1]: //c: text
b[1]: //c: stuff
b[1]: //c: code
b[0]: .//c: text
b[0]: .//c: stuff
b[1]: .//c: code