]> git.sesse.net Git - xml-template/blob - xml-template.php
Start on a PHP translation. simple.php works!
[xml-template] / xml-template.php
1 <?php
2
3 function XML_Template_process_file($filename, $obj, $clean = 1)
4 {
5         $doc = domxml_open_file($filename);
6         XML_Template_process($doc, $obj, $clean);
7         return $doc;
8 }
9
10 function XML_Template_process($node, $obj, $clean = 1, $nsup = array())
11 {
12         if (!is_array($obj)) {                                # overwrite
13                 foreach ($node->child_nodes() as $child) {
14                         $node->remove_child($child);
15                 }
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) {
21                         $processed = false;
22
23                         if ($child->node_type() == XML_ELEMENT_NODE) {
24                                 $tag = $child->node_name();
25                                 $attrs = $child->attributes();
26                                 if ($attrs != null) {
27                                         foreach ($child->attributes() as $attr) {
28                                                 # FIXME: xmlns, nsuri?
29                                                 if ($attr->name() == 'id') {
30                                                         $id = $attr->value();
31                                                         if ($clean) {
32                                                                 $child->remove_attribute($attr->name());
33                                                         }
34                                                 }
35                                         }
36                                 }
37                         
38                                 # check all substitutions to see if we found anything
39                                 # appropriate
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]);
45                                         }
46
47                                         if ($processed) {
48                                                 continue;
49                                         }
50                                         if ($key == $tag || ($id != null && $key == ('#'.$id))) {
51                                                 XML_Template_process($child, $obj[$key], $clean, $nsup);
52                                                 $processed = true;
53                                         }
54                                 }
55                         }
56
57                         if (!$processed) {
58                                 XML_Template_process($child, $obj, $clean, $nsup);
59                         }
60                 }
61         }
62
63         # FIXME: repeat
64         # FIXME: clean
65 }
66
67 function is_associative_array($arr)
68 {
69         if (!is_array($arr)) {
70                 return false;
71         }
72         $diff = array_diff(range(0, count($arr) - 1), array_keys($arr));
73         return (count($diff) > 0);
74 }
75 ?>