From: sgunderson@bigfoot.com <> Date: Mon, 21 Apr 2008 19:55:40 +0000 (+0200) Subject: Some fixes for php5 cloning. Still doesn't work properly. X-Git-Url: https://git.sesse.net/?p=xml-template;a=commitdiff_plain;h=1740b1f59b984b42ea1609f64fd7c994b85e121f Some fixes for php5 cloning. Still doesn't work properly. --- diff --git a/php5/xml-template.php b/php5/xml-template.php index 7d0ab19..af05591 100644 --- a/php5/xml-template.php +++ b/php5/xml-template.php @@ -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); } }