3 function XML_Template_process_file($filename, $obj, $clean = 1)
5 $doc = domxml_open_file($filename);
6 XML_Template_process($doc, $obj, $clean);
10 function XML_Template_process($node, $obj, $clean = 1, $nsup = array())
12 if (!is_array($obj)) { # overwrite
13 foreach ($node->child_nodes() as $child) {
14 $node->remove_child($child);
16 $doc = $node->owner_document();
17 $node->add_child($doc->create_text_node($obj));
18 # handle overwrite with a DOM object here
19 } else if (is_associative_array($obj)) { # substitute
20 foreach ($node->child_nodes() as $child) {
23 if ($child->node_type() == XML_ELEMENT_NODE) {
24 $tag = $child->node_name();
25 $attrs = $child->attributes();
27 foreach ($child->attributes() as $attr) {
28 # FIXME: xmlns, nsuri?
29 if ($attr->name() == 'id') {
32 $child->remove_attribute($attr->name());
38 # check all substitutions to see if we found anything
40 foreach (array_keys($obj) as $key) {
41 # FIXME: we would want something like \Q and \E here...
42 if (preg_match('/^' . $tag . '\.(.*)$/', $key, $matches) ||
43 ($id != null && preg_match('/^#' . $id . '\.(.*)$/', $key, $matches))) {
44 $child->set_attribute($matches[0], $obj[$key]);
50 if ($key == $tag || ($id != null && $key == ('#'.$id))) {
51 XML_Template_process($child, $obj[$key], $clean, $nsup);
58 XML_Template_process($child, $obj, $clean, $nsup);
62 $doc = $node->owner_document();
63 $frag = $doc->create_element("temporary-fragment"); # ugh
65 foreach ($node->child_nodes() as $child) {
66 $frag->append_child($child);
67 $node->remove_child($child);
70 foreach ($obj as $instance) {
71 $newnode = $frag->clone_node(true);
72 $node->append_child($newnode);
73 XML_Template_process($newnode, $instance, $clean, $nsup);
78 # remove all the <fragment> tags
80 foreach ($node->child_nodes() as $child) {
81 if ($child->name() != 'temporary-fragment') {
84 foreach ($child->child_nodes() as $child2) {
85 $node->append_child($child2);
87 $node->remove_child($child);
96 function is_associative_array($arr)
98 if (!is_array($arr)) {
101 $diff = array_diff(range(0, count($arr) - 1), array_keys($arr));
102 return (count($diff) > 0);