Currently I'm taking the results of a xpath query and transferring the nodelist to a new xmldocument for output:
$doc = new DOMDocument();
$doc->loadxml($results);
$xpath = new DOMXpath($doc);
$xpathResults = $xpath->query("/results/result[contains(status,'$status')]");
$nodes = $xpathResults->length;
if($nodes > 0)
{
$newDom = new DOMDocument('1.0','UTF-8');
$root = $newDom->createElement('results');
$root = $newDom->appendChild($root);
foreach ($xpathResults as $domElement)
{
$domNode = $newDom->importNode($domElement, true);
$root->appendChild($domNode);
$newResults = $newDom->saveXML();
}
header("Content-type: application/xml");
echo $newResults;
}
I'm beating my head against the wall trying how to insert a single child after the root called count
<results>
<count>$nodes</count>
//imported node list here
<result>...</result>
//
</results>
no matter what I've toyed with I cannot get it to insert properly without destroying the element ordering and causing errors