3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $xmlns = 'urn:foo'; $dom = new DOMDocument(); /* On the root level the prefix is replaced by default and the namespace for the attribute is lost */ $root = $dom->createElementNS($xmlns, 'element'); $root->setAttributeNS($xmlns, 'foo:attribute', 'bar'); $dom->appendChild($root); /* On the second level the prefix is replaced, the namespace declaration is duplicated and the element gets a prefix that was never provided. */ $child = $dom->createElementNS($xmlns, 'element'); $child->setAttributeNS($xmlns, 'foo:attribute', 'bar'); $root->appendChild($child); /* On the third level the prefix is replaced and the element gets a prefix again. */ $subchild = $dom->createElementNS($xmlns, 'element'); $subchild->setAttributeNS($xmlns, 'foo:attribute', 'bar'); $child->appendChild($subchild); /* If you add the element node before setting the attribute it gets no prefix */ $child = $dom->createElementNS($xmlns, 'element'); $root->appendChild($child); $child->setAttributeNS($xmlns, 'foo:attribute', 'bar'); $dom->formatOutput = TRUE; echo $dom->saveXml(); /* Expected <?xml version="1.0"?> <element xmlns="urn:foo" xmlns:foo="urn:foo" foo:attribute="bar"> <element foo:attribute="bar"> <element foo:attribute="bar"/> </element> <element foo:attribute="bar"/> </element> */
Output for 5.3.0 - 7.1.0
<?xml version="1.0"?> <element xmlns="urn:foo" xmlns:default="urn:foo" attribute="bar"> <default:element xmlns:default="urn:foo" default:attribute="bar"> <default:element default:attribute="bar"/> </default:element> <element default:attribute="bar"/> </element>
Output for 5.1.0 - 5.2.17
<?xml version="1.0"?> <element xmlns="urn:foo" xmlns:default="urn:foo" attribute="bar"> <element attribute="bar"> <element attribute="bar"/> </element> <element default:attribute="bar"/> </element>
Output for 5.0.0 - 5.0.5
<?xml version="1.0"?> <element xmlns="urn:foo" xmlns:foo="urn:foo" attribute="bar"> <element attribute="bar"> <element attribute="bar"/> </element> <element foo:attribute="bar"/> </element>
Output for 4.3.2 - 4.4.9
Fatal error: Cannot instantiate non-existent class: domdocument in /in/Kreis on line 5
Process exited with code 255.
Output for 4.3.0 - 4.3.1
Fatal error: Cannot instantiate non-existent class: domdocument in /in/Kreis on line 5