child_nodes() as $child) { $node->remove_child($child); } if (is_a($obj, 'domdocument')) { $obj = $obj->document_element(); } $newobj = own_clone_node($obj, $node->owner_document()); $node->append_child($newobj); XML_Template_process($newobj, array(), $clean); } else if (!is_array($obj)) { # overwrite foreach ($node->child_nodes() as $child) { $node->remove_child($child); } $doc = $node->owner_document(); $node->add_child($doc->create_text_node($obj)); } else if (is_associative_array($obj)) { # substitute foreach ($node->child_nodes() as $child) { $processed = false; if ($child->node_type() == XML_ELEMENT_NODE) { unset($id); $tag = $child->node_name(); $attrs = $child->attributes(); if (isset($attrs)) { foreach ($child->attributes() as $attr) { if ($attr->namespace_uri() == 'http://template.sesse.net/' && $attr->name() == 'id') { $id = $attr->value(); if ($clean) { $attr->unlink_node(); } } } } # check all substitutions to see if we found anything # appropriate foreach (array_keys($obj) as $key) { # FIXME: we would want something like \Q and \E here... if (preg_match('/^' . $tag . '\/(.*)$/', $key, $matches) || (isset($id) && preg_match('/^#' . $id . '\/(.*)$/', $key, $matches))) { $child->set_attribute($matches[1], $obj[$key]); } if ($processed) { continue; } if ($key == $tag || (isset($id) && $key == ('#'.$id))) { XML_Template_process($child, $obj[$key], $clean); $processed = true; } } } if (!$processed) { XML_Template_process($child, $obj, $clean); } } } else { # repeat $doc = $node->owner_document(); $frag = $doc->create_element("temporary-fragment"); # ugh foreach ($node->child_nodes() as $child) { $frag->append_child($child); $node->remove_child($child); } foreach ($obj as $instance) { if (!isset($instance)) { continue; } $newnode = own_clone_node($frag, $frag->owner_document()); $node->append_child($newnode); XML_Template_process($newnode, $instance, $clean); if ($clean) { XML_Template_clean($newnode); } } # remove all the tags foreach ($node->child_nodes() as $child) { if ($child->name() != 'temporary-fragment') { continue; } foreach ($child->child_nodes() as $child2) { $node->append_child($child2); } $node->remove_child($child); } } if ($clean) { XML_Template_clean($node); } } function XML_Template_clean($node) { if ($node->node_type() == XML_ELEMENT_NODE) { if ($node->namespace_uri() != 'http://template.sesse.net/') { return; } # as this is a dummy node, we want to remove it and move everything further up # after we've done any required replacements $doc = $node->owner_document(); $parent = $node->parent_node(); foreach ($node->child_nodes() as $child) { $node->remove_child($child); $node->insert_before($child, $node); } $parent->remove_child($node); } } # FIXME: use varargs here function XML_Template_alternate($tag, $array, $elems) { $num = count($elems); for ($i = 0, $j = 0; $i < count($array); $i++) { if (isset($array[$i])) { $array[$i][$tag] = $elems[$j++ % $num]; } } return $array; } # Ideally, this would be "return $obj->clone_node(true)". But surprise, surprise, # PHP is buggy (at least PHP4); it removes the prefix information from all attributes # during a clone. IOW, we'll have to clone evverything ourselves. function own_clone_node($node, $doc) { // we only need these two if ($node->node_type() == XML_ELEMENT_NODE) { $nsuri = $node->namespace_uri(); if (isset($nsuri)) { $newnode = $doc->create_element_ns($node->namespace_uri(), $node->node_name(), $node->prefix()); } else { $newnode = $doc->create_element($node->node_name()); } $attrs = $node->attributes(); if (isset($attrs)) { foreach ($node->attributes() as $attr) { $attr2 = $doc->create_attribute($attr->name(), $attr->value()); $nsuri = $attr->namespace_uri(); if (isset($nsuri)) { $attr2->set_namespace($nsuri, $attr->prefix()); } $newnode->append_child($attr2); } } foreach ($node->child_nodes() as $child) { $newnode->append_child(own_clone_node($child, $doc)); } return $newnode; } else { return $node->clone_node(true); } } function is_associative_array($arr) { if (!is_array($arr)) { return false; } $diff = array_diff(range(0, count($arr) - 1), array_keys($arr)); return (count($diff) > 0); } ?>