]> git.sesse.net Git - xml-template/blobdiff - php/xml-template.php
Horrible workaround for PHP's namespace bugs.
[xml-template] / php / xml-template.php
index dc84bf6e5f8ba023977b997397d3036a9129968f..d7e8aef7428d4817fd03b3121be80271bcc20498 100644 (file)
@@ -20,6 +20,8 @@ function XML_Template_process($node, $obj, $clean = 1)
 
                $newobj = $obj->clone_node(true);
                $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);
@@ -33,12 +35,16 @@ function XML_Template_process($node, $obj, $clean = 1)
                        if ($child->node_type() == XML_ELEMENT_NODE) {
                                $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') {
+                                               // PHP's DOMXML module forgets the prefix information when
+                                               // cloning nodes, so we'll have to avoid the namespace check
+                                               // here, unfortunately
+                                               if (/* $attr->namespace_uri() == 'http://template.sesse.net/' && */ $attr->name() == 'id') {
                                                        $id = $attr->value();
                                                        if ($clean) {
-                                                               $child->remove_attribute($attr->name());
+                                                               // FIXME: this won't work since we're not in the right namespace
+                                                               // $child->remove_attribute($attr->name());
                                                        }
                                                }
                                        }
@@ -48,15 +54,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;
                                        }