3v4l.org

run code in 300+ PHP versions simultaneously
<?php $doc = new DOMDocument(); $doc->loadXML( <<<XML <container xmlns="http://symfony.com/schema/dic/services"> CHILDREN </container> XML ); // // Remove xmlns Attribute + Validate it is actually gone. // As you can see, it still remembers a 'xmlns' DOMNameSpaceNode with an empty namespaceURI (which basically is invalid!) // $doc->documentElement->removeAttributeNS('http://symfony.com/schema/dic/services', ''); var_dump($doc->documentElement->getAttributeNode('xmlns')); $namespaces = [...(new DOMXPath($doc))->query('./namespace::*', $doc->documentElement)]; // // Yet when saving the XML : there is no problem, the namespaces are gone. // echo $doc->saveXML(); var_dump($namespaces); // // However ... // When importing the node in a new document, it gets prefixed by <default:container ...> without any xmlns:default declaration // $new = new DOMDocument(); $new->append( $new->importNode($doc->documentElement, true) ); echo $new->saveXML(); // Resulting in invalid XML obviously: $previousErrorReporting = libxml_use_internal_errors(true); libxml_clear_errors(); $parsedAgain = new DOMDocument(); $parsedAgain->loadXML($new->saveXML()); $errors = libxml_get_errors(); libxml_clear_errors(); libxml_use_internal_errors($previousErrorReporting); var_dump($errors);
Output for 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
bool(false) <?xml version="1.0"?> <container> CHILDREN </container> array(1) { [0]=> object(DOMNameSpaceNode)#4 (10) { ["nodeName"]=> string(9) "xmlns:xml" ["nodeValue"]=> string(36) "http://www.w3.org/XML/1998/namespace" ["nodeType"]=> int(18) ["prefix"]=> string(3) "xml" ["localName"]=> string(3) "xml" ["namespaceURI"]=> string(36) "http://www.w3.org/XML/1998/namespace" ["isConnected"]=> bool(true) ["ownerDocument"]=> string(22) "(object value omitted)" ["parentNode"]=> string(22) "(object value omitted)" ["parentElement"]=> string(22) "(object value omitted)" } } <?xml version="1.0"?> <container> CHILDREN </container> array(0) { }
Output for 8.1.27 - 8.1.33, 8.2.14 - 8.2.29
bool(false) <?xml version="1.0"?> <container> CHILDREN </container> array(1) { [0]=> object(DOMNameSpaceNode)#4 (8) { ["nodeName"]=> string(9) "xmlns:xml" ["nodeValue"]=> string(36) "http://www.w3.org/XML/1998/namespace" ["nodeType"]=> int(18) ["prefix"]=> string(3) "xml" ["localName"]=> string(3) "xml" ["namespaceURI"]=> string(36) "http://www.w3.org/XML/1998/namespace" ["ownerDocument"]=> string(22) "(object value omitted)" ["parentNode"]=> string(22) "(object value omitted)" } } <?xml version="1.0"?> <container> CHILDREN </container> array(0) { }
Output for 8.1.21 - 8.1.26, 8.2.8 - 8.2.13
object(DOMNameSpaceNode)#3 (8) { ["nodeName"]=> string(5) "xmlns" ["nodeValue"]=> NULL ["nodeType"]=> int(18) ["prefix"]=> string(0) "" ["localName"]=> string(5) "xmlns" ["namespaceURI"]=> NULL ["ownerDocument"]=> string(22) "(object value omitted)" ["parentNode"]=> string(22) "(object value omitted)" } <?xml version="1.0"?> <container> CHILDREN </container> array(2) { [0]=> object(DOMNameSpaceNode)#4 (8) { ["nodeName"]=> string(9) "xmlns:xml" ["nodeValue"]=> string(36) "http://www.w3.org/XML/1998/namespace" ["nodeType"]=> int(18) ["prefix"]=> string(3) "xml" ["localName"]=> string(3) "xml" ["namespaceURI"]=> string(36) "http://www.w3.org/XML/1998/namespace" ["ownerDocument"]=> string(22) "(object value omitted)" ["parentNode"]=> string(22) "(object value omitted)" } [1]=> object(DOMNameSpaceNode)#5 (8) { ["nodeName"]=> string(5) "xmlns" ["nodeValue"]=> NULL ["nodeType"]=> int(18) ["prefix"]=> string(0) "" ["localName"]=> string(5) "xmlns" ["namespaceURI"]=> NULL ["ownerDocument"]=> string(22) "(object value omitted)" ["parentNode"]=> string(22) "(object value omitted)" } } <?xml version="1.0"?> <default:container> CHILDREN </default:container> array(1) { [0]=> object(LibXMLError)#7 (6) { ["level"]=> int(2) ["code"]=> int(201) ["column"]=> int(19) ["message"]=> string(53) "Namespace prefix default on container is not defined " ["file"]=> string(0) "" ["line"]=> int(2) } }
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.20, 8.2.0 - 8.2.7
object(DOMNameSpaceNode)#3 (8) { ["nodeName"]=> string(5) "xmlns" ["nodeValue"]=> NULL ["nodeType"]=> int(18) ["prefix"]=> string(0) "" ["localName"]=> string(5) "xmlns" ["namespaceURI"]=> NULL ["ownerDocument"]=> string(22) "(object value omitted)" ["parentNode"]=> NULL } <?xml version="1.0"?> <container> CHILDREN </container> array(2) { [0]=> object(DOMNameSpaceNode)#4 (8) { ["nodeName"]=> string(9) "xmlns:xml" ["nodeValue"]=> string(36) "http://www.w3.org/XML/1998/namespace" ["nodeType"]=> int(18) ["prefix"]=> string(3) "xml" ["localName"]=> string(3) "xml" ["namespaceURI"]=> string(36) "http://www.w3.org/XML/1998/namespace" ["ownerDocument"]=> string(22) "(object value omitted)" ["parentNode"]=> string(22) "(object value omitted)" } [1]=> object(DOMNameSpaceNode)#5 (8) { ["nodeName"]=> string(5) "xmlns" ["nodeValue"]=> NULL ["nodeType"]=> int(18) ["prefix"]=> string(0) "" ["localName"]=> string(5) "xmlns" ["namespaceURI"]=> NULL ["ownerDocument"]=> string(22) "(object value omitted)" ["parentNode"]=> string(22) "(object value omitted)" } } <?xml version="1.0"?> <default:container> CHILDREN </default:container> array(1) { [0]=> object(LibXMLError)#3 (6) { ["level"]=> int(2) ["code"]=> int(201) ["column"]=> int(19) ["message"]=> string(53) "Namespace prefix default on container is not defined " ["file"]=> string(0) "" ["line"]=> int(2) } }

preferences:
99.66 ms | 418 KiB | 5 Q