public static function mergeNodes(SimpleXMLElement $element1, SimpleXMLElement $element2)
    {
        $document1 = new DOMDocument();
        $document2 = new DOMDocument();

        $document1->loadXML($element1->asXML());
        $document2->loadXML($element2->asXML());

        $path = new DOMXPath($document2);
        $query = $path->query('/*/*');
        $nodesCount = $query->length;

        if (0 === $nodesCount) {
            return $element1;
        }

        for ($i = 0; $i < $nodesCount; ++$i) {
            $node = $document1->importNode($query->item($i), true);
            $document1->documentElement->appendChild($node);
        }

        return simplexml_import_dom($document1);
    }