protected function moveChildNamespacesToEnvelope(\SimpleXMLElement $element): \SimpleXMLElement
    {
        $dom = dom_import_simplexml($element);

        foreach ($element->getNamespaces(true) as $prefix => $namespace) {
            if (!in_array($namespace, $element->getDocNamespaces())) {
                $dom->setAttributeNS(
                    'http://www.w3.org/2000/xmlns/',
                    $prefix ? 'xmlns:' . $prefix : 'xmlns',
                    $namespace
                );
            }
        }

        return simplexml_import_dom($dom->ownerDocument);
    }