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
Last updated: Fri, 20 May 2011

view this page in

DOMXPath::evaluate

(PHP 5 >= 5.1.0)

DOMXPath::evaluate Evalúa la expresión XPath dada y devuelve un resultado escrito si es posible

Descripción

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

Ejecuta la expresión XPath dada por expression y devuelve un resultado escrito si es posible.

Parámetros

expression

La expresión XPath a ejecutar.

contextnode

El parámetro opcional contextnode se puede especificar para hacer consultas XPath relativas. Por omisión, las consultas son relativas al elemento raíz.

registerNodeNS

El parámetro opcional registerNodeNS puede ser especificado para deshabilitar el registro automático del nodo de contexto.

Valores devueltos

Devuelve un resultado escrito si es posible o un objeto DOMNodeList que contiene todos los nodos coincidentes con la expresión XPath dada por expression.

Si el parámetro expression está malformado o el parámetro contextnode es inválido, DOMXPath::evaluate() devuelve FALSE.

Historial de cambios

Versión Descripción
5.3.3 Se añadió el parámetro registerNodeNS.

Ejemplos

Example #1 Obtener el número de todos los libros en inglés

<?php

$doc 
= new DOMDocument;

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

$xpath = new DOMXPath($doc);

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

// nuestra consulta es relativa al nodo tbody
$consulta 'count(row/entry[. = "en"])';

$entradas $xpath->evaluate($consulta$tbody);
echo 
"Hay $entradas libros en inglés\n";

?>

El resultado del ejemplo sería:

Hay 2 libros en inglés

Ver también



DOMXPath::query> <DOMXPath::__construct
Last updated: Fri, 20 May 2011
 
add a note add a note User Contributed Notes DOMXPath::evaluate
Damien Bezborodov 29-Mar-2011 02: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 10:02
It seems this method is only included from PHP 5.1.
yuriucsal at NOSPAM dot yahoo dot com dot br 06-Mar-2005 08: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 | stats | sitemap | contact | advertising | mirror sites