public static function copyElement(DOMElement $element, DOMElement $parent = null) : DOMElement
    {
        if ($parent === null) {
            $document = DOMDocumentFactory::create();
        } else {
            $document = $parent->ownerDocument;
        }

        $namespaces = [];
        for ($e = $element; $e instanceof DOMNode; $e = $e->parentNode) {
            foreach (Utils::xpQuery($e, './namespace::*') as $ns) {
                $prefix = $ns->localName;
                if ($prefix === 'xml' || $prefix === 'xmlns') {
                    continue;
                }
                $uri = $ns->nodeValue;
                if (!isset($namespaces[$prefix])) {
                    $namespaces[$prefix] = $uri;
                }
            }
        }

        /** @var \DOMElement $newElement */
        $newElement = $document->importNode($element, true);
        if ($parent !== null) {
            /* We need to append the child to the parent before we add the namespaces. */
            $parent->appendChild($newElement);
        }

        foreach ($namespaces as $prefix => $uri) {
            $newElement->setAttributeNS($uri, $prefix.':__ns_workaround__', 'tmp');
            $newElement->removeAttributeNS($uri, '__ns_workaround__');
        }

        return $newElement;
    }
public static function copyElement(DOMElement $element, DOMElement $parent = null) : DOMElement
    {
        if ($parent === null) {
            $document = DOMDocumentFactory::create();
        } else {
            $document = $parent->ownerDocument;
        }

        $namespaces = [];
        for ($e = $element; $e instanceof DOMNode; $e = $e->parentNode) {
            foreach (Utils::xpQuery($e, './namespace::*') as $ns) {
                $prefix = $ns->localName;
                if ($prefix === 'xml' || $prefix === 'xmlns') {
                    continue;
                }
                $uri = $ns->nodeValue;
                if (!isset($namespaces[$prefix])) {
                    $namespaces[$prefix] = $uri;
                }
            }
        }

        /** @var \DOMElement $newElement */
        $newElement = $document->importNode($element, true);
        if ($parent !== null) {
            /* We need to append the child to the parent before we add the namespaces. */
            $parent->appendChild($newElement);
        }

        foreach ($namespaces as $prefix => $uri) {
            $newElement->setAttributeNS($uri, $prefix.':__ns_workaround__', 'tmp');
            $newElement->removeAttributeNS($uri, '__ns_workaround__');
        }

        return $newElement;
    }
public static function copyElement ( DOMElement $ element , DOMElement $ parent = null ) : DOMElement { if ( $ parent === null ) { $ document = DOMDocumentFactory :: create ( ) ; } else { $ document = $ parent -> ownerDocument ; } $ namespaces = [ ] ; for ( $ e = $ element ; $ e instanceof DOMNode ; $ e = $ e -> parentNode ) { foreach ( Utils :: xpQuery ( $ e , './namespace::*' ) as $ ns ) { $ prefix = $ ns -> localName ; if ( $ prefix === 'xml' || $ prefix === 'xmlns' ) { continue ; } $ uri = $ ns -> nodeValue ; if ( ! isset ( $ namespaces [ $ prefix ] ) ) { $ namespaces [ $ prefix ] = $ uri ; } } } /** @var \DOMElement $newElement */ $ newElement = $ document -> importNode ( $ element , true ) ; if ( $ parent !== null ) { /* We need to append the child to the parent before we add the namespaces. */ $ parent -> appendChild ( $ newElement ) ; } foreach ( $ namespaces as $ prefix => $ uri ) { $ newElement -> setAttributeNS ( $ uri , $ prefix . ':__ns_workaround__' , 'tmp' ) ; $ newElement -> removeAttributeNS ( $ uri , '__ns_workaround__' ) ; } return $ newElement ; }
Make an exact copy the specific \DOMElement.

@param \DOMElement $element The element we should copy.
@param \DOMElement|null $parent The target parent element.
@return \DOMElement The copied element.
Make an exact copy the specific \ DOMElement .