X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=php5%2Fxml-template.php;h=ed1e8f85529fc389120184c8023dee97a516518a;hb=a65b22fc1dd7fe224ec09df1c4846334c8ad1ab9;hp=b326e45914beafb84ecf3657317c125bec5e0c0a;hpb=7294f2eaa75313cac1aae6e95bb05ce57185bc01;p=xml-template diff --git a/php5/xml-template.php b/php5/xml-template.php index b326e45..ed1e8f8 100644 --- a/php5/xml-template.php +++ b/php5/xml-template.php @@ -11,7 +11,7 @@ function XML_Template_process_file($filename, $obj, $clean = 1) function XML_Template_process($node, $obj, $clean = 1) { if (is_a($obj, 'DOMNode')) { # overwrite - while ($node->childeNodes->length > 0) { + while ($node->childNodes->length > 0) { $child = $node->childNodes->item(0); $node->removeChild($child); } @@ -25,14 +25,14 @@ function XML_Template_process($node, $obj, $clean = 1) XML_Template_process($frag, array(), $clean); $node->appendChild($frag); } else if (!is_array($obj)) { # overwrite - for ($i = 0; $i < $node->childNodes->length; ++$i) { - $child = $node->childNodes->item($i); - $node->removeChild($child); + while ($node->childNodes->length > 0) { + $node->removeChild($node->firstChild); } $doc = $node->ownerDocument; $node->appendChild($doc->createTextNode($obj)); } else if (is_associative_array($obj)) { # substitute - for ($i = 0; $i < $node->childNodes->length; ++$i) { + $num_children = ($node->childNodes == null) ? 0 : $node->childNodes->length; + for ($i = 0; $i < $num_children; ++$i) { $child = $node->childNodes->item($i); $processed = false; @@ -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 @@ -141,10 +154,10 @@ 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 both PHP4 and PHP5); it removes the prefix information -# from all attributes during a clone. IOW, we'll have to clone evverything -# ourselves. +# Ideally, this would be "return $obj->clone_node(true)". But surprise, +# surprise, PHP is buggy; it does not preserve the prefix information on +# attributes properly during a clone. (PHP4 and PHP5 are broken in different +# ways here...) IOW, we'll have to clone everything ourselves. function own_clone_node($node, $doc) { // we only need these two @@ -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,36 @@ 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); + } + + # You've got to be kidding me... + $attr2->value = htmlentities($attr->value); + $newnode->appendChild($attr2); + } + } + return $newnode; +} function is_associative_array($arr) {