<?php
$string = <<<XML
<a>
<b>
<c>text</c>
<c>stuff</c>
</b>
<d>
<c>code</c>
</d>
</a>
XML;
$xml = new SimpleXMLElement($string);
/* On cherche <a><b><c> */
$result = $xml->xpath('/a/b/c');
foreach ($result as $node) {
echo '/a/b/c: ',$node,"\n";
}
/* Les chemins relatifs fonctionnent aussi... */
$result = $xml->xpath('b/c');
foreach ($result as $node) {
echo 'b/c: ',$node,"\n";
}
?>
L'exemple ci-dessus va afficher :
/a/b/c: text
/a/b/c: stuff
b/c: text
b/c: stuff
Il est à noter que les deux résultats sont égaux.