Funzione xpath() PHP

Definizione e uso

La funzione xpath() esegue una query XPath sul documento XML.

Se ha successo, restituisce un array contenente oggetti SimpleXMLElement. Se fallisce, restituisce false.

Sintassi

class SimpleXMLElement
{
string xpath(path)
}
Parametro Descrizione
path Obbligatorio. Percorso XPath.

Esempio

File XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>George</to>
<from>John</from>
<heading>Ricordatorio</heading>
<body>Non dimenticare l'incontro!</body>
</note>

Codice PHP:

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

Output:

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