PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

DOMXPath->registerNamespace()> <DOMXPath->evaluate()
Last updated: Sat, 24 Mar 2007

view this page in

DOMXPath->query()

(No version information available, might be only in CVS)

DOMXPath->query() — Evaluates the given XPath expression

Popis

class DOMXPath {
DOMNodeList query ( string $expression [, DOMNode $contextnode] )
}

Executes the given XPath expression.

Seznam parametrů

expression

The XPath expression to execute.

contextnode

The optional contextnode can be specified for doing relative XPath queries. By default, the queries are relative to the root element.

Návratové hodnoty

Returns a DOMNodeList containing all nodes matching the given XPath expression. Any expression which do not return nodes will return an empty DOMNodeList.

Příklady

Příklad 417. Getting all the english books

<?php

$doc
= new DOMDocument;

// We don't want to bother with white spaces
$doc->preserveWhiteSpace = false;

$doc->Load('book.xml');

$xpath = new DOMXPath($doc);

// We starts from the root element
$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";
}
?>

Výše uvedený příklad vypíše:


Found The Grapes of Wrath, by John Steinbeck
Found The Pearl, by John Steinbeck

    

We can also use the contextnode parameter to shorten our expression:

<?php

$doc
= new DOMDocument;
$doc->preserveWhiteSpace = false;

$doc->load('book.xml');

$xpath = new DOMXPath($doc);

$tbody = $doc->getElementsByTagName('tbody')->item(0);

// our query is relative to the tbody node
$query = 'row/entry[. = "en"]';

$entries = $xpath->query($query, $tbody);

foreach (
$entries as $entry) {
    echo
"Found {$entry->previousSibling->previousSibling->nodeValue}," .
        
" by {$entry->previousSibling->nodeValue}\n";
}
?>

Viz také

DOMXPath->evaluate()



DOMXPath->registerNamespace()> <DOMXPath->evaluate()
Last updated: Sat, 24 Mar 2007
 
add a note add a note User Contributed Notes
DOMXPath->query()
Hayley Watson
12-Aug-2007 05:43
Note that if your DOMDocument was loaded from HTML, where element and attribute names are case-insensitive, the DOM parser converts them all to lower-case, so your XPath queries will have to as well; '//A/@HREF' won't find anything even if the original HTML contained "<A HREF='example.com'>".
nicolas_rainardNOSPAM at yahoo dot fr
10-Jul-2007 02:04
Please note that what clochix says is valid for *any* document which has a default namespace (as it is the case for XHTML).

This document :

<?xml version="1.0" encoding="UTF-8" ?>

<root xmlns="http://www.exemple.org/namespace">

    <element id="1">
    ...
    </element>

    <element id="2">
    ...
    </element>

</element>

must be accessed this way :

$document = new DOMDocument();
$document->load('document.xml');

$xpath = new DOMXPath($document);
$xpath->registerNameSpace('fakeprefix', 'http://www.exemple.org/namespace');

$elements = $xpath->query('//fakeprefix:element');

Of course, there is no prefix in the original document, but the DOMXPath class *needs* one, whatever it is, if you use a default namespace. It *doesn't work* if you specify an empty prefix like this :

$xpath->registerNameSpace('', 'http://www.exemple.org/namespace');

Hope this help to spare some time...
clochix at clochix dot net
01-Mar-2007 04:05
If you want to perform queries on XHTML documents, you must fix a default namespace:

$doc = new DOMDocument;
$doc->preserveWhiteSpace = true;
$doc->resolveExternals = true; // for character entities
$doc->load("http://www.w3.org/");
$xpath = new DOMXPath($doc);
// won't work
$entries = $xpath->query("//div");
// you should use :
$xpath->registerNamespace("html", "http://www.w3.org/1999/xhtml");
$entries = $xpath->query("//html:div");
ckrack at i-z dot de
22-Dec-2005 07:27
tried finding a node by it's text content?

// Get all elements that equal the string "test"
$query = "//*[.='test']";
jakob dot voss at nichtich dot de
14-Nov-2005 12:33
You can transform the result nodes into new DOMDocument objects this way:

<?php
$result
= $xpath->query($query);
$resultNode = $result->item(0);
$newDom = new DOMDocument;
$newDom->appendChild($newDom->importNode($resultNode,1));

print
"<pre>" . htmlspecialchars($newDom->saveXML()) . "</pre>";
?>
Eric Hanson
12-Jul-2005 05:40
Two great XPath references follow.

XPath in Five Paragraphs (finally!):
http://www.rpbourret.com/xml/XPathIn5.htm

The w3c spec actually has a bunch of helpful examples:
http://www.w3.org/TR/xpath#location-paths

DOMXPath->registerNamespace()> <DOMXPath->evaluate()
Last updated: Sat, 24 Mar 2007
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites