Documentación offline PHP master

SimpleXMLElement::xpath

master Ver versión oficial en línea Licencia CC-BY-3.0Descargado el 2026-08-02

En esta página

SimpleXMLElement::xpath

Ejecuta una consulta XPath sobre datos XML

Descripción#

public SimpleXMLElement::xpath(string $expression): array
```php

El método `xpath` busca en el nodo SimpleXML hijos que correspondan al `expression` Xpath.

## Parámetros

`expression`  
Una ruta XPath

## Valores devueltos

Devuelve un array de objetos SimpleXMLElement en caso de éxito o `null` o `false` si ocurre un error.

## Ejemplos

Xpath
text stuff code XML; $xml = new SimpleXMLElement($string); /* Se busca */ $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"; } ?>

```php

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.

Véase también#

SimpleXMLElement::registerXPathNamespace, SimpleXMLElement::getDocNamespaces, SimpleXMLElement::getNamespaces, ???