3 * @file domxml-php4-php5.php
4 * Require PHP5, uses built-in DOM extension.
5 * To be used in PHP4 scripts using DOMXML extension.
6 * Allows PHP4/DOMXML scripts to run on PHP5/DOM.
7 * (Requires PHP5/XSL extension for domxml_xslt functions)
12 * if (version_compare(PHP_VERSION,'5','>='))
13 * require_once('domxml-php4-to-php5.php');
17 * Version 1.5.5, 2005-01-18, http://alexandre.alapetite.net/doc-alex/domxml-php4-php5/
19 * ------------------------------------------------------------------<br>
20 * Written by Alexandre Alapetite, http://alexandre.alapetite.net/cv/
22 * Copyright 2004, Licence: Creative Commons "Attribution-ShareAlike 2.0 France" BY-SA (FR),
23 * http://creativecommons.org/licenses/by-sa/2.0/fr/
24 * http://alexandre.alapetite.net/divers/apropos/#by-sa
25 * - Attribution. You must give the original author credit
26 * - Share Alike. If you alter, transform, or build upon this work,
27 * you may distribute the resulting work only under a license identical to this one
28 * - The French law is authoritative
29 * - Any of these conditions can be waived if you get permission from Alexandre Alapetite
30 * - Please send to Alexandre Alapetite the modifications you make,
31 * in order to improve this file for the benefit of everybody
33 * If you want to distribute this code, please do it as a link to:
34 * http://alexandre.alapetite.net/doc-alex/domxml-php4-php5/
37 function domxml_new_doc($version) {return new php4DOMDocument('');}
38 function domxml_open_file($filename) {return new php4DOMDocument($filename);}
39 function domxml_open_mem($str)
41 $dom=new php4DOMDocument('');
42 $dom->myDOMNode->loadXML($str);
45 function xpath_eval($xpath_context,$eval_str,$contextnode=null) {return $xpath_context->query($eval_str,$contextnode);}
46 function xpath_new_context($dom_document) {return new php4DOMXPath($dom_document);}
48 class php4DOMAttr extends php4DOMNode
50 function php4DOMAttr($aDOMAttr) {$this->myDOMNode=$aDOMAttr;}
51 function Name() {return $this->myDOMNode->name;}
52 function Specified() {return $this->myDOMNode->specified;}
53 function Value() {return $this->myDOMNode->value;}
56 class php4DOMDocument extends php4DOMNode
58 function php4DOMDocument($filename='')
60 $this->myDOMNode=new DOMDocument();
61 if ($filename!='') $this->myDOMNode->load($filename);
63 function create_attribute($name,$value)
65 $myAttr=$this->myDOMNode->createAttribute($name);
66 $myAttr->value=$value;
67 return new php4DOMAttr($myAttr,$this);
69 function create_cdata_section($content) {return new php4DOMNode($this->myDOMNode->createCDATASection($content),$this);}
70 function create_comment($data) {return new php4DOMNode($this->myDOMNode->createComment($data),$this);}
71 function create_element($name) {return new php4DOMElement($this->myDOMNode->createElement($name),$this);}
72 function create_text_node($content) {return new php4DOMNode($this->myDOMNode->createTextNode($content),$this);}
73 function document_element() {return new php4DOMElement($this->myDOMNode->documentElement,$this);}
74 function dump_file($filename,$compressionmode=false,$format=false) {return $this->myDOMNode->save($filename);}
75 function dump_mem($format=false,$encoding=false) {return $this->myDOMNode->saveXML();}
76 function get_element_by_id($id) {return new php4DOMElement($this->myDOMNode->getElementById($id),$this);}
77 function get_elements_by_tagname($name)
79 $myDOMNodeList=$this->myDOMNode->getElementsByTagName($name);
82 if (isset($myDOMNodeList))
83 while ($node=$myDOMNodeList->item($i))
85 $nodeSet[]=new php4DOMElement($node,$this);
90 function html_dump_mem() {return $this->myDOMNode->saveHTML();}
91 function root() {return new php4DOMElement($this->myDOMNode->documentElement,$this);}
94 class php4DOMElement extends php4DOMNode
96 function get_attribute($name) {return $this->myDOMNode->getAttribute($name);}
97 function get_elements_by_tagname($name)
99 $myDOMNodeList=$this->myDOMNode->getElementsByTagName($name);
102 if (isset($myDOMNodeList))
103 while ($node=$myDOMNodeList->item($i))
105 $nodeSet[]=new php4DOMElement($node,$this->myOwnerDocument);
110 function has_attribute($name) {return $this->myDOMNode->hasAttribute($name);}
111 function remove_attribute($name) {return $this->myDOMNode->removeAttribute($name);}
112 function set_attribute($name,$value) {return $this->myDOMNode->setAttribute($name,$value);}
113 function tagname() {return $this->myDOMNode->tagName;}
119 var $myOwnerDocument;
120 function php4DOMNode($aDomNode,$aOwnerDocument)
122 $this->myDOMNode=$aDomNode;
123 $this->myOwnerDocument=$aOwnerDocument;
125 function __get($name)
127 if ($name=='type') return $this->myDOMNode->nodeType;
128 elseif ($name=='tagname') return $this->myDOMNode->tagName;
129 elseif ($name=='content') return $this->myDOMNode->textContent;
132 $myErrors=debug_backtrace();
133 trigger_error('Undefined property: '.get_class($this).'::$'.$name.' ['.$myErrors[0]['file'].':'.$myErrors[0]['line'].']',E_USER_NOTICE);
137 function append_child($newnode) {return new php4DOMElement($this->myDOMNode->appendChild($newnode->myDOMNode),$this->myOwnerDocument);}
138 function append_sibling($newnode) {return new php4DOMElement($this->myDOMNode->parentNode->appendChild($newnode->myDOMNode),$this->myOwnerDocument);}
139 function attributes()
141 $myDOMNodeList=$this->myDOMNode->attributes;
144 if (isset($myDOMNodeList))
145 while ($node=$myDOMNodeList->item($i))
147 $nodeSet[]=new php4DOMAttr($node,$this->myOwnerDocument);
152 function child_nodes()
154 $myDOMNodeList=$this->myDOMNode->childNodes;
157 if (isset($myDOMNodeList))
158 while ($node=$myDOMNodeList->item($i))
160 $nodeSet[]=new php4DOMElement($node,$this->myOwnerDocument);
165 function children() {return $this->child_nodes();}
166 function clone_node($deep=false) {return new php4DOMElement($this->myDOMNode->cloneNode($deep),$this->myOwnerDocument);}
167 function first_child() {return new php4DOMElement($this->myDOMNode->firstChild,$this->myOwnerDocument);}
168 function get_content() {return $this->myDOMNode->textContent;}
169 function has_attributes() {return $this->myDOMNode->hasAttributes();}
170 function has_child_nodes() {return $this->myDOMNode->hasChildNodes();}
171 function insert_before($newnode,$refnode) {return new php4DOMElement($this->myDOMNode->insertBefore($newnode->myDOMNode,$refnode->myDOMNode),$this->myOwnerDocument);}
172 function is_blank_node()
174 $myDOMNodeList=$this->myDOMNode->childNodes;
176 if (isset($myDOMNodeList))
177 while ($node=$myDOMNodeList->item($i))
179 if (($node->nodeType==XML_ELEMENT_NODE)||
180 (($node->nodeType==XML_TEXT_NODE)&&!ereg('^([[:cntrl:]]|[[:space:]])*$',$node->nodeValue)))
186 function last_child() {return new php4DOMElement($this->myDOMNode->lastChild,$this->myOwnerDocument);}
187 function new_child($name,$content)
189 $mySubNode=$this->myDOMNode->ownerDocument->createElement($name);
190 $mySubNode->appendChild($this->myDOMNode->ownerDocument->createTextNode($content));
191 $this->myDOMNode->appendChild($mySubNode);
192 return new php4DOMElement($mySubNode,$this->myOwnerDocument);
194 function next_sibling() {return new php4DOMElement($this->myDOMNode->nextSibling,$this->myOwnerDocument);}
195 function node_name() {return $this->myDOMNode->localName;}
196 function node_type() {return $this->myDOMNode->nodeType;}
197 function node_value() {return $this->myDOMNode->nodeValue;}
198 function owner_document() {return $this->myOwnerDocument;}
199 function parent_node() {return new php4DOMElement($this->myDOMNode->parentNode,$this->myOwnerDocument);}
200 function prefix() {return $this->myDOMNode->prefix;}
201 function previous_sibling() {return new php4DOMElement($this->myDOMNode->previousSibling,$this->myOwnerDocument);}
202 function remove_child($oldchild) {return new php4DOMElement($this->myDOMNode->removeChild($oldchild->myDOMNode),$this->myOwnerDocument);}
203 function replace_child($oldnode,$newnode) {return new php4DOMElement($this->myDOMNode->replaceChild($oldnode->myDOMNode,$newnode->myDOMNode),$this->myOwnerDocument);}
204 function set_content($text)
206 if (($this->myDOMNode->hasChildNodes())&&($this->myDOMNode->firstChild->nodeType==XML_TEXT_NODE))
207 $this->myDOMNode->removeChild($this->myDOMNode->firstChild);
208 return $this->myDOMNode->appendChild($this->myDOMNode->ownerDocument->createTextNode($text));
212 class php4DOMNodelist
216 function php4DOMNodelist($aDOMNodelist,$aOwnerDocument)
218 $this->myDOMNodelist=$aDOMNodelist;
219 $this->nodeset=array();
221 if (isset($this->myDOMNodelist))
222 while ($node=$this->myDOMNodelist->item($i))
224 $this->nodeset[]=new php4DOMElement($node,$aOwnerDocument);
233 var $myOwnerDocument;
234 function php4DOMXPath($dom_document)
236 $this->myOwnerDocument=$dom_document;
237 $this->myDOMXPath=new DOMXPath($dom_document->myDOMNode);
239 function query($eval_str,$contextnode)
241 if (isset($contextnode)) return new php4DOMNodelist($this->myDOMXPath->query($eval_str,$contextnode->myDOMNode),$this->myOwnerDocument);
242 else return new php4DOMNodelist($this->myDOMXPath->query($eval_str),$this->myOwnerDocument);
244 function xpath_register_ns($prefix,$namespaceURI) {return $this->myDOMXPath->registerNamespace($prefix,$namespaceURI);}
247 if (extension_loaded('xsl'))
248 {//See also: http://alexandre.alapetite.net/doc-alex/xslt-php4-php5/
249 function domxml_xslt_stylesheet($xslstring) {return new php4DomXsltStylesheet(DOMDocument::loadXML($xslstring));}
250 function domxml_xslt_stylesheet_doc($dom_document) {return new php4DomXsltStylesheet($dom_document);}
251 function domxml_xslt_stylesheet_file($xslfile) {return new php4DomXsltStylesheet(DOMDocument::load($xslfile));}
252 class php4DomXsltStylesheet
254 var $myxsltProcessor;
255 function php4DomXsltStylesheet($dom_document)
257 $this->myxsltProcessor=new xsltProcessor();
258 $this->myxsltProcessor->importStyleSheet($dom_document);
260 function process($dom_document,$xslt_parameters=array(),$param_is_xpath=false)
262 foreach ($xslt_parameters as $param=>$value)
263 $this->myxsltProcessor->setParameter('',$param,$value);
264 $myphp4DOMDocument=new php4DOMDocument();
265 $myphp4DOMDocument->myDOMNode=$this->myxsltProcessor->transformToDoc($dom_document->myDOMNode);
266 return $myphp4DOMDocument;
268 function result_dump_file($dom_document,$filename)
270 $html=$dom_document->myDOMNode->saveHTML();
271 file_put_contents($filename,$html);
274 function result_dump_mem($dom_document) {return $dom_document->myDOMNode->saveHTML();}