©
本文檔使用 php中文網手册 發布
(PHP 5 >= 5.1.0, PHP 7)
DOMXPath::evaluate — Evaluates the given XPath expression and returns a typed result if possible
$expression
[, DOMNode $contextnode
[, bool $registerNodeNS
= true
]] )
Executes the given XPath expression
and returns
a typed result if possible.
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.
registerNodeNS
The optional registerNodeNS
can be specified to
disable automatic registration of the context node.
Returns a typed result if possible or a DOMNodeList
containing all nodes matching the given XPath expression
.
If the expression
is malformed or the
contextnode
is invalid,
DOMXPath::evaluate() returns FALSE
.
版本 | 说明 |
---|---|
5.3.3 |
The registerNodeNS parameter was added.
|
Example #1 Getting the count of all the english books
<?php
$doc = new DOMDocument ;
$doc -> load ( 'book.xml' );
$xpath = new DOMXPath ( $doc );
$tbody = $doc -> getElementsByTagName ( 'tbody' )-> item ( 0 );
// our query is relative to the tbody node
$query = 'count(row/entry[. = "en"])' ;
$entries = $xpath -> evaluate ( $query , $tbody );
echo "There are $entries english books\n" ;
?>
以上例程会输出:
There are 2 english books
[#1] aazaharov81 at gmail dot com [2015-01-14 12:48:37]
To query DOMNodes by their HTML classes, use such snippet
<?php
// CssClassXPathSelector
function ccxs($class) {
return '[contains(concat(" ", normalize-space(@class), " "), " ' . $class . ' ")]';
}
// then just
$domitems = $this->xpath("/
the severity of the error (one of the following constants:
LIBXML_ERR_WARNING
,
LIBXML_ERR_ERROR
or
LIBXML_ERR_FATAL
)
The error's code.
The column where the error occurred.
Note:
This property isn't entirely implemented in libxml and therefore 0 is often returned.
The error message, if any.
The filename, or empty if the XML was loaded from a string.
The line where the error occurred.
[#1] mati [2013-08-12 08:27:18]
If someone wanna use this to parse xml schema and validate it. Look out there is a bug
https://bugs.php.net/bug.php?id=54138
which wont be fixed.