php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22530 append_child does not unlink node
Submitted: 2003-03-04 00:53 UTC Modified: 2003-03-04 07:53 UTC
From: bobsledbob at yahoo dot com Assigned: chregu (profile)
Status: Closed Package: DOM XML related
PHP Version: 4.3.0 OS: Linux
Private report: No CVE-ID: None
 [2003-03-04 00:53 UTC] bobsledbob at yahoo dot com
Quoth the manual:

(PHP >= 4.3) The new child newnode is first unlinked from its existing context, if it already existed in a document. Therefore the node is moved and not copies anymore. This is the behaviour according to the W3C specifications. If you want to duplicate large parts of a xml document, use DomNode->clone_node() before appending.

It seems that the appended node is not unlinked in 4.3.0 as the manual suggests?  Here's my test script:


  $xml  = "<?xml version=\"1.0\" ?>";
  $xml .= "<root>";
  $xml .= "<node1 />";
  $xml .= "<node2 />";
  $xml .= "</root>";

  $dom =& domxml_open_mem($xml);
  $root =& $dom->document_element();

  $nodeArray =& $root->child_nodes();
  $node1 =& $nodeArray[0];
  $node2 =& $nodeArray[1];

  $node1->append_child($node2);

  echo str_replace(" ", "&nbsp;&nbsp;", str_replace("\n", "<br>\n", htmlentities($dom->dump_mem(1))));
  echo "Php Version: " . phpversion() . "<br>\n";

And here's my results:

<?xml version="1.0" ?>
<root>
    <node1>
        <node2/>
    </node1>
    <node2/>
</root>
Php Version: 4.3.0


Thanks.

Adam

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-03-04 05:45 UTC] chregu@php.net
yep. wrong. it just unlinks the node, if it's a child of the parent node ($node1 in your case). I'll fix that.

chregu
 [2003-03-04 06:14 UTC] chregu@php.net
here's the patch against 4.3.0. Didn't apply it now, have to do some tests before...

Index: php_domxml.c
===================================================================
RCS file: /repository/php4/ext/domxml/php_domxml.c,v
retrieving revision 1.218.2.8
diff -u -r1.218.2.8 php_domxml.c
--- php_domxml.c        10 Jan 2003 18:05:02 -0000      1.218.2.8
+++ php_domxml.c        4 Mar 2003 12:13:21 -0000
@@ -2308,8 +2308,8 @@
                RETURN_FALSE;
        }
 
-       /* first unlink node, if child is already a child of parent */
-       if (child->parent == parent){
+       /* first unlink node, if child is already in the tree */
+       if (child->doc == parent->doc && child->parent != NULL){
                xmlUnlinkNode(child);
        }


chregu
 [2003-03-04 07:53 UTC] chregu@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.

And fixed in PHP_4_3 branch 
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 15:01:28 2024 UTC