note 110738 added to domdocument.importnode
| From: | denis dot flaven at combodo dot com | Date: | Thu, 29 Nov 2012 18:14:19 +0000 |
| Subject: | note 110738 added to domdocument.importnode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-192203@lists.php.net to get a copy of this message | ||
Beware: whether ImportNode copies or not the attributes depends on the PHP version.
PHP versions < 5.2.17 DO NOT copy the attributes if bDeep is false.
PHP versions >= 5.2.17 DO copy the attributes even if bDeep is false.
Example:
<?php
$doc = new DOMDocument;
$doc->loadXML('<root><element foo="bar" /></root>');
$root = $doc->documentElement;
$elem = $root->firstChild;
$doc2 = new DOMDocument;
$frag = $doc2->createDocumentFragment();
$frag->appendChild($doc2->importNode($elem, false));
$doc2->appendChild($frag);
echo htmlentities($doc2->saveXML(), ENT_QUOTES, 'UTF-8');
?>
Output for PHP < 5.2.17:
<?xml version="1.0"?> <element/>
Output for PHP >= 5.2.17:
<?xml version="1.0"?> <element foo="bar"/>
If you want to always copy the attributes (but not do a deep copy), do:
$doc2->importNode($elem->clone(false), true)
----
Server IP: 69.147.83.199
Probable Submitter: 217.128.93.129
----
Manual Page -- http://www.php.net/manual/en/domdocument.importnode.php
Edit -- https://master.php.net/note/edit/110738
Del: integrated -- https://master.php.net/note/delete/110738/integrated
Del: useless -- https://master.php.net/note/delete/110738/useless
Del: bad code -- https://master.php.net/note/delete/110738/bad+code
Del: spam -- https://master.php.net/note/delete/110738/spam
Del: non-english -- https://master.php.net/note/delete/110738/non-english
Del: in docs -- https://master.php.net/note/delete/110738/in+docs
Del: other reasons-- https://master.php.net/note/delete/110738
Reject -- https://master.php.net/note/reject/110738
Search -- https://master.php.net/manage/user-notes.php