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, 13 Nov 2009

view this page in

DOMXPath::evaluate

(PHP 5 >= 5.1.0)

DOMXPath::evaluate 与えられた XPath 式を評価し、可能であれば結果を返す

説明

mixed DOMXPath::evaluate ( string $expression [, DOMNode $contextnode ] )

与えられた XPath 式 expression を実行し、 可能であれば型付けされた結果を返します。

パラメータ

expression

実行する XPath 式。

contextnode

相対 XPath クエリを実行する場合に、オプションで基準ノードを指定することが可能です。 デフォルトでは、クエリは root 要素に対する相対パスとなります。

返り値

可能であれば型付けされた結果、あるいは指定された XPath 式 expression にマッチするすべてのノードを含む DOMNodeList を返します。

例1 英語の書籍の数を取得する

<?php

$doc 
= new DOMDocument;

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

$xpath = new DOMXPath($doc);

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

// tbody ノードを基準とした相対クエリとなります
$query 'count(row/entry[. = "en"])';

$entries $xpath->evaluate($query$tbody);
echo 
"There are $entries english books\n";

?>

上の例の出力は以下となります。

There are 2 english books

参考



add a note add a note User Contributed Notes
DOMXPath::evaluate
daniel ad demus dk
16-Jun-2008 07:02
It seems this method is only included from PHP 5.1.
yuriucsal at NOSPAM dot yahoo dot com dot br
07-Mar-2005 05: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

           
}
        }
    }
?>

DOMXPath::query> <DOMXPath::__construct
Last updated: Fri, 13 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites