/** * @param \DOMElement $oldElement * @param string $newNamespace * @return mixed */ 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; }
function removeAllAttributes(DOMElement $element) { $attribs = $element->attributes; for ($i = $attribs->length - 1; $i >= 0; $i--) { $element->removeAttributeNode($attribs->item($i)); } }