I am looking to append an xml tree to another.
For example, I want the following xml:
<a>
<b>
<c/>
</b>
</a>
To have the following xml inside it:
<n:d xmlns:xsl="namespace">
<n:e>
<n:f/>
</n:e>
</n:d>
so that it looks like this:
<a>
<b>
<c/>
<n:d xmlns:n="namespace">
<n:e>
<n:f/>
</n:e>
</n:d>
</b>
</a>
The code that I have attempting and failing to do this is as follows:
$doc1 = new DOMDocument();
$doc2 = new DOMDocument();
$doc1->loadXML($xml1);
$doc2->loadXML($xml2);
$node_To_Insert = $doc2->getElementsByTagName('d')->item(0);
$node_To_Be_Inserted_To = $doc1->getElementsByTagName('b')->item(0);
$node_To_Be_Inserted_To->appendChild($doc1->importNode($node_To_Insert));
echo '<pre>'.htmlspecialchars(print_r($doc1->saveXML(),true)).'</pre>';
The current result I get from the echo:
<a>
<b>
<c/>
<n:d xmlns:n="namespace" />
</b>
</a>
I am out of ideas that aren't impossible to read, or aren't seemingly stupidly roundabout.
Any help would be appreciated. Thank you in advance.