I have a XML that has a t:subscriptions element that holds multiple t:subscription child elements.
My main goal is to replace current t:subsciptions as a whole or all t:subsciption elements with new ones. At the moment I have created new t:subscription elements as strings (didn't get a grasp of dom/simplexml classes so I had to use "fallback" method).
Now I'm trying to convert string to XML by simplexml_load_string and then dom_import_simplexml in order to append/replace current subscriptions in XML. I've tried following example found from this site:
$newsubs = ''; //new XML as string
$sdom = new DOMDocument();
$sdom->loadXML($xml->asXML()); // current XML
//dpm($sdom->saveXML());
$xpath = new DOMXPath($sdom);
// ... x functions generate $newsubs string ...
$remove = $sdom->getElementsByTagName('subscriptions')->item(0); // get t:subscriptions for removal
$remove->parentNode->removeChild($remove); // remove t:subscriptions
$fragment = $sdom->createDocumentFragment();
$fragment->appendXml($newsubs);
$xpaths
->evaluate('//t:member') // this node holds t:subscriptions among others
->item(0) // there's always only one member node
->appendChild($fragment);
debug($sdom->saveXML());
I've come so far that only error received is Namespace prefix X on Y is not defined and this is for all elements. Script removes namespace prefixes although they are defined in xml string eg <t:myelement>asdasd</t:myelement>.
The XML document that this XML is going to be added is already prefixed. I don't get it why it can't accept prefixed XML and strips prefix away and yet new XML get appended to the document? Doesn't make sense.