|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 /* 00003 Requires PHP5, uses built-in DOM extension. 00004 To be used in PHP4 scripts using DOMXML extension: allows PHP4/DOMXML scripts to run on PHP5/DOM. 00005 (Optional: requires PHP5/XSL extension for domxml_xslt functions, PHP>=5.1 for XPath evaluation functions, and PHP>=5.1/libxml for DOMXML error reports) 00006 00007 Typical use: 00008 { 00009 if (PHP_VERSION>='5') 00010 require_once('domxml-php4-to-php5.php'); 00011 } 00012 00013 Version 1.21.1a, 2009-03-13, http://alexandre.alapetite.fr/doc-alex/domxml-php4-php5/ 00014 00015 ------------------------------------------------------------------ 00016 Written by Alexandre Alapetite, http://alexandre.alapetite.fr/cv/ 00017 00018 Copyright 2004-2009, GNU Lesser General Public License, 00019 http://www.gnu.org/licenses/lgpl.html 00020 00021 This program is free software: you can redistribute it and/or modify 00022 it under the terms of the GNU Lesser General Public License as published by 00023 the Free Software Foundation, either version 3 of the License, or 00024 (at your option) any later version. 00025 This program is distributed in the hope that it will be useful, 00026 but WITHOUT ANY WARRANTY; without even the implied warranty of 00027 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00028 GNU Lesser General Public License for more details. 00029 You should have received a copy of the GNU Lesser General Public License 00030 along with this program. If not, see <http://www.gnu.org/licenses/lgpl.html> 00031 00032 == Rights and obligations == 00033 - Attribution: You must give the original author credit. 00034 - Share Alike: If you alter or transform this library, 00035 you may distribute the resulting library only under the same license GNU/LGPL. 00036 - In case of jurisdiction dispute, the French law is authoritative. 00037 - Any of these conditions can be waived if you get permission from Alexandre Alapetite. 00038 - Not required, but please send to Alexandre Alapetite the modifications you make, 00039 in order to improve this file for the benefit of everybody. 00040 00041 If you want to distribute this code, please do it as a link to: 00042 http://alexandre.alapetite.fr/doc-alex/domxml-php4-php5/ 00043 */ 00044 00045 define('DOMXML_LOAD_PARSING',0); 00046 define('DOMXML_LOAD_VALIDATING',1); 00047 define('DOMXML_LOAD_RECOVERING',2); 00048 define('DOMXML_LOAD_SUBSTITUTE_ENTITIES',4); 00049 //define('DOMXML_LOAD_COMPLETE_ATTRS',8); 00050 define('DOMXML_LOAD_DONT_KEEP_BLANKS',16); 00051 00052 function domxml_new_doc($version) {return new php4DOMDocument();} 00053 function domxml_new_xmldoc($version) {return new php4DOMDocument();} 00054 function domxml_open_file($filename,$mode=DOMXML_LOAD_PARSING,&$error=null) 00055 { 00056 $dom=new php4DOMDocument($mode); 00057 $errorMode=(func_num_args()>2)&&defined('LIBXML_VERSION'); 00058 if ($errorMode) libxml_use_internal_errors(true); 00059 if (!$dom->myDOMNode->load($filename)) $dom=null; 00060 if ($errorMode) 00061 { 00062 $error=array_map('_error_report',libxml_get_errors()); 00063 libxml_clear_errors(); 00064 } 00065 return $dom; 00066 } 00067 function domxml_open_mem($str,$mode=DOMXML_LOAD_PARSING,&$error=null) 00068 { 00069 $dom=new php4DOMDocument($mode); 00070 $errorMode=(func_num_args()>2)&&defined('LIBXML_VERSION'); 00071 if ($errorMode) libxml_use_internal_errors(true); 00072 if (!$dom->myDOMNode->loadXML($str)) $dom=null; 00073 if ($errorMode) 00074 { 00075 $error=array_map('_error_report',libxml_get_errors()); 00076 libxml_clear_errors(); 00077 } 00078 return $dom; 00079 } 00080 function html_doc($html_doc,$from_file=false) 00081 { 00082 $dom=new php4DOMDocument(); 00083 if ($from_file) $result=$dom->myDOMNode->loadHTMLFile($html_doc); 00084 else $result=$dom->myDOMNode->loadHTML($html_doc); 00085 return $result ? $dom : null; 00086 } 00087 function html_doc_file($filename) {return html_doc($filename,true);} 00088 function xmldoc($str) {return domxml_open_mem($str);} 00089 function xmldocfile($filename) {return domxml_open_file($filename);} 00090 function xpath_eval($xpath_context,$eval_str,$contextnode=null) {return $xpath_context->xpath_eval($eval_str,$contextnode);} 00091 function xpath_new_context($dom_document) {return new php4DOMXPath($dom_document);} 00092 function xpath_register_ns($xpath_context,$prefix,$namespaceURI) {return $xpath_context->myDOMXPath->registerNamespace($prefix,$namespaceURI);} 00093 function _entityDecode($text) {return html_entity_decode(strtr($text,array('''=>'\'')),ENT_QUOTES,'UTF-8');} 00094 function _error_report($error) {return array('errormessage'=>$error->message,'nodename'=>'','line'=>$error->line,'col'=>$error->column)+($error->file==''?array():array('directory'=>dirname($error->file),'file'=>basename($error->file)));} 00095 00096 class php4DOMAttr extends php4DOMNode 00097 { 00098 function __get($name) 00099 { 00100 if ($name==='name') return $this->myDOMNode->name; 00101 else return parent::__get($name); 00102 } 00103 function name() {return $this->myDOMNode->name;} 00104 function set_content($text) {} 00105 //function set_value($content) {return $this->myDOMNode->value=htmlspecialchars($content,ENT_QUOTES);} 00106 function specified() {return $this->myDOMNode->specified;} 00107 function value() {return $this->myDOMNode->value;} 00108 } 00109 00110 class php4DOMDocument extends php4DOMNode 00111 { 00112 function php4DOMDocument($mode=DOMXML_LOAD_PARSING) 00113 { 00114 $this->myDOMNode=new DOMDocument(); 00115 $this->myOwnerDocument=$this; 00116 if ($mode & DOMXML_LOAD_VALIDATING) $this->myDOMNode->validateOnParse=true; 00117 if ($mode & DOMXML_LOAD_RECOVERING) $this->myDOMNode->recover=true; 00118 if ($mode & DOMXML_LOAD_SUBSTITUTE_ENTITIES) $this->myDOMNode->substituteEntities=true; 00119 if ($mode & DOMXML_LOAD_DONT_KEEP_BLANKS) $this->myDOMNode->preserveWhiteSpace=false; 00120 } 00121 function add_root($name) 00122 { 00123 if ($this->myDOMNode->hasChildNodes()) $this->myDOMNode->removeChild($this->myDOMNode->firstChild); 00124 return new php4DOMElement($this->myDOMNode->appendChild($this->myDOMNode->createElement($name)),$this->myOwnerDocument); 00125 } 00126 function create_attribute($name,$value) 00127 { 00128 $myAttr=$this->myDOMNode->createAttribute($name); 00129 $myAttr->value=htmlspecialchars($value,ENT_QUOTES); 00130 return new php4DOMAttr($myAttr,$this); 00131 } 00132 function create_cdata_section($content) {return new php4DOMNode($this->myDOMNode->createCDATASection($content),$this);} 00133 function create_comment($data) {return new php4DOMNode($this->myDOMNode->createComment($data),$this);} 00134 function create_element($name) {return new php4DOMElement($this->myDOMNode->createElement($name),$this);} 00135 function create_element_ns($uri,$name,$prefix=null) 00136 { 00137 if ($prefix==null) $prefix=$this->myDOMNode->lookupPrefix($uri); 00138 if (($prefix==null)&&(($this->myDOMNode->documentElement==null)||(!$this->myDOMNode->documentElement->isDefaultNamespace($uri)))) $prefix='a'.sprintf('%u',crc32($uri)); 00139 return new php4DOMElement($this->myDOMNode->createElementNS($uri,$prefix==null ? $name : $prefix.':'.$name),$this); 00140 } 00141 function create_entity_reference($content) {return new php4DOMNode($this->myDOMNode->createEntityReference($content),$this);} //By Walter Ebert 2007-01-22 00142 function create_processing_instruction($target,$data=''){return new php4DomProcessingInstruction($this->myDOMNode->createProcessingInstruction($target,$data),$this);} 00143 function create_text_node($content) {return new php4DOMText($this->myDOMNode->createTextNode($content),$this);} 00144 function document_element() {return parent::_newDOMElement($this->myDOMNode->documentElement,$this);} 00145 function dump_file($filename,$compressionmode=false,$format=false) 00146 { 00147 $format0=$this->myDOMNode->formatOutput; 00148 $this->myDOMNode->formatOutput=$format; 00149 $res=$this->myDOMNode->save($filename); 00150 $this->myDOMNode->formatOutput=$format0; 00151 return $res; 00152 } 00153 function dump_mem($format=false,$encoding=false) 00154 { 00155 $format0=$this->myDOMNode->formatOutput; 00156 $this->myDOMNode->formatOutput=$format; 00157 $encoding0=$this->myDOMNode->encoding; 00158 if ($encoding) $this->myDOMNode->encoding=$encoding; 00159 $dump=$this->myDOMNode->saveXML(); 00160 $this->myDOMNode->formatOutput=$format0; 00161 if ($encoding) $this->myDOMNode->encoding= $encoding0=='' ? 'UTF-8' : $encoding0; //UTF-8 is XML default encoding 00162 return $dump; 00163 } 00164 function free() 00165 { 00166 if ($this->myDOMNode->hasChildNodes()) $this->myDOMNode->removeChild($this->myDOMNode->firstChild); 00167 $this->myDOMNode=null; 00168 $this->myOwnerDocument=null; 00169 } 00170 function get_element_by_id($id) {return parent::_newDOMElement($this->myDOMNode->getElementById($id),$this);} 00171 function get_elements_by_tagname($name) 00172 { 00173 $myDOMNodeList=$this->myDOMNode->getElementsByTagName($name); 00174 $nodeSet=array(); 00175 $i=0; 00176 if (isset($myDOMNodeList)) 00177 while ($node=$myDOMNodeList->item($i++)) $nodeSet[]=new php4DOMElement($node,$this); 00178 return $nodeSet; 00179 } 00180 function html_dump_mem() {return $this->myDOMNode->saveHTML();} 00181 function root() {return parent::_newDOMElement($this->myDOMNode->documentElement,$this);} 00182 function xinclude() {return $this->myDOMNode->xinclude();} 00183 function xpath_new_context() {return new php4DOMXPath($this);} 00184 } 00185 00186 class php4DOMElement extends php4DOMNode 00187 { 00188 function add_namespace($uri,$prefix) 00189 { 00190 if ($this->myDOMNode->hasAttributeNS('http://www.w3.org/2000/xmlns/',$prefix)) return false; 00191 else 00192 { 00193 $this->myDOMNode->setAttributeNS('http://www.w3.org/2000/xmlns/','xmlns:'.$prefix,$uri); //By Daniel Walker 2006-09-08 00194 return true; 00195 } 00196 } 00197 function get_attribute($name) {return $this->myDOMNode->getAttribute($name);} 00198 function get_attribute_node($name) {return parent::_newDOMElement($this->myDOMNode->getAttributeNode($name),$this->myOwnerDocument);} 00199 function get_elements_by_tagname($name) 00200 { 00201 $myDOMNodeList=$this->myDOMNode->getElementsByTagName($name); 00202 $nodeSet=array(); 00203 $i=0; 00204 if (isset($myDOMNodeList)) 00205 while ($node=$myDOMNodeList->item($i++)) $nodeSet[]=new php4DOMElement($node,$this->myOwnerDocument); 00206 return $nodeSet; 00207 } 00208 function has_attribute($name) {return $this->myDOMNode->hasAttribute($name);} 00209 function remove_attribute($name) {return $this->myDOMNode->removeAttribute($name);} 00210 function set_attribute($name,$value) 00211 { 00212 //return $this->myDOMNode->setAttribute($name,$value); //Does not return a DomAttr 00213 $myAttr=$this->myDOMNode->ownerDocument->createAttribute($name); 00214 $myAttr->value=htmlspecialchars($value,ENT_QUOTES); //Entity problem reported by AL-DesignWorks 2007-09-07 00215 $this->myDOMNode->setAttributeNode($myAttr); 00216 return new php4DOMAttr($myAttr,$this->myOwnerDocument); 00217 } 00218 /*function set_attribute_node($attr) 00219 { 00220 $this->myDOMNode->setAttributeNode($this->_importNode($attr)); 00221 return $attr; 00222 }*/ 00223 function set_name($name) 00224 { 00225 if ($this->myDOMNode->prefix=='') $newNode=$this->myDOMNode->ownerDocument->createElement($name); 00226 else $newNode=$this->myDOMNode->ownerDocument->createElementNS($this->myDOMNode->namespaceURI,$this->myDOMNode->prefix.':'.$name); 00227 $myDOMNodeList=$this->myDOMNode->attributes; 00228 $i=0; 00229 if (isset($myDOMNodeList)) 00230 while ($node=$myDOMNodeList->item($i++)) 00231 if ($node->namespaceURI=='') $newNode->setAttribute($node->name,$node->value); 00232 else $newNode->setAttributeNS($node->namespaceURI,$node->nodeName,$node->value); 00233 $myDOMNodeList=$this->myDOMNode->childNodes; 00234 if (isset($myDOMNodeList)) 00235 while ($node=$myDOMNodeList->item(0)) $newNode->appendChild($node); 00236 $this->myDOMNode->parentNode->replaceChild($newNode,$this->myDOMNode); 00237 $this->myDOMNode=$newNode; 00238 return true; 00239 } 00240 function tagname() {return $this->tagname;} 00241 } 00242 00243 class php4DOMNode 00244 { 00245 public $myDOMNode; 00246 public $myOwnerDocument; 00247 function php4DOMNode($aDomNode,$aOwnerDocument) 00248 { 00249 $this->myDOMNode=$aDomNode; 00250 $this->myOwnerDocument=$aOwnerDocument; 00251 } 00252 function __get($name) 00253 { 00254 switch ($name) 00255 { 00256 case 'type': return $this->myDOMNode->nodeType; 00257 case 'tagname': return ($this->myDOMNode->nodeType===XML_ELEMENT_NODE) ? $this->myDOMNode->localName : $this->myDOMNode->tagName; //Avoid namespace prefix for DOMElement 00258 case 'content': return $this->myDOMNode->textContent; 00259 case 'value': return $this->myDOMNode->value; 00260 default: 00261 $myErrors=debug_backtrace(); 00262 trigger_error('Undefined property: '.get_class($this).'::$'.$name.' ['.$myErrors[0]['file'].':'.$myErrors[0]['line'].']',E_USER_NOTICE); 00263 return false; 00264 } 00265 } 00266 function add_child($newnode) {return $this->append_child($newnode);} 00267 function add_namespace($uri,$prefix) {return false;} 00268 function append_child($newnode) {return self::_newDOMElement($this->myDOMNode->appendChild($this->_importNode($newnode)),$this->myOwnerDocument);} 00269 function append_sibling($newnode) {return self::_newDOMElement($this->myDOMNode->parentNode->appendChild($this->_importNode($newnode)),$this->myOwnerDocument);} 00270 function attributes() 00271 { 00272 $myDOMNodeList=$this->myDOMNode->attributes; 00273 if (!(isset($myDOMNodeList)&&$this->myDOMNode->hasAttributes())) return null; 00274 $nodeSet=array(); 00275 $i=0; 00276 while ($node=$myDOMNodeList->item($i++)) $nodeSet[]=new php4DOMAttr($node,$this->myOwnerDocument); 00277 return $nodeSet; 00278 } 00279 function child_nodes() 00280 { 00281 $myDOMNodeList=$this->myDOMNode->childNodes; 00282 $nodeSet=array(); 00283 $i=0; 00284 if (isset($myDOMNodeList)) 00285 while ($node=$myDOMNodeList->item($i++)) $nodeSet[]=self::_newDOMElement($node,$this->myOwnerDocument); 00286 return $nodeSet; 00287 } 00288 function children() {return $this->child_nodes();} 00289 function clone_node($deep=false) {return self::_newDOMElement($this->myDOMNode->cloneNode($deep),$this->myOwnerDocument);} 00290 //dump_node($node) should only be called on php4DOMDocument 00291 function dump_node($node=null) {return $node==null ? $this->myOwnerDocument->myDOMNode->saveXML($this->myDOMNode) : $this->myOwnerDocument->myDOMNode->saveXML($node->myDOMNode);} 00292 function first_child() {return self::_newDOMElement($this->myDOMNode->firstChild,$this->myOwnerDocument);} 00293 function get_content() {return $this->myDOMNode->textContent;} 00294 function has_attributes() {return $this->myDOMNode->hasAttributes();} 00295 function has_child_nodes() {return $this->myDOMNode->hasChildNodes();} 00296 function insert_before($newnode,$refnode) {return self::_newDOMElement($this->myDOMNode->insertBefore($this->_importNode($newnode),$refnode==null?null:$refnode->myDOMNode),$this->myOwnerDocument);} 00297 function is_blank_node() {return ($this->myDOMNode->nodeType===XML_TEXT_NODE)&&preg_match('%^\s*$%',$this->myDOMNode->nodeValue);} 00298 function last_child() {return self::_newDOMElement($this->myDOMNode->lastChild,$this->myOwnerDocument);} 00299 function new_child($name,$content) 00300 { 00301 $mySubNode=$this->myDOMNode->ownerDocument->createElement($name); 00302 $mySubNode->appendChild($this->myDOMNode->ownerDocument->createTextNode(_entityDecode($content))); 00303 $this->myDOMNode->appendChild($mySubNode); 00304 return new php4DOMElement($mySubNode,$this->myOwnerDocument); 00305 } 00306 function next_sibling() {return self::_newDOMElement($this->myDOMNode->nextSibling,$this->myOwnerDocument);} 00307 function node_name() {return ($this->myDOMNode->nodeType===XML_ELEMENT_NODE) ? $this->myDOMNode->localName : $this->myDOMNode->nodeName;} //Avoid namespace prefix for DOMElement 00308 function node_type() {return $this->myDOMNode->nodeType;} 00309 function node_value() {return $this->myDOMNode->nodeValue;} 00310 function owner_document() {return $this->myOwnerDocument;} 00311 function parent_node() {return self::_newDOMElement($this->myDOMNode->parentNode,$this->myOwnerDocument);} 00312 function prefix() {return $this->myDOMNode->prefix;} 00313 function previous_sibling() {return self::_newDOMElement($this->myDOMNode->previousSibling,$this->myOwnerDocument);} 00314 function remove_child($oldchild) {return self::_newDOMElement($this->myDOMNode->removeChild($oldchild->myDOMNode),$this->myOwnerDocument);} 00315 function replace_child($newnode,$oldnode) {return self::_newDOMElement($this->myDOMNode->replaceChild($this->_importNode($newnode),$oldnode->myDOMNode),$this->myOwnerDocument);} 00316 function replace_node($newnode) {return self::_newDOMElement($this->myDOMNode->parentNode->replaceChild($this->_importNode($newnode),$this->myDOMNode),$this->myOwnerDocument);} 00317 function set_content($text) {return $this->myDOMNode->appendChild($this->myDOMNode->ownerDocument->createTextNode(_entityDecode($text)));} //Entity problem reported by AL-DesignWorks 2007-09-07 00318 //function set_name($name) {return $this->myOwnerDocument->renameNode($this->myDOMNode,$this->myDOMNode->namespaceURI,$name);} 00319 function set_namespace($uri,$prefix=null) 00320 {//Contributions by Daniel Walker 2006-09-08 00321 $nsprefix=$this->myDOMNode->lookupPrefix($uri); 00322 if ($nsprefix==null) 00323 { 00324 $nsprefix= $prefix==null ? $nsprefix='a'.sprintf('%u',crc32($uri)) : $prefix; 00325 if ($this->myDOMNode->nodeType===XML_ATTRIBUTE_NODE) 00326 { 00327 if (($prefix!=null)&&$this->myDOMNode->ownerElement->hasAttributeNS('http://www.w3.org/2000/xmlns/',$nsprefix)&& 00328 ($this->myDOMNode->ownerElement->getAttributeNS('http://www.w3.org/2000/xmlns/',$nsprefix)!=$uri)) 00329 {//Remove namespace 00330 $parent=$this->myDOMNode->ownerElement; 00331 $parent->removeAttributeNode($this->myDOMNode); 00332 $parent->setAttribute($this->myDOMNode->localName,$this->myDOMNode->nodeValue); 00333 $this->myDOMNode=$parent->getAttributeNode($this->myDOMNode->localName); 00334 return; 00335 } 00336 $this->myDOMNode->ownerElement->setAttributeNS('http://www.w3.org/2000/xmlns/','xmlns:'.$nsprefix,$uri); 00337 } 00338 } 00339 if ($this->myDOMNode->nodeType===XML_ATTRIBUTE_NODE) 00340 { 00341 $parent=$this->myDOMNode->ownerElement; 00342 $parent->removeAttributeNode($this->myDOMNode); 00343 $parent->setAttributeNS($uri,$nsprefix.':'.$this->myDOMNode->localName,$this->myDOMNode->nodeValue); 00344 $this->myDOMNode=$parent->getAttributeNodeNS($uri,$this->myDOMNode->localName); 00345 } 00346 elseif ($this->myDOMNode->nodeType===XML_ELEMENT_NODE) 00347 { 00348 $NewNode=$this->myDOMNode->ownerDocument->createElementNS($uri,$nsprefix.':'.$this->myDOMNode->localName); 00349 foreach ($this->myDOMNode->attributes as $n) $NewNode->appendChild($n->cloneNode(true)); 00350 foreach ($this->myDOMNode->childNodes as $n) $NewNode->appendChild($n->cloneNode(true)); 00351 $xpath=new DOMXPath($this->myDOMNode->ownerDocument); 00352 $myDOMNodeList=$xpath->query('namespace::*[name()!="xml"]',$this->myDOMNode); //Add old namespaces 00353 foreach ($myDOMNodeList as $n) $NewNode->setAttributeNS('http://www.w3.org/2000/xmlns/',$n->nodeName,$n->nodeValue); 00354 $this->myDOMNode->parentNode->replaceChild($NewNode,$this->myDOMNode); 00355 $this->myDOMNode=$NewNode; 00356 } 00357 } 00358 function unlink_node() 00359 { 00360 if ($this->myDOMNode->parentNode!=null) 00361 { 00362 if ($this->myDOMNode->nodeType===XML_ATTRIBUTE_NODE) $this->myDOMNode->parentNode->removeAttributeNode($this->myDOMNode); 00363 else $this->myDOMNode->parentNode->removeChild($this->myDOMNode); 00364 } 00365 } 00366 protected function _importNode($newnode) {return $this->myOwnerDocument===$newnode->myOwnerDocument ? $newnode->myDOMNode : $this->myOwnerDocument->myDOMNode->importNode($newnode->myDOMNode,true);} //To import DOMNode from another DOMDocument 00367 static function _newDOMElement($aDOMNode,$aOwnerDocument) 00368 {//Check the PHP5 DOMNode before creating a new associated PHP4 DOMNode wrapper 00369 if ($aDOMNode==null) return null; 00370 switch ($aDOMNode->nodeType) 00371 { 00372 case XML_ELEMENT_NODE: return new php4DOMElement($aDOMNode,$aOwnerDocument); 00373 case XML_TEXT_NODE: return new php4DOMText($aDOMNode,$aOwnerDocument); 00374 case XML_ATTRIBUTE_NODE: return new php4DOMAttr($aDOMNode,$aOwnerDocument); 00375 case XML_PI_NODE: return new php4DomProcessingInstruction($aDOMNode,$aOwnerDocument); 00376 default: return new php4DOMNode($aDOMNode,$aOwnerDocument); 00377 } 00378 } 00379 } 00380 00381 class php4DomProcessingInstruction extends php4DOMNode 00382 { 00383 function data() {return $this->myDOMNode->data;} 00384 function target() {return $this->myDOMNode->target;} 00385 } 00386 00387 class php4DOMText extends php4DOMNode 00388 { 00389 function __get($name) 00390 { 00391 if ($name==='tagname') return '#text'; 00392 else return parent::__get($name); 00393 } 00394 function tagname() {return '#text';} 00395 function set_content($text) {$this->myDOMNode->nodeValue=$text; return true;} 00396 } 00397 00398 if (!defined('XPATH_NODESET')) 00399 { 00400 define('XPATH_UNDEFINED',0); 00401 define('XPATH_NODESET',1); 00402 define('XPATH_BOOLEAN',2); 00403 define('XPATH_NUMBER',3); 00404 define('XPATH_STRING',4); 00405 /*define('XPATH_POINT',5); 00406 define('XPATH_RANGE',6); 00407 define('XPATH_LOCATIONSET',7); 00408 define('XPATH_USERS',8); 00409 define('XPATH_XSLT_TREE',9);*/ 00410 } 00411 00412 class php4DOMNodelist 00413 { 00414 private $myDOMNodelist; 00415 public $nodeset; 00416 public $type=XPATH_UNDEFINED; 00417 public $value; 00418 function php4DOMNodelist($aDOMNodelist,$aOwnerDocument) 00419 { 00420 if (!isset($aDOMNodelist)) return; 00421 elseif (is_object($aDOMNodelist)||is_array($aDOMNodelist)) 00422 { 00423 if ($aDOMNodelist->length>0) 00424 { 00425 $this->myDOMNodelist=$aDOMNodelist; 00426 $this->nodeset=array(); 00427 $this->type=XPATH_NODESET; 00428 $i=0; 00429 while ($node=$this->myDOMNodelist->item($i++)) $this->nodeset[]=php4DOMNode::_newDOMElement($node,$aOwnerDocument); 00430 } 00431 } 00432 elseif (is_int($aDOMNodelist)||is_float($aDOMNodelist)) 00433 { 00434 $this->type=XPATH_NUMBER; 00435 $this->value=$aDOMNodelist; 00436 } 00437 elseif (is_bool($aDOMNodelist)) 00438 { 00439 $this->type=XPATH_BOOLEAN; 00440 $this->value=$aDOMNodelist; 00441 } 00442 elseif (is_string($aDOMNodelist)) 00443 { 00444 $this->type=XPATH_STRING; 00445 $this->value=$aDOMNodelist; 00446 } 00447 } 00448 } 00449 00450 class php4DOMXPath 00451 { 00452 public $myDOMXPath; 00453 private $myOwnerDocument; 00454 function php4DOMXPath($dom_document) 00455 { 00456 //TODO: If $dom_document is a DomElement, make that default $contextnode and modify XPath. Ex: '/test' 00457 $this->myOwnerDocument=$dom_document->myOwnerDocument; 00458 $this->myDOMXPath=new DOMXPath($this->myOwnerDocument->myDOMNode); 00459 } 00460 function xpath_eval($eval_str,$contextnode=null) 00461 { 00462 if (method_exists($this->myDOMXPath,'evaluate')) $xp=isset($contextnode->myDOMNode) ? $this->myDOMXPath->evaluate($eval_str,$contextnode->myDOMNode) : $this->myDOMXPath->evaluate($eval_str); 00463 else $xp=isset($contextnode->myDOMNode) ? $this->myDOMXPath->query($eval_str,$contextnode->myDOMNode) : $this->myDOMXPath->query($eval_str); 00464 $xp=new php4DOMNodelist($xp,$this->myOwnerDocument); 00465 return ($xp->type===XPATH_UNDEFINED) ? false : $xp; 00466 } 00467 function xpath_register_ns($prefix,$namespaceURI) {return $this->myDOMXPath->registerNamespace($prefix,$namespaceURI);} 00468 } 00469 00470 if (extension_loaded('xsl')) 00471 {//See also: http://alexandre.alapetite.fr/doc-alex/xslt-php4-php5/ 00472 function domxml_xslt_stylesheet($xslstring) {return new php4DomXsltStylesheet(DOMDocument::loadXML($xslstring));} 00473 function domxml_xslt_stylesheet_doc($dom_document) {return new php4DomXsltStylesheet($dom_document);} 00474 function domxml_xslt_stylesheet_file($xslfile) {return new php4DomXsltStylesheet(DOMDocument::load($xslfile));} 00475 class php4DomXsltStylesheet 00476 { 00477 private $myxsltProcessor; 00478 function php4DomXsltStylesheet($dom_document) 00479 { 00480 $this->myxsltProcessor=new xsltProcessor(); 00481 $this->myxsltProcessor->importStyleSheet($dom_document); 00482 } 00483 function process($dom_document,$xslt_parameters=array(),$param_is_xpath=false) 00484 { 00485 foreach ($xslt_parameters as $param=>$value) $this->myxsltProcessor->setParameter('',$param,$value); 00486 $myphp4DOMDocument=new php4DOMDocument(); 00487 $myphp4DOMDocument->myDOMNode=$this->myxsltProcessor->transformToDoc($dom_document->myDOMNode); 00488 return $myphp4DOMDocument; 00489 } 00490 function result_dump_file($dom_document,$filename) 00491 { 00492 $html=$dom_document->myDOMNode->saveHTML(); 00493 file_put_contents($filename,$html); 00494 return $html; 00495 } 00496 function result_dump_mem($dom_document) {return $dom_document->myDOMNode->saveHTML();} 00497 } 00498 } 00499 ?>