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

search for in the

DOMXPath::query> <DOMXPath::__construct
[edit] Last updated: Fri, 10 Feb 2012

view this page in

DOMXPath::evaluate

(PHP 5 >= 5.1.0)

DOMXPath::evaluate Évalue une expression XPath donnée et retourne un résultat écrit si possible

Description

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

Exécute l'expression XPath expression et retourne un résultat écrit si possible.

Liste de paramètres

expression

L'expression XPath à exécuter.

contextnode

Le paramètre optionnel contextnode peut être spécifié pour effectuer des requêtes XPath relatives. Par défaut, les requêtes sont relatives à l'élément root.

registerNodeNS

Le paramètre optionnel registerNodeNS peut être renseigné pour désactiver l'enregistrement automatique du noeud de contexte.

Valeurs de retour

Retourne un résultat écrit si possible ou un DOMNodeList contenant tous les noeuds correspondant à 'expression XPath expression.

Si le paramètre expression est mal formé ou bien si le paramètre contextnode est invalide, la méthode DOMXPath::evaluate() retournera FALSE.

Historique

Version Description
5.3.3 Ajout du paramètre registerNodeNS.

Exemples

Exemple #1 Récupération du nombre total de livres anglais

<?php

$doc 
= new DOMDocument;

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

$xpath = new DOMXPath($doc);

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

// notre requête est relative au noeud tbody
$query 'count(row/entry[. = "en"])';

$entries $xpath->evaluate($query$tbody);
echo 
"Il y a $entries livres anglais\n";

?>

L'exemple ci-dessus va afficher :

Il y a 2 livres anglais

Voir aussi



DOMXPath::query> <DOMXPath::__construct
[edit] Last updated: Fri, 10 Feb 2012
 
add a note add a note User Contributed Notes DOMXPath::evaluate
daniel at danielnorton dot com 25-Jun-2011 11:56
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.
Damien Bezborodov 28-Mar-2011 08:13
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])".
daniel ad demus dk 16-Jun-2008 05:02
It seems this method is only included from PHP 5.1.
yuriucsal at NOSPAM dot yahoo dot com dot br 06-Mar-2005 01: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

           
}
        }
    }
?>

 
show source | credits | sitemap | contact | advertising | mirror sites