(PHP 5, PHP 7, PHP 8)
SimpleXMLElement::xpath — Ejecuta una consulta XPath sobre datos XML
El método xpath busca en el nodo SimpleXML
hijos que correspondan al expression
Xpath.
expressionUna ruta XPath
Devuelve un array de objetos SimpleXMLElement en caso de éxito o null
o false si ocurre un error.
Ejemplo #1 Xpath
<?php
$string = <<<XML
<a>
<b>
<c>text</c>
<c>stuff</c>
</b>
<d>
<c>code</c>
</d>
</a>
XML;
$xml = new SimpleXMLElement($string);
/* Se busca <a><b><c> */
$result = $xml->xpath('/a/b/c');
foreach ($result as $node) {
echo '/a/b/c: ',$node,"\n";
}
/* Las rutas relativas también funcionan... */
$result = $xml->xpath('b/c');
foreach ($result as $node) {
echo 'b/c: ',$node,"\n";
}
?>El ejemplo anterior mostrará :
/a/b/c: text /a/b/c: stuff b/c: text b/c: stuff
Observe que los dos resultados son iguales.