DOMXPath->query()
(No version information available, might be only in CVS)
DOMXPath->query() — Valuta l'espressione XPath
Descrizione
DOMXPath
DOMNodeList query
( string $expression
[, DOMNode $contextnode
] )
Esegue la query XPath data.
Elenco dei parametri
- expression
-
La query XPath da eseguire.
- contextnode
-
Il parametro opzinale contextnode serve per indicare di eseguire query XPath relative. Per default le query sono relative all'elemento radice.
Valori restituiti
Restituisce un oggetto DOMNodeList contenente tutti i nodi che soddisfano la condizione expression . Qualsiasi espressione che non restituisce nodi creerà un DOMNodeList vuoto.
Esempi
Example#1 Ottenere il numero dei libri inglesi
<?php
$doc = new DOMDocument;
// We don't want to bother with white spaces
$doc->preserveWhiteSpace = false;
$doc->Load('book.xml');
$xpath = new DOMXPath($doc);
// Partiamo dell'elemento radice
$query = '//book/chapter/para/informaltable/tgroup/tbody/row/entry[. = "en"]';
$entries = $xpath->query($query);
foreach ($entries as $entry) {
echo "Found {$entry->previousSibling->previousSibling->nodeValue}," .
" by {$entry->previousSibling->nodeValue}\n";
}
?>
Il precedente esempio visualizzerà :
Found The Grapes of Wrath, by John Steinbeck Found The Pearl, by John Steinbeck
Possiamo utilizzare il parametro contextnode per ridurre l'espressione:
<?php
$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
$doc->load('book.xml');
$xpath = new DOMXPath($doc);
$tbody = $doc->getElementsByTagName('tbody')->item(0);
// query relativa ai nodi tbody
$query = 'row/entry[. = "en"]';
$entries = $xpath->query($query, $tbody);
foreach ($entries as $entry) {
echo "Found {$entry->previousSibling->previousSibling->nodeValue}," .
" by {$entry->previousSibling->nodeValue}\n";
}
?>