note 69607 added to function.dom-domdocument-importnode
| From: | Colin at osu1 dot php dot net | Date: | Tue, 12 Sep 2006 16:11:31 +0000 |
| Subject: | note 69607 added to function.dom-domdocument-importnode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-117068@lists.php.net to get a copy of this message | ||
As of PHP 5.1.6 with libxml 2.6.26 and DOM/XML API version 20031129, importNode() does nothing if
attempting to import from the same document. Meaning, if you do an $ret = importNode and then
appendChild($ret) or insertBefore($ret, ...) then you will end up *moving* the node instead of
ending up with a copy.
If you expect importNode to give you a copy of the source (in this case, a deep copy) then you must
account for them being from the same document. This function addresses this:
<?
// Import $b into $a's document
function myimport($a, $b)
{
if ($a->ownerDocument->isSameNode($b->ownerDocument))
{
$temp = new DOMDocument();
$ret = $temp->importNode($b, TRUE);
return $a->ownerDocument->importNode($ret, TRUE);
}
else
{
return $a->ownerDocument->importNode($b, TRUE);
}
}
?>
(Function was freshly coded for this note but I based it off another, working function of mine.)
This function checks if the documents are the same and uses a new document (guaranteed to be
different this way) to force a copy into $temp and then force a copy back into $a->ownerDocument.
Obviously, no error checking has been done.
----
Server IP: 64.71.164.2
Probable Submitter: 65.125.133.211
----
Manual Page -- http://www.php.net/manual/en/function.dom-domdocument-importnode.php
Edit -- https://master.php.net/note/edit/69607
Del: integrated -- https://master.php.net/note/delete/69607/integrated
Del: useless -- https://master.php.net/note/delete/69607/useless
Del: bad code -- https://master.php.net/note/delete/69607/bad+code
Del: spam -- https://master.php.net/note/delete/69607/spam
Del: non-english -- https://master.php.net/note/delete/69607/non-english
Del: in docs -- https://master.php.net/note/delete/69607/in+docs
Del: other reasons-- https://master.php.net/note/delete/69607
Reject -- https://master.php.net/note/reject/69607
Search -- https://master.php.net/manage/user-notes.php