Look out for the following gotcha when loading XML from a string:
<?php
$doc = new \DOMDocument;
$doc->documentURI = $myXmlFilename;
$doc->loadXML($myXmlString);
?>
documentURI is now set to the value of $myXmlFilename, right?
Wrong!
It's set to the current working directory. If you want to manually set documentURI to something other than the CWD, do so AFTER the call to loadXML().
E.g.:
<?php
$doc = new \DOMDocument;
$doc->loadXML($myXmlString);
$doc->documentURI = $myXmlFilename;
?>
documentURI really is now set to the value of $myXmlFilename.