note 110738 deleted from domdocument.importnode by aharvey
| From: | aharvey@php.net | Date: | Mon, 03 Dec 2012 01:11:14 +0000 |
| Subject: | note 110738 deleted from domdocument.importnode by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-192251@lists.php.net to get a copy of this message | ||
Note Submitter: denis dot flaven at combodo dot com
----
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)