PHP Doku:: Evaluates the given XPath expression and returns a typed result if possible. - domxpath.evaluate.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzXML-ManipulationDocument Object ModelThe DOMXPath classDOMXPath::evaluate

Ein Service von Reinhard Neidl - Webprogrammierung.

The DOMXPath class

<<DOMXPath::__construct

DOMXPath::query>>

DOMXPath::evaluate

(PHP 5 >= 5.1.0)

DOMXPath::evaluate Evaluates the given XPath expression and returns a typed result if possible.

Beschreibung

mixed DOMXPath::evaluate ( string $expression [, DOMNode $contextnode [, boolean $registerNodeNS = true ]] )

Executes the given XPath expression and returns a typed result if possible.

Parameter-Liste

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.

Rückgabewerte

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.

Changelog

Version Beschreibung
5.3.3 The registerNodeNS parameter was added.

Beispiele

Beispiel #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";

?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

There are 2 english books

Siehe auch


2 BenutzerBeiträge:
- Beiträge aktualisieren...
daniel ad demus dk
16.06.2008 12:02
It seems this method is only included from PHP 5.1.
yuriucsal at NOSPAM dot yahoo dot com dot br
6.03.2005 21:55
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

           
}
        }
    }
?>



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)