]> git.sesse.net Git - xml-template/blobdiff - php/xml-template.php
Remove unused code in xml-template.php.
[xml-template] / php / xml-template.php
index dc84bf6e5f8ba023977b997397d3036a9129968f..a0c65078a831dfe21f12c39452f582f280cd3429 100644 (file)
@@ -18,8 +18,10 @@ function XML_Template_process($node, $obj, $clean = 1)
                        $obj = $obj->document_element();
                }
 
-               $newobj = $obj->clone_node(true);
+               $newobj = own_clone_node($obj, $node->owner_document());
                $node->append_child($newobj);
+
+               XML_Template_process($newobj, array(), $clean);
        } else if (!is_array($obj)) {                         # overwrite
                foreach ($node->child_nodes() as $child) {
                        $node->remove_child($child);
@@ -31,14 +33,16 @@ function XML_Template_process($node, $obj, $clean = 1)
                        $processed = false;
 
                        if ($child->node_type() == XML_ELEMENT_NODE) {
+                               unset($id);
+
                                $tag = $child->node_name();
                                $attrs = $child->attributes();
-                               if ($attrs != null) {
+                               if (isset($attrs)) {
                                        foreach ($child->attributes() as $attr) {
                                                if ($attr->namespace_uri() == 'http://template.sesse.net/' && $attr->name() == 'id') {
                                                        $id = $attr->value();
                                                        if ($clean) {
-                                                               $child->remove_attribute($attr->name());
+                                                               $attr->unlink_node();
                                                        }
                                                }
                                        }
@@ -48,15 +52,15 @@ function XML_Template_process($node, $obj, $clean = 1)
                                # appropriate
                                foreach (array_keys($obj) as $key) {
                                        # FIXME: we would want something like \Q and \E here...
-                                       if (preg_match('/^' . $tag . '\.(.*)$/', $key, $matches) ||
-                                           ($id != null && preg_match('/^#' . $id . '\.(.*)$/', $key, $matches))) {
+                                       if (preg_match('/^' . $tag . '\/(.*)$/', $key, $matches) ||
+                                           (isset($id) && preg_match('/^#' . $id . '\/(.*)$/', $key, $matches))) {
                                                $child->set_attribute($matches[1], $obj[$key]);
                                        }
 
                                        if ($processed) {
                                                continue;
                                        }
-                                       if ($key == $tag || ($id != null && $key == ('#'.$id))) {
+                                       if ($key == $tag || (isset($id) && $key == ('#'.$id))) {
                                                XML_Template_process($child, $obj[$key], $clean);
                                                $processed = true;
                                        }
@@ -77,7 +81,7 @@ function XML_Template_process($node, $obj, $clean = 1)
                }
 
                foreach ($obj as $instance) {
-                       $newnode = $frag->clone_node(true);
+                       $newnode = own_clone_node($frag, $frag->owner_document());
                        $node->append_child($newnode);
                        XML_Template_process($newnode, $instance, $clean);
                        if ($clean) {
@@ -96,8 +100,6 @@ function XML_Template_process($node, $obj, $clean = 1)
                        }
                        $node->remove_child($child);
                }       
-
-               return;
        }
 
        if ($clean) {
@@ -127,7 +129,6 @@ function XML_Template_clean($node)
 # FIXME: use varargs here
 function XML_Template_alternate($tag, $array, $elems)
 {
-       $i = 0;
        $num = count($elems);
 
        for ($i = 0; $i < count($array); $i++) {
@@ -136,6 +137,40 @@ function XML_Template_alternate($tag, $array, $elems)
 
        return $array;
 }
+               
+# 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.
+function own_clone_node($node, $doc)
+{
+       // we only need these two
+       if ($node->node_type() == XML_ELEMENT_NODE) {
+               $nsuri = $node->namespace_uri();
+               if (isset($nsuri)) {
+                       $newnode = $doc->create_element_ns($node->namespace_uri(), $node->node_name(), $node->prefix());
+               } else {
+                       $newnode = $doc->create_element($node->node_name());
+               }
+               
+               $attrs = $node->attributes();
+               if (isset($attrs)) {
+                       foreach ($node->attributes() as $attr) {
+                               $attr2 = $doc->create_attribute($attr->name(), $attr->value());
+                               $nsuri = $attr->namespace_uri();
+                               if (isset($nsuri)) {
+                                       $attr2->set_namespace($nsuri, $attr->prefix());
+                               }
+                               $newnode->append_child($attr2);
+                       }
+               }
+               foreach ($node->child_nodes() as $child) {
+                       $newnode->append_child(own_clone_node($child, $doc));
+               }
+               return $newnode;
+       } else {
+               return $node->clone_node(true);
+       }
+}
 
 function is_associative_array($arr)
 {