<?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>
*/