public static function copyElementInNs($oldElement, $newNamespace)
{
$element = $oldElement->ownerDocument->createElementNS($newNamespace, $oldElement->nodeName);
// copy attributes
foreach (iterator_to_array($oldElement->attributes) as $attr) {
$oldElement->removeAttributeNode($attr);
if ($attr->namespaceURI) {
$element->setAttributeNodeNS($attr);
} else {
$element->setAttributeNode($attr);
}
}
// copy children
while ($child = $oldElement->firstChild) {
$oldElement->removeChild($child);
$element->appendChild($child);
}
$oldElement->parentNode->replaceChild($element, $oldElement);
return $element;
}