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);
}
}
$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) {
$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;
}
}
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);
}
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
}
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));
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)
{