45 define(
'DOMXML_LOAD_PARSING',0);
46 define(
'DOMXML_LOAD_VALIDATING',1);
47 define(
'DOMXML_LOAD_RECOVERING',2);
48 define(
'DOMXML_LOAD_SUBSTITUTE_ENTITIES',4);
50 define(
'DOMXML_LOAD_DONT_KEEP_BLANKS',16);
57 $errorMode=(func_num_args()>2)&&defined(
'LIBXML_VERSION');
58 if ($errorMode) libxml_use_internal_errors(
true);
59 if (!$dom->myDOMNode->load(
$filename)) $dom=null;
62 $error=array_map(
'_error_report',libxml_get_errors());
63 libxml_clear_errors();
70 $errorMode=(func_num_args()>2)&&defined(
'LIBXML_VERSION');
71 if ($errorMode) libxml_use_internal_errors(
true);
72 if (!$dom->myDOMNode->loadXML($str)) $dom=null;
75 $error=array_map(
'_error_report',libxml_get_errors());
76 libxml_clear_errors();
83 if ($from_file) $result=$dom->myDOMNode->loadHTMLFile($html_doc);
84 else $result=$dom->myDOMNode->loadHTML($html_doc);
85 return $result ? $dom : null;
90 function xpath_eval($xpath_context,$eval_str,$contextnode=null) {
return $xpath_context->xpath_eval($eval_str,$contextnode);}
92 function xpath_register_ns($xpath_context,$prefix,$namespaceURI) {
return $xpath_context->myDOMXPath->registerNamespace($prefix,$namespaceURI);}
93 function _entityDecode($text) {
return html_entity_decode(strtr($text,array(
'''=>
'\'')),ENT_QUOTES,
'UTF-8');}
99 if (
$name===
'name')
return $this->myDOMNode->name;
102 function name() {
return $this->myDOMNode->name;}
105 function specified() {
return $this->myDOMNode->specified;}
106 function value() {
return $this->myDOMNode->value;}
112 $this->myDOMNode=
new DOMDocument();
113 $this->myOwnerDocument=$this;
121 if ($this->myDOMNode->hasChildNodes()) $this->myDOMNode->removeChild($this->myDOMNode->firstChild);
122 return new php4DOMElement($this->myDOMNode->appendChild($this->myDOMNode->createElement(
$name)),$this->myOwnerDocument);
126 $myAttr=$this->myDOMNode->createAttribute(
$name);
127 $myAttr->value=htmlspecialchars($value,ENT_QUOTES);
135 if ($prefix==null) $prefix=$this->myDOMNode->lookupPrefix($uri);
136 if (($prefix==null)&&(($this->myDOMNode->documentElement==null)||(!$this->myDOMNode->documentElement->isDefaultNamespace($uri)))) $prefix=
'a'.sprintf(
'%u',crc32($uri));
145 $format0=$this->myDOMNode->formatOutput;
146 $this->myDOMNode->formatOutput=$format;
148 $this->myDOMNode->formatOutput=$format0;
153 $format0=$this->myDOMNode->formatOutput;
154 $this->myDOMNode->formatOutput=$format;
155 $encoding0=$this->myDOMNode->encoding;
156 if ($encoding) $this->myDOMNode->encoding=$encoding;
157 $dump=$this->myDOMNode->saveXML();
158 $this->myDOMNode->formatOutput=$format0;
159 if ($encoding) $this->myDOMNode->encoding= $encoding0==
'' ?
'UTF-8' : $encoding0;
164 if ($this->myDOMNode->hasChildNodes()) $this->myDOMNode->removeChild($this->myDOMNode->firstChild);
165 $this->myDOMNode=null;
166 $this->myOwnerDocument=null;
171 $myDOMNodeList=$this->myDOMNode->getElementsByTagName(
$name);
174 if (isset($myDOMNodeList))
175 while ($node=$myDOMNodeList->item($i++)) $nodeSet[]=
new php4DOMElement($node,$this);
180 function xinclude() {
return $this->myDOMNode->xinclude();}
187 if ($this->myDOMNode->hasAttributeNS(
'http://www.w3.org/2000/xmlns/',$prefix))
return false;
190 $this->myDOMNode->setAttributeNS(
'http://www.w3.org/2000/xmlns/',
'xmlns:'.$prefix,$uri);
198 $myDOMNodeList=$this->myDOMNode->getElementsByTagName(
$name);
201 if (isset($myDOMNodeList))
202 while ($node=$myDOMNodeList->item($i++)) $nodeSet[]=
new php4DOMElement($node,$this->myOwnerDocument);
210 $myAttr=$this->myDOMNode->ownerDocument->createAttribute(
$name);
211 $myAttr->value=htmlspecialchars($value,ENT_QUOTES);
212 $this->myDOMNode->setAttributeNode($myAttr);
213 return new php4DOMAttr($myAttr,$this->myOwnerDocument);
223 if ($this->myDOMNode->prefix==
'') $newNode=$this->myDOMNode->ownerDocument->createElement(
$name);
224 else $newNode=$this->myDOMNode->ownerDocument->createElementNS($this->myDOMNode->namespaceURI,$this->myDOMNode->prefix.
':'.
$name);
225 $myDOMNodeList=$this->myDOMNode->attributes;
227 if (isset($myDOMNodeList))
228 while ($node=$myDOMNodeList->item($i++))
229 if ($node->namespaceURI==
'') $newNode->setAttribute($node->name,$node->value);
230 else $newNode->setAttributeNS($node->namespaceURI,$node->nodeName,$node->value);
231 $myDOMNodeList=$this->myDOMNode->childNodes;
232 if (isset($myDOMNodeList))
233 while ($node=$myDOMNodeList->item(0)) $newNode->appendChild($node);
234 $this->myDOMNode->parentNode->replaceChild($newNode,$this->myDOMNode);
235 $this->myDOMNode=$newNode;
246 $this->myDOMNode=$aDomNode;
247 $this->myOwnerDocument=$aOwnerDocument;
253 case 'type':
return $this->myDOMNode->nodeType;
254 case 'tagname':
return ($this->myDOMNode->nodeType===XML_ELEMENT_NODE) ? $this->myDOMNode->localName : $this->myDOMNode->tagName;
255 case 'content':
return $this->myDOMNode->textContent;
256 case 'value':
return $this->myDOMNode->value;
258 $myErrors=debug_backtrace();
259 trigger_error(
'Undefined property: '.get_class($this).
'::$'.
$name.
' ['.$myErrors[0][
'file'].
':'.$myErrors[0][
'line'].
']',E_USER_NOTICE);
269 $myDOMNodeList=$this->myDOMNode->attributes;
270 if (!(isset($myDOMNodeList)&&$this->myDOMNode->hasAttributes()))
return null;
273 while ($node=$myDOMNodeList->item($i++)) $nodeSet[]=
new php4DOMAttr($node,$this->myOwnerDocument);
278 $myDOMNodeList=$this->myDOMNode->childNodes;
281 if (isset($myDOMNodeList))
282 while ($node=$myDOMNodeList->item($i++)) $nodeSet[]=self::_newDOMElement($node,$this->myOwnerDocument);
288 function dump_node($node=null) {
return $node==null ? $this->myOwnerDocument->myDOMNode->saveXML($this->myDOMNode) : $this->myOwnerDocument->myDOMNode->saveXML($node->myDOMNode);}
293 function insert_before($newnode,$refnode) {
return self::_newDOMElement($this->myDOMNode->insertBefore($this->_importNode($newnode),$refnode==null?null:$refnode->myDOMNode),$this->myOwnerDocument);}
294 function is_blank_node() {
return ($this->myDOMNode->nodeType===XML_TEXT_NODE)&&preg_match(
'%^\s*$%',$this->myDOMNode->nodeValue);}
298 $mySubNode=$this->myDOMNode->ownerDocument->createElement(
$name);
299 $mySubNode->appendChild($this->myDOMNode->ownerDocument->createTextNode(
_entityDecode($content)));
300 $this->myDOMNode->appendChild($mySubNode);
304 function node_name() {
return ($this->myDOMNode->nodeType===XML_ELEMENT_NODE) ? $this->myDOMNode->localName : $this->myDOMNode->nodeName;}
305 function node_type() {
return $this->myDOMNode->nodeType;}
309 function prefix() {
return $this->myDOMNode->prefix;}
312 function replace_child($newnode,$oldnode) {
return self::_newDOMElement($this->myDOMNode->replaceChild($this->_importNode($newnode),$oldnode->myDOMNode),$this->myOwnerDocument);}
314 function set_content($text) {
return $this->myDOMNode->appendChild($this->myDOMNode->ownerDocument->createTextNode(
_entityDecode($text)));}
318 $nsprefix=$this->myDOMNode->lookupPrefix($uri);
321 $nsprefix= $prefix==null ? $nsprefix=
'a'.sprintf(
'%u',crc32($uri)) : $prefix;
322 if ($this->myDOMNode->nodeType===XML_ATTRIBUTE_NODE)
324 if (($prefix!=null)&&$this->myDOMNode->ownerElement->hasAttributeNS(
'http://www.w3.org/2000/xmlns/',$nsprefix)&&
325 ($this->myDOMNode->ownerElement->getAttributeNS(
'http://www.w3.org/2000/xmlns/',$nsprefix)!=$uri))
327 $parent=$this->myDOMNode->ownerElement;
328 $parent->removeAttributeNode($this->myDOMNode);
329 $parent->setAttribute($this->myDOMNode->localName,$this->myDOMNode->nodeValue);
330 $this->myDOMNode=$parent->getAttributeNode($this->myDOMNode->localName);
333 $this->myDOMNode->ownerElement->setAttributeNS(
'http://www.w3.org/2000/xmlns/',
'xmlns:'.$nsprefix,$uri);
336 if ($this->myDOMNode->nodeType===XML_ATTRIBUTE_NODE)
338 $parent=$this->myDOMNode->ownerElement;
339 $parent->removeAttributeNode($this->myDOMNode);
340 $parent->setAttributeNS($uri,$nsprefix.
':'.$this->myDOMNode->localName,$this->myDOMNode->nodeValue);
341 $this->myDOMNode=$parent->getAttributeNodeNS($uri,$this->myDOMNode->localName);
343 elseif ($this->myDOMNode->nodeType===XML_ELEMENT_NODE)
345 $NewNode=$this->myDOMNode->ownerDocument->createElementNS($uri,$nsprefix.
':'.$this->myDOMNode->localName);
346 foreach ($this->myDOMNode->attributes as $n) $NewNode->appendChild($n->cloneNode(
true));
347 foreach ($this->myDOMNode->childNodes as $n) $NewNode->appendChild($n->cloneNode(
true));
348 $xpath=
new DOMXPath($this->myDOMNode->ownerDocument);
349 $myDOMNodeList=$xpath->query(
'namespace::*[name()!="xml"]',$this->myDOMNode);
350 foreach ($myDOMNodeList as $n) $NewNode->setAttributeNS(
'http://www.w3.org/2000/xmlns/',$n->nodeName,$n->nodeValue);
351 $this->myDOMNode->parentNode->replaceChild($NewNode,$this->myDOMNode);
352 $this->myDOMNode=$NewNode;
357 if ($this->myDOMNode->parentNode!=null)
359 if ($this->myDOMNode->nodeType===XML_ATTRIBUTE_NODE) $this->myDOMNode->parentNode->removeAttributeNode($this->myDOMNode);
360 else $this->myDOMNode->parentNode->removeChild($this->myDOMNode);
363 protected function _importNode($newnode) {
return $this->myOwnerDocument===$newnode->myOwnerDocument ? $newnode->myDOMNode : $this->myOwnerDocument->myDOMNode->importNode($newnode->myDOMNode,
true);}
366 if ($aDOMNode==null)
return null;
367 switch ($aDOMNode->nodeType)
369 case XML_ELEMENT_NODE:
return new php4DOMElement($aDOMNode,$aOwnerDocument);
370 case XML_TEXT_NODE:
return new php4DOMText($aDOMNode,$aOwnerDocument);
371 case XML_ATTRIBUTE_NODE:
return new php4DOMAttr($aDOMNode,$aOwnerDocument);
373 default:
return new php4DOMNode($aDOMNode,$aOwnerDocument);
379 function data() {
return $this->myDOMNode->data;}
380 function target() {
return $this->myDOMNode->target;}
387 if (
$name===
'tagname')
return '#text';
391 function set_content($text) {$this->myDOMNode->nodeValue=$text;
return true;}
394 if (!defined(
'XPATH_NODESET'))
396 define(
'XPATH_UNDEFINED',0);
397 define(
'XPATH_NODESET',1);
398 define(
'XPATH_BOOLEAN',2);
399 define(
'XPATH_NUMBER',3);
400 define(
'XPATH_STRING',4);
416 if (!isset($aDOMNodelist))
return;
417 elseif (is_object($aDOMNodelist)||is_array($aDOMNodelist))
419 if ($aDOMNodelist->length>0)
421 $this->myDOMNodelist=$aDOMNodelist;
422 $this->nodeset=array();
423 $this->type=XPATH_NODESET;
428 elseif (is_int($aDOMNodelist)||is_float($aDOMNodelist))
430 $this->type=XPATH_NUMBER;
431 $this->value=$aDOMNodelist;
433 elseif (is_bool($aDOMNodelist))
435 $this->type=XPATH_BOOLEAN;
436 $this->value=$aDOMNodelist;
438 elseif (is_string($aDOMNodelist))
440 $this->type=XPATH_STRING;
441 $this->value=$aDOMNodelist;
453 $this->myOwnerDocument=$dom_document->myOwnerDocument;
454 $this->myDOMXPath=
new DOMXPath($this->myOwnerDocument->myDOMNode);
458 if (method_exists($this->myDOMXPath,
'evaluate')) $xp=isset($contextnode) ? $this->myDOMXPath->evaluate($eval_str,$contextnode->myDOMNode) : $this->myDOMXPath->evaluate($eval_str);
459 else $xp=isset($contextnode) ? $this->myDOMXPath->query($eval_str,$contextnode->myDOMNode) : $this->myDOMXPath->query($eval_str);
461 return ($xp->type===XPATH_UNDEFINED) ?
false : $xp;
463 function xpath_register_ns($prefix,$namespaceURI) {
return $this->myDOMXPath->registerNamespace($prefix,$namespaceURI);}
466 if (extension_loaded(
'xsl'))
468 function domxml_xslt_stylesheet($xslstring) {
return new php4DomXsltStylesheet(DOMDocument::loadXML($xslstring));}
469 function domxml_xslt_stylesheet_doc($dom_document) {
return new php4DomXsltStylesheet($dom_document);}
470 function domxml_xslt_stylesheet_file($xslfile) {
return new php4DomXsltStylesheet(DOMDocument::load($xslfile));}
471 class php4DomXsltStylesheet
473 private $myxsltProcessor;
474 function php4DomXsltStylesheet($dom_document)
476 $this->myxsltProcessor=
new xsltProcessor();
477 $this->myxsltProcessor->importStyleSheet($dom_document);
479 function process($dom_document,$xslt_parameters=array(),$param_is_xpath=
false)
481 foreach ($xslt_parameters as $param=>$value) $this->myxsltProcessor->setParameter(
'',$param,$value);
483 $myphp4DOMDocument->myDOMNode=$this->myxsltProcessor->transformToDoc($dom_document->myDOMNode);
484 return $myphp4DOMDocument;
486 function result_dump_file($dom_document,
$filename)
488 $html=$dom_document->myDOMNode->saveHTML();
492 function result_dump_mem($dom_document) {
return $dom_document->myDOMNode->saveHTML();}