X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=php5%2Fxml-template.php;h=e6a4e1909d2f0dc4153fbd644ec6797b0040587c;hb=933565bfd7b14e26bf9dbc2afc7271e28ec34398;hp=66937d24e4a9e59b8b4fc842daecc14151d95767;hpb=c1d4dfa5fe43d3e229cd90a18975fbf4df120f50;p=xml-template diff --git a/php5/xml-template.php b/php5/xml-template.php index 66937d2..e6a4e19 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); } @@ -21,19 +21,24 @@ function XML_Template_process($node, $obj, $clean = 1) } $frag = $node->ownerDocument->createDocumentFragment(); - $frag->appendXML($obj->ownerDocument->saveXML($obj)); + $frag->appendChild($node->ownerDocument->importNode($obj, true)); 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) { - $child = $node->childNodes->item($i); + $num_children = ($node->childNodes == null) ? 0 : $node->childNodes->length; + $children = array(); + for ($i = 0; $i < $num_children; ++$i) { + $children[] = $node->childNodes->item($i); + } + + for ($i = 0; $i < $num_children; ++$i) { + $child = $children[$i]; $processed = false; if ($child->nodeType == XML_ELEMENT_NODE) { @@ -43,13 +48,25 @@ function XML_Template_process($node, $obj, $clean = 1) $attrs = $child->attributes; if (isset($attrs)) { - 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 = 0; + $id_node = $child->getAttributeNodeNS('http://template.sesse.net/', 'id'); + if (isset($id_node)) { + $id = $id_node->value; + if ($clean) { + $child->removeAttributeNode($id_node); + $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; } } @@ -121,7 +138,7 @@ function XML_Template_clean($node) while ($node->childNodes->length > 0) { $child = $node->childNodes->item(0); $node->removeChild($child); - $parent->insertBefore($child, $node); + $parent->insertBefore(own_clone_node($child, $doc), $node); } $parent->removeChild($node); } @@ -141,10 +158,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 +174,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 +184,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 = preg_replace("/&/", "&", $attr->value); + $newnode->appendChild($attr2); + } + } + return $newnode; +} function is_associative_array($arr) {