]> git.sesse.net Git - xml-template/blobdiff - perl/XML/Template.pm
Fix some cleaning bugs.
[xml-template] / perl / XML / Template.pm
index 3d4fad856b1fd4d082dad1a40cc9fec25d071cdc..19fa47082f7e7cb64021ae9be39a0e93c45eae52 100644 (file)
@@ -23,21 +23,7 @@ sub process {
        if (!defined($nsup)) {
                $nsup = XML::NamespaceSupport->new;
        }
-       $nsup->push_context;
-
-       # see if this node contains any namespace declarations that are relevant
-       # for us
-       my $attrs = $node->getAttributes;
-       if (defined($attrs)) {
-               for my $attr ($attrs->getValues) {
-                       my $name = $attr->getName;
-                       if ($name =~ /^xmlns:(.*)$/) {
-                               $nsup->declare_prefix($1, $attr->getValue);
-                               $node->removeAttribute($name) if ($clean);
-                       }
-               }
-       }
-       
+
        if (!ref($obj)) {                                                       # overwrite
                for my $child ($node->getChildNodes) {
                        $node->removeChild($child);
@@ -59,11 +45,27 @@ sub process {
                        $newobj->setOwnerDocument($node->getOwnerDocument);
                }
                $node->appendChild($newobj);
+
+               process($newobj, {}, $clean, $nsup);
        } elsif (ref($obj) eq 'HASH') {                                         # substitute
                for my $child ($node->getChildNodes) {
                        my $processed = 0;
+                       $nsup->push_context;
 
                        if ($child->getNodeType == XML::DOM::ELEMENT_NODE) {
+                               # see if this node contains any namespace declarations that are relevant
+                               # for us
+                               my $attrs = $child->getAttributes;
+                               if (defined($attrs)) {
+                                       for my $attr ($attrs->getValues) {
+                                               my $name = $attr->getName;
+                                               if ($name =~ /^xmlns:(.*)$/) {
+                                                       $nsup->declare_prefix($1, $attr->getValue);
+                                                       $child->removeAttribute($name) if ($clean);
+                                               }
+                                       }
+                               }
+
                                my (undef, undef, $tag) = $nsup->process_element_name($child->getTagName);
 
                                my $id;
@@ -83,8 +85,8 @@ sub process {
                                # check all substitutions to see if we found anything
                                # appropriate
                                for my $key (keys %$obj) {
-                                       if (($key =~ /^\Q$tag\E\.(.*)$/) ||
-                                           (defined($id) && $key =~ /^#\Q$id\E\.(.*)$/)) {
+                                       if (($key =~ /^\Q$tag\E\/(.*)$/) ||
+                                           (defined($id) && $key =~ /^#\Q$id\E\/(.*)$/)) {
                                                $child->setAttribute($1, $obj->{$key});
                                        }
 
@@ -97,6 +99,7 @@ sub process {
                        }
 
                        process($child, $obj, $clean, $nsup) unless ($processed);
+                       $nsup->pop_context;
                }
        } elsif (ref($obj) eq 'ARRAY') {                                        # repeat
                my $doc = $node->getOwnerDocument;
@@ -118,12 +121,9 @@ sub process {
                }
 
                $frag->dispose;
-               $nsup->pop_context;
-               return;
        }
 
        clean($node, $nsup) if $clean;
-       $nsup->pop_context;
 }
 
 sub clean {