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);

preferences:
25.51 ms | 407 KiB | 5 Q