domxml-php4-to-php5.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. <?php
  2. /*
  3. Requires PHP5, uses built-in DOM extension.
  4. To be used in PHP4 scripts using DOMXML extension: allows PHP4/DOMXML scripts to run on PHP5/DOM.
  5. (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)
  6. Typical use:
  7. {
  8. if (PHP_VERSION>='5')
  9. require_once('domxml-php4-to-php5.php');
  10. }
  11. Version 1.21, 2008-12-05, http://alexandre.alapetite.net/doc-alex/domxml-php4-php5/
  12. ------------------------------------------------------------------
  13. Written by Alexandre Alapetite, http://alexandre.alapetite.net/cv/
  14. Copyright 2004-2008, GNU Lesser General Public License,
  15. http://www.gnu.org/licenses/lgpl.html
  16. This program is free software: you can redistribute it and/or modify
  17. it under the terms of the GNU Lesser General Public License as published by
  18. the Free Software Foundation, either version 3 of the License, or
  19. (at your option) any later version.
  20. This program is distributed in the hope that it will be useful,
  21. but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. GNU Lesser General Public License for more details.
  24. You should have received a copy of the GNU Lesser General Public License
  25. along with this program. If not, see <http://www.gnu.org/licenses/lgpl.html>
  26. == Rights and obligations ==
  27. - Attribution: You must give the original author credit.
  28. - Share Alike: If you alter or transform this library,
  29. you may distribute the resulting library only under the same license GNU/LGPL.
  30. - In case of jurisdiction dispute, the French law is authoritative.
  31. - Any of these conditions can be waived if you get permission from Alexandre Alapetite.
  32. - Not required, but please send to Alexandre Alapetite the modifications you make,
  33. in order to improve this file for the benefit of everybody.
  34. If you want to distribute this code, please do it as a link to:
  35. http://alexandre.alapetite.net/doc-alex/domxml-php4-php5/
  36. */
  37. define('DOMXML_LOAD_PARSING',0);
  38. define('DOMXML_LOAD_VALIDATING',1);
  39. define('DOMXML_LOAD_RECOVERING',2);
  40. define('DOMXML_LOAD_SUBSTITUTE_ENTITIES',4);
  41. //define('DOMXML_LOAD_COMPLETE_ATTRS',8);
  42. define('DOMXML_LOAD_DONT_KEEP_BLANKS',16);
  43. function domxml_new_doc($version) {return new php4DOMDocument();}
  44. function domxml_new_xmldoc($version) {return new php4DOMDocument();}
  45. function domxml_open_file($filename,$mode=DOMXML_LOAD_PARSING,&$error=null)
  46. {
  47. $dom=new php4DOMDocument($mode);
  48. $errorMode=(func_num_args()>2)&&defined('LIBXML_VERSION');
  49. if ($errorMode) libxml_use_internal_errors(true);
  50. if (!$dom->myDOMNode->load($filename)) $dom=null;
  51. if ($errorMode)
  52. {
  53. $error=array_map('_error_report',libxml_get_errors());
  54. libxml_clear_errors();
  55. }
  56. return $dom;
  57. }
  58. function domxml_open_mem($str,$mode=DOMXML_LOAD_PARSING,&$error=null)
  59. {
  60. $dom=new php4DOMDocument($mode);
  61. $errorMode=(func_num_args()>2)&&defined('LIBXML_VERSION');
  62. if ($errorMode) libxml_use_internal_errors(true);
  63. if (!$dom->myDOMNode->loadXML($str)) $dom=null;
  64. if ($errorMode)
  65. {
  66. $error=array_map('_error_report',libxml_get_errors());
  67. libxml_clear_errors();
  68. }
  69. return $dom;
  70. }
  71. function html_doc($html_doc,$from_file=false)
  72. {
  73. $dom=new php4DOMDocument();
  74. if ($from_file) $result=$dom->myDOMNode->loadHTMLFile($html_doc);
  75. else $result=$dom->myDOMNode->loadHTML($html_doc);
  76. return $result ? $dom : null;
  77. }
  78. function html_doc_file($filename) {return html_doc($filename,true);}
  79. function xmldoc($str) {return domxml_open_mem($str);}
  80. function xmldocfile($filename) {return domxml_open_file($filename);}
  81. function xpath_eval($xpath_context,$eval_str,$contextnode=null) {return $xpath_context->xpath_eval($eval_str,$contextnode);}
  82. function xpath_new_context($dom_document) {return new php4DOMXPath($dom_document);}
  83. function xpath_register_ns($xpath_context,$prefix,$namespaceURI) {return $xpath_context->myDOMXPath->registerNamespace($prefix,$namespaceURI);}
  84. function _entityDecode($text) {return html_entity_decode(strtr($text,array('&apos;'=>'\'')),ENT_QUOTES,'UTF-8');}
  85. 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)));}
  86. class php4DOMAttr extends php4DOMNode
  87. {
  88. function __get($name)
  89. {
  90. if ($name==='name') return $this->myDOMNode->name;
  91. else return parent::__get($name);
  92. }
  93. function name() {return $this->myDOMNode->name;}
  94. function set_content($text) {}
  95. //function set_value($content) {return $this->myDOMNode->value=htmlspecialchars($content,ENT_QUOTES);}
  96. function specified() {return $this->myDOMNode->specified;}
  97. function value() {return $this->myDOMNode->value;}
  98. }
  99. class php4DOMDocument extends php4DOMNode
  100. {
  101. function php4DOMDocument($mode=DOMXML_LOAD_PARSING)
  102. {
  103. $this->myDOMNode=new DOMDocument();
  104. $this->myOwnerDocument=$this;
  105. if ($mode & DOMXML_LOAD_VALIDATING) $this->myDOMNode->validateOnParse=true;
  106. if ($mode & DOMXML_LOAD_RECOVERING) $this->myDOMNode->recover=true;
  107. if ($mode & DOMXML_LOAD_SUBSTITUTE_ENTITIES) $this->myDOMNode->substituteEntities=true;
  108. if ($mode & DOMXML_LOAD_DONT_KEEP_BLANKS) $this->myDOMNode->preserveWhiteSpace=false;
  109. }
  110. function add_root($name)
  111. {
  112. if ($this->myDOMNode->hasChildNodes()) $this->myDOMNode->removeChild($this->myDOMNode->firstChild);
  113. return new php4DOMElement($this->myDOMNode->appendChild($this->myDOMNode->createElement($name)),$this->myOwnerDocument);
  114. }
  115. function create_attribute($name,$value)
  116. {
  117. $myAttr=$this->myDOMNode->createAttribute($name);
  118. $myAttr->value=htmlspecialchars($value,ENT_QUOTES);
  119. return new php4DOMAttr($myAttr,$this);
  120. }
  121. function create_cdata_section($content) {return new php4DOMNode($this->myDOMNode->createCDATASection($content),$this);}
  122. function create_comment($data) {return new php4DOMNode($this->myDOMNode->createComment($data),$this);}
  123. function create_element($name) {return new php4DOMElement($this->myDOMNode->createElement($name),$this);}
  124. function create_element_ns($uri,$name,$prefix=null)
  125. {
  126. if ($prefix==null) $prefix=$this->myDOMNode->lookupPrefix($uri);
  127. if (($prefix==null)&&(($this->myDOMNode->documentElement==null)||(!$this->myDOMNode->documentElement->isDefaultNamespace($uri)))) $prefix='a'.sprintf('%u',crc32($uri));
  128. return new php4DOMElement($this->myDOMNode->createElementNS($uri,$prefix==null ? $name : $prefix.':'.$name),$this);
  129. }
  130. function create_entity_reference($content) {return new php4DOMNode($this->myDOMNode->createEntityReference($content),$this);} //By Walter Ebert 2007-01-22
  131. function create_processing_instruction($target,$data=''){return new php4DomProcessingInstruction($this->myDOMNode->createProcessingInstruction($target,$data),$this);}
  132. function create_text_node($content) {return new php4DOMText($this->myDOMNode->createTextNode($content),$this);}
  133. function document_element() {return parent::_newDOMElement($this->myDOMNode->documentElement,$this);}
  134. function dump_file($filename,$compressionmode=false,$format=false)
  135. {
  136. $format0=$this->myDOMNode->formatOutput;
  137. $this->myDOMNode->formatOutput=$format;
  138. $res=$this->myDOMNode->save($filename);
  139. $this->myDOMNode->formatOutput=$format0;
  140. return $res;
  141. }
  142. function dump_mem($format=false,$encoding=false)
  143. {
  144. $format0=$this->myDOMNode->formatOutput;
  145. $this->myDOMNode->formatOutput=$format;
  146. $encoding0=$this->myDOMNode->encoding;
  147. if ($encoding) $this->myDOMNode->encoding=$encoding;
  148. $dump=$this->myDOMNode->saveXML();
  149. $this->myDOMNode->formatOutput=$format0;
  150. if ($encoding) $this->myDOMNode->encoding= $encoding0=='' ? 'UTF-8' : $encoding0; //UTF-8 is XML default encoding
  151. return $dump;
  152. }
  153. function free()
  154. {
  155. if ($this->myDOMNode->hasChildNodes()) $this->myDOMNode->removeChild($this->myDOMNode->firstChild);
  156. $this->myDOMNode=null;
  157. $this->myOwnerDocument=null;
  158. }
  159. function get_element_by_id($id) {return parent::_newDOMElement($this->myDOMNode->getElementById($id),$this);}
  160. function get_elements_by_tagname($name)
  161. {
  162. $myDOMNodeList=$this->myDOMNode->getElementsByTagName($name);
  163. $nodeSet=array();
  164. $i=0;
  165. if (isset($myDOMNodeList))
  166. while ($node=$myDOMNodeList->item($i++)) $nodeSet[]=new php4DOMElement($node,$this);
  167. return $nodeSet;
  168. }
  169. function html_dump_mem() {return $this->myDOMNode->saveHTML();}
  170. function root() {return parent::_newDOMElement($this->myDOMNode->documentElement,$this);}
  171. function xinclude() {return $this->myDOMNode->xinclude();}
  172. function xpath_new_context() {return new php4DOMXPath($this);}
  173. }
  174. class php4DOMElement extends php4DOMNode
  175. {
  176. function add_namespace($uri,$prefix)
  177. {
  178. if ($this->myDOMNode->hasAttributeNS('http://www.w3.org/2000/xmlns/',$prefix)) return false;
  179. else
  180. {
  181. $this->myDOMNode->setAttributeNS('http://www.w3.org/2000/xmlns/','xmlns:'.$prefix,$uri); //By Daniel Walker 2006-09-08
  182. return true;
  183. }
  184. }
  185. function get_attribute($name) {return $this->myDOMNode->getAttribute($name);}
  186. function get_attribute_node($name) {return parent::_newDOMElement($this->myDOMNode->getAttributeNode($name),$this->myOwnerDocument);}
  187. function get_elements_by_tagname($name)
  188. {
  189. $myDOMNodeList=$this->myDOMNode->getElementsByTagName($name);
  190. $nodeSet=array();
  191. $i=0;
  192. if (isset($myDOMNodeList))
  193. while ($node=$myDOMNodeList->item($i++)) $nodeSet[]=new php4DOMElement($node,$this->myOwnerDocument);
  194. return $nodeSet;
  195. }
  196. function has_attribute($name) {return $this->myDOMNode->hasAttribute($name);}
  197. function remove_attribute($name) {return $this->myDOMNode->removeAttribute($name);}
  198. function set_attribute($name,$value)
  199. {
  200. //return $this->myDOMNode->setAttribute($name,$value); //Does not return a DomAttr
  201. $myAttr=$this->myDOMNode->ownerDocument->createAttribute($name);
  202. $myAttr->value=htmlspecialchars($value,ENT_QUOTES); //Entity problem reported by AL-DesignWorks 2007-09-07
  203. $this->myDOMNode->setAttributeNode($myAttr);
  204. return new php4DOMAttr($myAttr,$this->myOwnerDocument);
  205. }
  206. /*function set_attribute_node($attr)
  207. {
  208. $this->myDOMNode->setAttributeNode($this->_importNode($attr));
  209. return $attr;
  210. }*/
  211. function set_name($name)
  212. {
  213. if ($this->myDOMNode->prefix=='') $newNode=$this->myDOMNode->ownerDocument->createElement($name);
  214. else $newNode=$this->myDOMNode->ownerDocument->createElementNS($this->myDOMNode->namespaceURI,$this->myDOMNode->prefix.':'.$name);
  215. $myDOMNodeList=$this->myDOMNode->attributes;
  216. $i=0;
  217. if (isset($myDOMNodeList))
  218. while ($node=$myDOMNodeList->item($i++))
  219. if ($node->namespaceURI=='') $newNode->setAttribute($node->name,$node->value);
  220. else $newNode->setAttributeNS($node->namespaceURI,$node->nodeName,$node->value);
  221. $myDOMNodeList=$this->myDOMNode->childNodes;
  222. if (isset($myDOMNodeList))
  223. while ($node=$myDOMNodeList->item(0)) $newNode->appendChild($node);
  224. $this->myDOMNode->parentNode->replaceChild($newNode,$this->myDOMNode);
  225. $this->myDOMNode=$newNode;
  226. return true;
  227. }
  228. function tagname() {return $this->tagname;}
  229. }
  230. class php4DOMNode
  231. {
  232. public $myDOMNode;
  233. public $myOwnerDocument;
  234. function php4DOMNode($aDomNode,$aOwnerDocument)
  235. {
  236. $this->myDOMNode=$aDomNode;
  237. $this->myOwnerDocument=$aOwnerDocument;
  238. }
  239. function __get($name)
  240. {
  241. switch ($name)
  242. {
  243. case 'type': return $this->myDOMNode->nodeType;
  244. case 'tagname': return ($this->myDOMNode->nodeType===XML_ELEMENT_NODE) ? $this->myDOMNode->localName : $this->myDOMNode->tagName; //Avoid namespace prefix for DOMElement
  245. case 'content': return $this->myDOMNode->textContent;
  246. case 'value': return $this->myDOMNode->value;
  247. default:
  248. $myErrors=debug_backtrace();
  249. trigger_error('Undefined property: '.get_class($this).'::$'.$name.' ['.$myErrors[0]['file'].':'.$myErrors[0]['line'].']',E_USER_NOTICE);
  250. return false;
  251. }
  252. }
  253. function add_child($newnode) {return append_child($newnode);}
  254. function add_namespace($uri,$prefix) {return false;}
  255. function append_child($newnode) {return self::_newDOMElement($this->myDOMNode->appendChild($this->_importNode($newnode)),$this->myOwnerDocument);}
  256. function append_sibling($newnode) {return self::_newDOMElement($this->myDOMNode->parentNode->appendChild($this->_importNode($newnode)),$this->myOwnerDocument);}
  257. function attributes()
  258. {
  259. $myDOMNodeList=$this->myDOMNode->attributes;
  260. if (!(isset($myDOMNodeList)&&$this->myDOMNode->hasAttributes())) return null;
  261. $nodeSet=array();
  262. $i=0;
  263. while ($node=$myDOMNodeList->item($i++)) $nodeSet[]=new php4DOMAttr($node,$this->myOwnerDocument);
  264. return $nodeSet;
  265. }
  266. function child_nodes()
  267. {
  268. $myDOMNodeList=$this->myDOMNode->childNodes;
  269. $nodeSet=array();
  270. $i=0;
  271. if (isset($myDOMNodeList))
  272. while ($node=$myDOMNodeList->item($i++)) $nodeSet[]=self::_newDOMElement($node,$this->myOwnerDocument);
  273. return $nodeSet;
  274. }
  275. function children() {return $this->child_nodes();}
  276. function clone_node($deep=false) {return self::_newDOMElement($this->myDOMNode->cloneNode($deep),$this->myOwnerDocument);}
  277. //dump_node($node) should only be called on php4DOMDocument
  278. function dump_node($node=null) {return $node==null ? $this->myOwnerDocument->myDOMNode->saveXML($this->myDOMNode) : $this->myOwnerDocument->myDOMNode->saveXML($node->myDOMNode);}
  279. function first_child() {return self::_newDOMElement($this->myDOMNode->firstChild,$this->myOwnerDocument);}
  280. function get_content() {return $this->myDOMNode->textContent;}
  281. function has_attributes() {return $this->myDOMNode->hasAttributes();}
  282. function has_child_nodes() {return $this->myDOMNode->hasChildNodes();}
  283. function insert_before($newnode,$refnode) {return self::_newDOMElement($this->myDOMNode->insertBefore($this->_importNode($newnode),$refnode==null?null:$refnode->myDOMNode),$this->myOwnerDocument);}
  284. function is_blank_node() {return ($this->myDOMNode->nodeType===XML_TEXT_NODE)&&preg_match('%^\s*$%',$this->myDOMNode->nodeValue);}
  285. function last_child() {return self::_newDOMElement($this->myDOMNode->lastChild,$this->myOwnerDocument);}
  286. function new_child($name,$content)
  287. {
  288. $mySubNode=$this->myDOMNode->ownerDocument->createElement($name);
  289. $mySubNode->appendChild($this->myDOMNode->ownerDocument->createTextNode(_entityDecode($content)));
  290. $this->myDOMNode->appendChild($mySubNode);
  291. return new php4DOMElement($mySubNode,$this->myOwnerDocument);
  292. }
  293. function next_sibling() {return self::_newDOMElement($this->myDOMNode->nextSibling,$this->myOwnerDocument);}
  294. function node_name() {return ($this->myDOMNode->nodeType===XML_ELEMENT_NODE) ? $this->myDOMNode->localName : $this->myDOMNode->nodeName;} //Avoid namespace prefix for DOMElement
  295. function node_type() {return $this->myDOMNode->nodeType;}
  296. function node_value() {return $this->myDOMNode->nodeValue;}
  297. function owner_document() {return $this->myOwnerDocument;}
  298. function parent_node() {return self::_newDOMElement($this->myDOMNode->parentNode,$this->myOwnerDocument);}
  299. function prefix() {return $this->myDOMNode->prefix;}
  300. function previous_sibling() {return self::_newDOMElement($this->myDOMNode->previousSibling,$this->myOwnerDocument);}
  301. function remove_child($oldchild) {return self::_newDOMElement($this->myDOMNode->removeChild($oldchild->myDOMNode),$this->myOwnerDocument);}
  302. function replace_child($newnode,$oldnode) {return self::_newDOMElement($this->myDOMNode->replaceChild($this->_importNode($newnode),$oldnode->myDOMNode),$this->myOwnerDocument);}
  303. function replace_node($newnode) {return self::_newDOMElement($this->myDOMNode->parentNode->replaceChild($this->_importNode($newnode),$this->myDOMNode),$this->myOwnerDocument);}
  304. function set_content($text) {return $this->myDOMNode->appendChild($this->myDOMNode->ownerDocument->createTextNode(_entityDecode($text)));} //Entity problem reported by AL-DesignWorks 2007-09-07
  305. //function set_name($name) {return $this->myOwnerDocument->renameNode($this->myDOMNode,$this->myDOMNode->namespaceURI,$name);}
  306. function set_namespace($uri,$prefix=null)
  307. {//Contributions by Daniel Walker 2006-09-08
  308. $nsprefix=$this->myDOMNode->lookupPrefix($uri);
  309. if ($nsprefix==null)
  310. {
  311. $nsprefix= $prefix==null ? $nsprefix='a'.sprintf('%u',crc32($uri)) : $prefix;
  312. if ($this->myDOMNode->nodeType===XML_ATTRIBUTE_NODE)
  313. {
  314. if (($prefix!=null)&&$this->myDOMNode->ownerElement->hasAttributeNS('http://www.w3.org/2000/xmlns/',$nsprefix)&&
  315. ($this->myDOMNode->ownerElement->getAttributeNS('http://www.w3.org/2000/xmlns/',$nsprefix)!=$uri))
  316. {//Remove namespace
  317. $parent=$this->myDOMNode->ownerElement;
  318. $parent->removeAttributeNode($this->myDOMNode);
  319. $parent->setAttribute($this->myDOMNode->localName,$this->myDOMNode->nodeValue);
  320. $this->myDOMNode=$parent->getAttributeNode($this->myDOMNode->localName);
  321. return;
  322. }
  323. $this->myDOMNode->ownerElement->setAttributeNS('http://www.w3.org/2000/xmlns/','xmlns:'.$nsprefix,$uri);
  324. }
  325. }
  326. if ($this->myDOMNode->nodeType===XML_ATTRIBUTE_NODE)
  327. {
  328. $parent=$this->myDOMNode->ownerElement;
  329. $parent->removeAttributeNode($this->myDOMNode);
  330. $parent->setAttributeNS($uri,$nsprefix.':'.$this->myDOMNode->localName,$this->myDOMNode->nodeValue);
  331. $this->myDOMNode=$parent->getAttributeNodeNS($uri,$this->myDOMNode->localName);
  332. }
  333. elseif ($this->myDOMNode->nodeType===XML_ELEMENT_NODE)
  334. {
  335. $NewNode=$this->myDOMNode->ownerDocument->createElementNS($uri,$nsprefix.':'.$this->myDOMNode->localName);
  336. foreach ($this->myDOMNode->attributes as $n) $NewNode->appendChild($n->cloneNode(true));
  337. foreach ($this->myDOMNode->childNodes as $n) $NewNode->appendChild($n->cloneNode(true));
  338. $xpath=new DOMXPath($this->myDOMNode->ownerDocument);
  339. $myDOMNodeList=$xpath->query('namespace::*[name()!="xml"]',$this->myDOMNode); //Add old namespaces
  340. foreach ($myDOMNodeList as $n) $NewNode->setAttributeNS('http://www.w3.org/2000/xmlns/',$n->nodeName,$n->nodeValue);
  341. $this->myDOMNode->parentNode->replaceChild($NewNode,$this->myDOMNode);
  342. $this->myDOMNode=$NewNode;
  343. }
  344. }
  345. function unlink_node()
  346. {
  347. if ($this->myDOMNode->parentNode!=null)
  348. {
  349. if ($this->myDOMNode->nodeType===XML_ATTRIBUTE_NODE) $this->myDOMNode->parentNode->removeAttributeNode($this->myDOMNode);
  350. else $this->myDOMNode->parentNode->removeChild($this->myDOMNode);
  351. }
  352. }
  353. protected function _importNode($newnode) {return $this->myOwnerDocument===$newnode->myOwnerDocument ? $newnode->myDOMNode : $this->myOwnerDocument->myDOMNode->importNode($newnode->myDOMNode,true);} //To import DOMNode from another DOMDocument
  354. static function _newDOMElement($aDOMNode,$aOwnerDocument)
  355. {//Check the PHP5 DOMNode before creating a new associated PHP4 DOMNode wrapper
  356. if ($aDOMNode==null) return null;
  357. switch ($aDOMNode->nodeType)
  358. {
  359. case XML_ELEMENT_NODE: return new php4DOMElement($aDOMNode,$aOwnerDocument);
  360. case XML_TEXT_NODE: return new php4DOMText($aDOMNode,$aOwnerDocument);
  361. case XML_ATTRIBUTE_NODE: return new php4DOMAttr($aDOMNode,$aOwnerDocument);
  362. case XML_PI_NODE: return new php4DomProcessingInstruction($aDOMNode,$aOwnerDocument);
  363. default: return new php4DOMNode($aDOMNode,$aOwnerDocument);
  364. }
  365. }
  366. }
  367. class php4DomProcessingInstruction extends php4DOMNode
  368. {
  369. function data() {return $this->myDOMNode->data;}
  370. function target() {return $this->myDOMNode->target;}
  371. }
  372. class php4DOMText extends php4DOMNode
  373. {
  374. function __get($name)
  375. {
  376. if ($name==='tagname') return '#text';
  377. else return parent::__get($name);
  378. }
  379. function tagname() {return '#text';}
  380. function set_content($text) {$this->myDOMNode->nodeValue=$text; return true;}
  381. }
  382. if (!defined('XPATH_NODESET'))
  383. {
  384. define('XPATH_UNDEFINED',0);
  385. define('XPATH_NODESET',1);
  386. define('XPATH_BOOLEAN',2);
  387. define('XPATH_NUMBER',3);
  388. define('XPATH_STRING',4);
  389. /*define('XPATH_POINT',5);
  390. define('XPATH_RANGE',6);
  391. define('XPATH_LOCATIONSET',7);
  392. define('XPATH_USERS',8);
  393. define('XPATH_XSLT_TREE',9);*/
  394. }
  395. class php4DOMNodelist
  396. {
  397. private $myDOMNodelist;
  398. public $nodeset;
  399. public $type=XPATH_UNDEFINED;
  400. public $value;
  401. function php4DOMNodelist($aDOMNodelist,$aOwnerDocument)
  402. {
  403. if (!isset($aDOMNodelist)) return;
  404. elseif (is_object($aDOMNodelist)||is_array($aDOMNodelist))
  405. {
  406. if ($aDOMNodelist->length>0)
  407. {
  408. $this->myDOMNodelist=$aDOMNodelist;
  409. $this->nodeset=array();
  410. $this->type=XPATH_NODESET;
  411. $i=0;
  412. while ($node=$this->myDOMNodelist->item($i++)) $this->nodeset[]=php4DOMNode::_newDOMElement($node,$aOwnerDocument);
  413. }
  414. }
  415. elseif (is_int($aDOMNodelist)||is_float($aDOMNodelist))
  416. {
  417. $this->type=XPATH_NUMBER;
  418. $this->value=$aDOMNodelist;
  419. }
  420. elseif (is_bool($aDOMNodelist))
  421. {
  422. $this->type=XPATH_BOOLEAN;
  423. $this->value=$aDOMNodelist;
  424. }
  425. elseif (is_string($aDOMNodelist))
  426. {
  427. $this->type=XPATH_STRING;
  428. $this->value=$aDOMNodelist;
  429. }
  430. }
  431. }
  432. class php4DOMXPath
  433. {
  434. public $myDOMXPath;
  435. private $myOwnerDocument;
  436. function php4DOMXPath($dom_document)
  437. {
  438. //TODO: If $dom_document is a DomElement, make that default $contextnode and modify XPath. Ex: '/test'
  439. $this->myOwnerDocument=$dom_document->myOwnerDocument;
  440. $this->myDOMXPath=new DOMXPath($this->myOwnerDocument->myDOMNode);
  441. }
  442. function xpath_eval($eval_str,$contextnode=null)
  443. {
  444. if (method_exists($this->myDOMXPath,'evaluate')) $xp=isset($contextnode) ? $this->myDOMXPath->evaluate($eval_str,$contextnode->myDOMNode) : $this->myDOMXPath->evaluate($eval_str);
  445. else $xp=isset($contextnode) ? $this->myDOMXPath->query($eval_str,$contextnode->myDOMNode) : $this->myDOMXPath->query($eval_str);
  446. $xp=new php4DOMNodelist($xp,$this->myOwnerDocument);
  447. return ($xp->type===XPATH_UNDEFINED) ? false : $xp;
  448. }
  449. function xpath_register_ns($prefix,$namespaceURI) {return $this->myDOMXPath->registerNamespace($prefix,$namespaceURI);}
  450. }
  451. if (extension_loaded('xsl'))
  452. {//See also: http://alexandre.alapetite.net/doc-alex/xslt-php4-php5/
  453. function domxml_xslt_stylesheet($xslstring) {return new php4DomXsltStylesheet(DOMDocument::loadXML($xslstring));}
  454. function domxml_xslt_stylesheet_doc($dom_document) {return new php4DomXsltStylesheet($dom_document);}
  455. function domxml_xslt_stylesheet_file($xslfile) {return new php4DomXsltStylesheet(DOMDocument::load($xslfile));}
  456. class php4DomXsltStylesheet
  457. {
  458. private $myxsltProcessor;
  459. function php4DomXsltStylesheet($dom_document)
  460. {
  461. $this->myxsltProcessor=new xsltProcessor();
  462. $this->myxsltProcessor->importStyleSheet($dom_document);
  463. }
  464. function process($dom_document,$xslt_parameters=array(),$param_is_xpath=false)
  465. {
  466. foreach ($xslt_parameters as $param=>$value) $this->myxsltProcessor->setParameter('',$param,$value);
  467. $myphp4DOMDocument=new php4DOMDocument();
  468. $myphp4DOMDocument->myDOMNode=$this->myxsltProcessor->transformToDoc($dom_document->myDOMNode);
  469. return $myphp4DOMDocument;
  470. }
  471. function result_dump_file($dom_document,$filename)
  472. {
  473. $html=$dom_document->myDOMNode->saveHTML();
  474. file_put_contents($filename,$html);
  475. return $html;
  476. }
  477. function result_dump_mem($dom_document) {return $dom_document->myDOMNode->saveHTML();}
  478. }
  479. }
  480. ?>