From: TomTrnka at seznam dot cz Operating system: Linux PHP version: 5CVS-2007-08-13 (snap) PHP Bug Type: DOM XML related Bug description: appendChild breaks loop when copying imported tree
Description: ------------ When a node tree is copied from one DOMDocument to another using recursive importNode on the tree root and then copying the childNodes in a loop (doesn't matter whether for or foreach) using e.g. appendChild, the loop is no more repeated after copying the first element, that means, other child elements do not get copied. Additionally, the source childNodes->length gets decremented. The opposite approach - iterating through the source elements and importing&appending one at a time works flawlessly. On commenting out the appendChild call the iteration goes normally, too. Reproduce code: --------------- <?php error_reporting(E_ALL | E_STRICT); $srcdoc = new DOMDocument("1.0", "UTF-8"); $src = $srcdoc->appendChild($srcdoc->createElement("srcroot")); $src->appendChild($srcdoc->createElement("elem1", "1")); $src->appendChild($srcdoc->createElement("elem2", "2")); echo $srcdoc->saveXML(); $dstdoc = new DOMDocument("1.0", "UTF-8"); $dst = $dstdoc->appendChild($dstdoc->createElement("dstroot")); $tmp = $dstdoc->importNode($src, TRUE); echo $tmp->childNodes->length; foreach ($tmp->childNodes as $child) { echo "."; $dst->appendChild($child); } echo "\n"; echo $dstdoc->saveXML(); ?> Expected result: ---------------- The output should look like this (both elements copied): <?xml version="1.0" encoding="UTF-8"?> <srcroot><elem1>1</elem1><elem2>2</elem2></srcroot> 2.. <?xml version="1.0" encoding="UTF-8"?> <dstroot><elem1>1</elem1><elem2>2</elem2></dstroot> Actual result: -------------- The output looks like this: <?xml version="1.0" encoding="UTF-8"?> <srcroot><elem1>1</elem1><elem2>2</elem2></srcroot> 2. <?xml version="1.0" encoding="UTF-8"?> <dstroot><elem1>1</elem1></dstroot> That means the elem2 did not get copied. There is also only one dot at the "2." row, meaning the second iteration of the loop hasn't even started. -- Edit bug report at http://bugs.php.net/?id=42282&edit=1 -- Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=42282&r=trysnapshot44 Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=42282&r=trysnapshot52 Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=42282&r=trysnapshot60 Fixed in CVS: http://bugs.php.net/fix.php?id=42282&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=42282&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=42282&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=42282&r=needscript Try newer version: http://bugs.php.net/fix.php?id=42282&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=42282&r=support Expected behavior: http://bugs.php.net/fix.php?id=42282&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=42282&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=42282&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=42282&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=42282&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=42282&r=dst IIS Stability: http://bugs.php.net/fix.php?id=42282&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=42282&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=42282&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=42282&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=42282&r=mysqlcfg