Función xpath() PHP

Definición y uso

La función xpath() ejecuta una consulta XPath en el documento XML.

Si tiene éxito, devuelve un array que contiene un objeto SimpleXMLElement. Si falla, devuelve false.

Sintaxis

class SimpleXMLElement
{
string xpath(ruta)
}
Parámetros Descripción
ruta Obligatorio. Ruta de XPath.

Ejemplo

Archivo XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>George</to>
<from>John</from>
<heading>Recordatorio</heading>
<body>¡No olvide la reunión!</body>
</note>

Código PHP:

<?php
$xml = simplexml_load_file("test.xml");
$result = $xml->xpath("from");
print_r($result);
?>

Salida:

Array
(
[0] => SimpleXMLElement Object
  (
  [0] => John
  )
)