]> git.sesse.net Git - xml-template/commitdiff
Some fixes for php5 cloning. Still doesn't work properly.
authorsgunderson@bigfoot.com <>
Mon, 21 Apr 2008 19:55:40 +0000 (21:55 +0200)
committersgunderson@bigfoot.com <>
Mon, 21 Apr 2008 19:55:40 +0000 (21:55 +0200)
php5/xml-template.php

index 7d0ab1985102e734fa256deaa0111492b4bd5960..af055917757f63f7902294636b615c15231ab5fe 100644 (file)
@@ -79,10 +79,9 @@ function XML_Template_process($node, $obj, $clean = 1)
                $doc = $node->ownerDocument;
                $frag = $doc->createElement("temporary-fragment");    # ugh
 
-               for ($i = 0; $i < $node->childNodes->length; ++$i) {
-                       $child = $node->childNodes->item($i);
+               while ($node->childNodes->length > 0) {
+                       $child = $node->childNodes->item(0);
                        $frag->appendChild($child);
-                       $node->removeChild($child);
                }
 
                foreach ($obj as $instance) {
@@ -153,15 +152,16 @@ function XML_Template_alternate($tag, $array, $elems)
 }
                
 # Ideally, this would be "return $obj->clone_node(true)". But surprise, surprise,
-# PHP is buggy (at least PHP4); it removes the prefix information from all attributes
-# during a clone. IOW, we'll have to clone evverything ourselves.
+# PHP is buggy (at least both PHP4 and PHP5); it removes the prefix information
+# from all attributes during a clone. IOW, we'll have to clone evverything
+# ourselves.
 function own_clone_node($node, $doc)
 {
        // we only need these two
        if ($node->nodeType == XML_ELEMENT_NODE) {
                $nsuri = $node->namespaceURI;
                if (isset($nsuri)) {
-                       $newnode = $doc->createElementNS($node->namespaceURI, $node->nodeName, $node->prefix());
+                       $newnode = $doc->createElementNS($node->namespaceURI, $node->nodeName, $node->prefix);
                } else {
                        $newnode = $doc->createElement($node->nodeName);
                }
@@ -169,11 +169,14 @@ function own_clone_node($node, $doc)
                $attrs = $node->attributes;
                if (isset($attrs)) {
                        foreach ($node->attributes as $attr) {
-                               $attr2 = $doc->createAttribute($attr->name, $attr->value);
                                $nsuri = $attr->namespaceURI;
                                if (isset($nsuri)) {
-                                       $attr2->set_namespace($nsuri, $attr->prefix);
+                                       $attr2 = $doc->createAttribute($attr->name);
+                               } else {
+                                       $attr2 = $doc->createAttributeNS($nsuri, $attr->name);
+                                       $attr2->prefix = $attr->prefix;
                                }
+                               $attr2->value = $attr->value;
                                $newnode->appendChild($attr2);
                        }
                }
@@ -183,7 +186,7 @@ function own_clone_node($node, $doc)
                }
                return $newnode;
        } else {
-               return $node->clone_node(true);
+               return $node->cloneNode(true);
        }
 }