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)
12 if (is_a($obj, 'domnode')) { # overwrite
13 foreach ($node->child_nodes() as $child) {
14 $node->remove_child($child);
17 if (is_a($obj, 'domdocument')) {
18 $obj = $obj->document_element();
21 $newobj = own_clone_node($obj, $node->owner_document());
22 $node->append_child($newobj);
24 XML_Template_process($newobj, array(), $clean);
25 } else if (!is_array($obj)) { # overwrite
26 foreach ($node->child_nodes() as $child) {
27 $node->remove_child($child);
29 $doc = $node->owner_document();
30 $node->add_child($doc->create_text_node($obj));
31 } else if (is_associative_array($obj)) { # substitute
32 foreach ($node->child_nodes() as $child) {
35 if ($child->node_type() == XML_ELEMENT_NODE) {
36 $tag = $child->node_name();
37 $attrs = $child->attributes();
39 foreach ($child->attributes() as $attr) {
40 // PHP's DOMXML module forgets the prefix information when
41 // cloning nodes, so we'll have to avoid the namespace check
42 // here, unfortunately
43 if (/* $attr->namespace_uri() == 'http://template.sesse.net/' && */ $attr->name() == 'id') {
46 // FIXME: this won't work since we're not in the right namespace
47 // $child->remove_attribute($attr->name());
53 # check all substitutions to see if we found anything
55 foreach (array_keys($obj) as $key) {
56 # FIXME: we would want something like \Q and \E here...
57 if (preg_match('/^' . $tag . '\/(.*)$/', $key, $matches) ||
58 (isset($id) && preg_match('/^#' . $id . '\/(.*)$/', $key, $matches))) {
59 $child->set_attribute($matches[1], $obj[$key]);
65 if ($key == $tag || (isset($id) && $key == ('#'.$id))) {
66 XML_Template_process($child, $obj[$key], $clean);
73 XML_Template_process($child, $obj, $clean);
77 $doc = $node->owner_document();
78 $frag = $doc->create_element("temporary-fragment"); # ugh
80 foreach ($node->child_nodes() as $child) {
81 $frag->append_child($child);
82 $node->remove_child($child);
85 foreach ($obj as $instance) {
86 $newnode = own_clone_node($frag, $frag->owner_document());
87 $node->append_child($newnode);
88 XML_Template_process($newnode, $instance, $clean);
90 XML_Template_clean($newnode);
94 # remove all the <fragment> tags
96 foreach ($node->child_nodes() as $child) {
97 if ($child->name() != 'temporary-fragment') {
100 foreach ($child->child_nodes() as $child2) {
101 $node->append_child($child2);
103 $node->remove_child($child);
108 XML_Template_clean($node);
112 function XML_Template_clean($node)
114 if ($node->node_type() == XML_ELEMENT_NODE) {
115 if ($node->namespace_uri() != 'http://template.sesse.net/') {
119 # as this is a dummy node, we want to remove it and move everything further up
120 # after we've done any required replacements
121 $doc = $node->owner_document();
122 $parent = $node->parent_node();
123 foreach ($node->child_nodes() as $child) {
124 $node->remove_child($child);
125 $node->insert_before($child, $node);
127 $parent->remove_child($node);
131 # FIXME: use varargs here
132 function XML_Template_alternate($tag, $array, $elems)
135 $num = count($elems);
137 for ($i = 0; $i < count($array); $i++) {
138 $array[$i][$tag] = $elems[$i % $num];
144 # Ideally, this would be "return $obj->clone_node(true)". But surprise, surprise,
145 # PHP is buggy (at least PHP4); it removes the prefix information from all attributes
146 # during a clone. IOW, we'll have to clone evverything ourselves.
147 function own_clone_node($node, $doc)
149 // we only need these two
150 if ($node->node_type() == XML_ELEMENT_NODE) {
151 $newnode = $doc->create_element_ns($node->namespace_uri(), $node->node_name(), $node->prefix());
152 $attrs = $node->attributes();
154 foreach ($node->attributes() as $attr) {
155 // set_attribute_node() does not exist...
156 $prefix = $attr->prefix();
157 if (isset($prefix)) {
158 $newnode->set_attribute($prefix . ":" . $attr->name(), $attr->value());
160 $newnode->set_attribute($attr->name(), $attr->value());
164 foreach ($node->child_nodes() as $child) {
165 $newnode->append_child(own_clone_node($child, $doc));
169 return $node->clone_node(true);
173 function is_associative_array($arr)
175 if (!is_array($arr)) {
178 $diff = array_diff(range(0, count($arr) - 1), array_keys($arr));
179 return (count($diff) > 0);