From 13fb5825f2cef59f70b8ad7518a460a3e43c4f41 Mon Sep 17 00:00:00 2001 From: "sgunderson@bigfoot.com" <> Date: Mon, 21 Apr 2008 23:46:55 +0200 Subject: [PATCH] Add some hairy magic for removing excess namespaces. --- php5/xml-template.php | 62 +++++++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 20 deletions(-) diff --git a/php5/xml-template.php b/php5/xml-template.php index b326e45..43b8b57 100644 --- a/php5/xml-template.php +++ b/php5/xml-template.php @@ -43,14 +43,27 @@ function XML_Template_process($node, $obj, $clean = 1) $attrs = $child->attributes; if (isset($attrs)) { + $replace_child = 0; foreach ($child->attributes as $attr) { if ($attr->namespaceURI == 'http://template.sesse.net/' && $attr->name == 'id') { $id = $attr->value; if ($clean) { $child->removeAttributeNode($attr); + $replace_child = 1; } } } + + # This seems to be the only way to reliably get rid of the excess + # XML namespace declarations. + if ($replace_child == 1) { + $newchild = own_clone_element($child, $child->ownerDocument); + while ($child->childNodes->length > 0) { + $newchild->appendChild($child->firstChild); + } + $node->replaceChild($newchild, $child); + $child = $newchild; + } } # check all substitutions to see if we found anything @@ -157,26 +170,7 @@ function own_clone_node($node, $doc) } return $newnode; } else if ($node->nodeType == XML_ELEMENT_NODE) { - $nsuri = $node->namespaceURI; - if (isset($nsuri) && $node->prefix != "default") { - $newnode = $doc->createElementNS($node->namespaceURI, $node->nodeName, $node->prefix); - } else { - $newnode = $doc->createElement($node->localName); - } - - $attrs = $node->attributes; - if (isset($attrs)) { - foreach ($node->attributes as $attr) { - $nsuri = $attr->namespaceURI; - if (isset($nsuri) && $attr->prefix != "default") { - $attr2 = $doc->createAttributeNS($nsuri, $attr->prefix . ":" . $attr->name); - } else { - $attr2 = $doc->createAttribute($attr->localName); - } - $attr2->value = $attr->value; - $newnode->appendChild($attr2); - } - } + $newnode = own_clone_element($node, $doc); for ($i = 0; $i < $node->childNodes->length; ++$i) { $child = $node->childNodes->item($i); $newnode->appendChild(own_clone_node($child, $doc)); @@ -186,6 +180,34 @@ function own_clone_node($node, $doc) return $node->cloneNode(true); } } + +function own_clone_element($node, $doc) +{ + $nsuri = $node->namespaceURI; + if (isset($nsuri) && $node->prefix != "default") { + $newnode = $doc->createElementNS($node->namespaceURI, $node->nodeName, $node->prefix); + + // remove useless empty text child + $newnode->removeChild($newnode->firstChild); + } else { + $newnode = $doc->createElement($node->localName); + } + + $attrs = $node->attributes; + if (isset($attrs)) { + foreach ($node->attributes as $attr) { + $nsuri = $attr->namespaceURI; + if (isset($nsuri) && $attr->prefix != "default") { + $attr2 = $doc->createAttributeNS($nsuri, $attr->prefix . ":" . $attr->name); + } else { + $attr2 = $doc->createAttribute($attr->localName); + } + $attr2->value = $attr->value; + $newnode->appendChild($attr2); + } + } + return $newnode; +} function is_associative_array($arr) { -- 2.39.2