note 50674 modified in function.dom-domxpath-evaluate by danbrown

From: Date: Fri, 28 Nov 2008 22:43:17 +0000
Subject: note 50674 modified in function.dom-domxpath-evaluate by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-147374@lists.php.net to get a copy of this message
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 } } } ?> --was-- 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 } } } ?> http://php.net/manual/en/function.dom-domxpath-evaluate.php

« previous php.notes (#147374) next »