]> git.sesse.net Git - xml-template/blobdiff - php5/xml-template.php
More cloning fixes. clone.xml doesn't pass, but it passes morally.
[xml-template] / php5 / xml-template.php
index af055917757f63f7902294636b615c15231ab5fe..08c78ab57bcaf4998890628303b5790918c7eb99 100644 (file)
@@ -10,13 +10,13 @@ function XML_Template_process_file($filename, $obj, $clean = 1)
 
 function XML_Template_process($node, $obj, $clean = 1)
 {
-       if (is_a($obj, 'domnode')) {                          # overwrite
+       if (is_a($obj, 'DOMNode')) {                          # overwrite
                for ($i = 0; $i < $node->childNodes->length; ++$i) {
                        $child = $node->childNodes->item($i);
                        $node->removeChild($child);
                }
 
-               if (is_a($obj, 'domdocument')) {
+               if (is_a($obj, 'DOMDocument')) {
                        $obj = $obj->document_element();
                }
 
@@ -39,8 +39,9 @@ function XML_Template_process($node, $obj, $clean = 1)
                        if ($child->nodeType == XML_ELEMENT_NODE) {
                                unset($id);
 
-                               $tag = $child->nodeName;
+                               $tag = $child->localName;
                                $attrs = $child->attributes;
+
                                if (isset($attrs)) {
                                        foreach ($child->attributes as $attr) {
                                                if ($attr->namespaceURI == 'http://template.sesse.net/' && $attr->name == 'id') {
@@ -58,7 +59,7 @@ function XML_Template_process($node, $obj, $clean = 1)
                                        # FIXME: we would want something like \Q and \E here...
                                        if (preg_match('/^' . $tag . '\/(.*)$/', $key, $matches) ||
                                            (isset($id) && preg_match('/^#' . $id . '\/(.*)$/', $key, $matches))) {
-                                               $child->set_attribute($matches[1], $obj[$key]);
+                                               $child->setAttribute($matches[1], $obj[$key]);
                                        }
 
                                        if ($processed) {
@@ -98,16 +99,16 @@ function XML_Template_process($node, $obj, $clean = 1)
                }
 
                # remove all the <fragment> tags
-
                for ($i = 0; $i < $node->childNodes->length; ++$i) {
                        $child = $node->childNodes->item($i);
-                       if ($child->name != 'temporary-fragment') {
+                       if ($child->localName != 'temporary-fragment') {
                                continue;
                        }
-                       for ($j = 0; $j < $child->childNodes->length; ++$j) {
-                               $child2 = $child->childNodes->item($j);
+                       while ($child->childNodes->length > 0) {
+                               $child2 = $child->childNodes->item(0);
                                $node->appendChild($child2);
                        }
+                       --$i;
                        $node->removeChild($child);
                }       
        }
@@ -128,10 +129,11 @@ function XML_Template_clean($node)
                # after we've done any required replacements
                $doc = $node->ownerDocument;
                $parent = $node->parentNode;
-               for ($i = 0; $i < $node->childNodes->length; ++$i) {
-                       $child = $node->childNodes->item($i);
+
+               while ($node->childNodes->length > 0) {
+                       $child = $node->childNodes->item(0);
                        $node->removeChild($child);
-                       $node->insert_before($child, $node);
+                       $parent->insertBefore($child, $node);
                }
                $parent->removeChild($node);
        }
@@ -173,8 +175,7 @@ function own_clone_node($node, $doc)
                                if (isset($nsuri)) {
                                        $attr2 = $doc->createAttribute($attr->name);
                                } else {
-                                       $attr2 = $doc->createAttributeNS($nsuri, $attr->name);
-                                       $attr2->prefix = $attr->prefix;
+                                       $attr2 = $doc->createAttributeNS($nsuri, $attr->prefix . ":" . $attr->name);
                                }
                                $attr2->value = $attr->value;
                                $newnode->appendChild($attr2);