Note that this method does not provide any means to distinguish between a successful result that returns FALSE and an error.
For example, this will succeed and return FALSE:
<?php $xpath->evaluate("1 = 0"); ?>
One workaround when you know you are expecting a Boolean is to wrap the result with string(). e.g.
<?php $xpath->evaluate("string(1 = 0)"); ?>
This will return a string "false" on success, or the Boolean FALSE on error.DOMXPath::evaluate
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
DOMXPath::evaluate
Референца за `domxpath.evaluate.php` со подобрена типографија и навигација.
DOMXPath::evaluate
(PHP 5 >= 5.1.0, PHP 7, PHP 8)
DOMXPath::evaluate — (PHP 5 >= 5.1.0, PHP 7, PHP 8)
= NULL
$expression, ?DOMNode $contextNode = null, bool $registerNodeNS = true): mixed
Ја проценува дадената XPath експресија и враќа типизиран резултат ако е можно expression Ја извршува дадената XPath
Параметри
expression-
и враќа типизиран резултат ако е можно.
contextNode-
Опционалниот
contextNodeXPath експресијата што треба да се изврши. registerNodeNS-
може да се специфицира за правење релативни XPath прашања. Стандардно, прашањата се релативни на коренскиот елемент. DOMXPath Дали автоматски да се регистрираат префиксите на просторот за имиња што се во опсег на контекстниот јазол на DOMXPath::registerPhpFunctions() објектот. Ова може да се користи за да се избегне потребата да се повика
Errors/Exceptions
класата.
Вратени вредности
рачно за секој простор за имиња што е во опсег. Кога постои конфликт на префиксите на просторот за имиња, се регистрира само префиксот на просторот за имиња на најблискиот потомок. DOMNodeList
Враќа типизиран резултат ако е можно или expression.
Ако expression што ги содржи сите јазли што одговараат на дадената XPath
contextNode е невалиден,
- Оценува даден XPath израз returns false.
Примери
е погрешно форматиран или
<?php
$doc = new DOMDocument;
$doc->load('examples/book-dcobook.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";
?>Пример #1 Пример што покажува затворачка ознака што го опфаќа последниот нов ред
There are 2 english books
Белешки од корисници 5 белешки
If your expression returns a node set, you will get a DOMNodeList instead of a typed result. Instead, try modifying your expression from "//node[1]" to "string(//node[1])".this class can substitute the method evaluate while it is not validated. Made for Yuri Bastos and Jo�o Gilberto Magalh�es.
<?php
class XPtahQuery
{
// function returns a DOMNodeList from a relative xPath
public static function selectNodes($pNode, $xPath)
{
$pos = strpos(self::getFullXpath($pNode),"/",1);
$xPathQuery = substr(self::getFullXpath($pNode),$pos);//to paste /#document[1]/
$xPathQueryFull = $xPathQuery. $xPath;
$domXPath = new DOMXPath($pNode->ownerDocument);
$rNodeList = $domXPath->query($xPathQueryFull);
return $rNodeList;
}
// function returns a DOMNode from a xPath from other DOMNode
public static function selectSingleNode($pNode, $xPath)
{
$pos = strpos(self::getFullXpath($pNode),"/",1);
$xPathQuery = substr(self::getFullXpath($pNode),$pos);//to paste /#document[1]/
$xPathQueryFull = $xPathQuery. $xPath;
$domXPath = new DOMXPath($pNode->ownerDocument);
$rNode = $domXPath->query($xPathQueryFull)->item(0);
return $rNode;
}
//utilitaries functions off selectSingleNode
private function getNodePos($pNode, $nodeName)
{
if($pNode == null)
{
return 0;
}
else
{
$var = 0;
if ($pNode->previousSibling != null)
{
if ($pNode->previousSibling->nodeName == $nodeName)
{
$var = 1;
}
}
return self::getNodePos($pNode->previousSibling, $nodeName) + $var;
}
}
//utilitaries functions off selectSingleNode
private function getFullXpath($pNode)
{
if($pNode == null)
{
return "";
}
else
{
return self::getFullXpath($pNode->parentNode) . "/" . $pNode->nodeName . "[" .strval(self::getNodePos($pNode, $pNode->nodeName)+1) . "]";//+1 to get the real xPath index
}
}
}
?>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("//*[@id='searchResultsRows']//a" . ccxs('listing_row'));
?>The only way how to distinguish FALSE returned value from syntax error FALSE is to re-run the XPath expression wrapped in string() function. If must return empty string. If it returns FALSE again then it is an error.
<?php
$ret=$this->xp->evaluate($eval, $context);
// Error detection: DOMXPath::evaluate() returns FALSE on error
// so does DOMXPath::evaluate("boolean(/nothing)")
// @workaround webdevelopers.eu
if ($ret === false && $this->xp->evaluate("string($eval)", $context) === false) {
throw new Exception("Invalid XPath expression ".json_encode($eval), 3491);
}
?>