Kleist wrote:
> I use DOM XML PHP functions to build a document. Is it possible to
> insert a new node as sibling right after the selected by xpath node?
> There is a function insert_before() but it seems that it appends a child?
In PHP 5 it should work alike
node->parentNode->insertBefore(newNode, node->nextSibling);
as in the following complete example
<?php
$xmlDocument = new DOMDocument('1.0', 'UTF-8');
$loaded = $xmlDocument->loadXML('
<gods>
<god name="Kibo" />
<god name="Xibo" />
</gods>');
$newGod = $xmlDocument->createElement('god');
$newGod->setAttribute('name', 'Maho');
$xpathEvaluator = new DOMXPath($xmlDocument);
$oldGod = $xpathEvaluator->query('/gods/god[@name = "Kibo"]',
$xmlDocument)->item(0);
if ($oldGod != NULL) {
$oldGod->parentNode->insertBefore($newGod, $oldGod->nextSibling);
}
header('Content-Type: application/xml');
echo $xmlDocument->saveXML();
?>
--
Martin Honnen
http://JavaScript.FAQTs.com/