Forum und email

SimpleXMLElement->xpath

(PHP 5 >= 5.2.0)

SimpleXMLElement->xpath — Ejecuta una petición Xpath en la cadena XML

Descripción

array SimpleXMLElement->xpath ( string $path )

El método xpath busca en el nodo SimpleXML los hijos que emparejen con el parámetro path de Xpath. Devuelve siempre un array de objetos SimpleXMLElement.

Example#1 Xpath

<?php
$string 
= <<<XML
<a>
 <b>
  <c>text</c>
  <c>stuff</c>
 </b>
 <d>
  <c>code</c>
 </d>
</a>
XML;

$xml simplexml_load_string($string);

/* Busca en <a><b><c> */
$result $xml->xpath('/a/b/c');

while(list( , 
$node) = each($result)) {
    echo 
'/a/b/c: ',$node,"\n";
}

/* Paths relativos tambi&eacute;n funcionan... */
$result $xml->xpath('b/c');

while(list( , 
$node) = each($result)) {
    echo 
'b/c: ',$node,"\n";
}
?>

Éste script mostrará:

/a/b/c: text
/a/b/c: stuff
b/c: text
b/c: stuff

Notar que ámbos resultados son iguales.