What I tried and what doesn't work:
Input:
$d = new DOMDocument(); $d->formatOutput = true; // Out of my control: $someEl = $d->createElementNS('http://example.com/a', 'a:some'); // Under my control: $envelopeEl = $d->createElementNS('http://example.com/default', 'envelope'); $d->appendChild($envelopeEl); $envelopeEl->appendChild($someEl); echo $d->saveXML(); $someEl->prefix = null; echo $d->saveXML();Output is invalid XML after substitution:
<?xml version="1.0"?> <envelope xmlns="http://example.com/default"> <a:some xmlns:a="http://example.com/a"/> </envelope> <?xml version="1.0"?> <envelope xmlns="http://example.com/default"> <:some xmlns:a="http://example.com/a" xmlns:="http://example.com/a"/> </envelope>
Note that <a:some> may have children. One solution would be
to create a new <some>, and copy all children from <a:some> to <some>. Is
that the way to go?
ato default) is the same as renaming the node. So this question can be marked as a duplicate of the question "Rename an XML node using PHP", which already has a decent answer.function moveToDefaultNamespace($element) { renameElement($element, $element->localName); }