3 function XML_Template_process_file($filename, $obj, $clean = 1)
5 $doc = new DOMDocument;
7 XML_Template_process($doc, $obj, $clean);
11 function XML_Template_process($node, $obj, $clean = 1)
13 if (is_a($obj, 'DOMNode')) { # overwrite
14 while ($node->childNodes->length > 0) {
15 $child = $node->childNodes->item(0);
16 $node->removeChild($child);
19 if (is_a($obj, 'DOMDocument')) {
20 $obj = $obj->documentElement;
23 $frag = $node->ownerDocument->createDocumentFragment();
24 $frag->appendChild($node->ownerDocument->importNode($obj, true));
25 XML_Template_process($frag, array(), $clean);
26 $node->appendChild($frag);
27 } else if (!is_array($obj)) { # overwrite
28 while ($node->childNodes->length > 0) {
29 $node->removeChild($node->firstChild);
31 $doc = $node->ownerDocument;
32 $node->appendChild($doc->createTextNode($obj));
33 } else if (is_associative_array($obj)) { # substitute
34 $num_children = ($node->childNodes == null) ? 0 : $node->childNodes->length;
35 for ($i = 0; $i < $num_children; ++$i) {
36 $child = $node->childNodes->item($i);
39 if ($child->nodeType == XML_ELEMENT_NODE) {
42 $tag = $child->localName;
43 $attrs = $child->attributes;
47 foreach ($child->attributes as $attr) {
48 if ($attr->namespaceURI == 'http://template.sesse.net/' && $attr->name == 'id') {
51 $child->removeAttributeNode($attr);
57 # This seems to be the only way to reliably get rid of the excess
58 # XML namespace declarations.
59 if ($replace_child == 1) {
60 $newchild = own_clone_element($child, $child->ownerDocument);
61 while ($child->childNodes->length > 0) {
62 $newchild->appendChild($child->firstChild);
64 $node->replaceChild($newchild, $child);
69 # check all substitutions to see if we found anything
71 foreach (array_keys($obj) as $key) {
72 # FIXME: we would want something like \Q and \E here...
73 if (preg_match('/^' . $tag . '\/(.*)$/', $key, $matches) ||
74 (isset($id) && preg_match('/^#' . $id . '\/(.*)$/', $key, $matches))) {
75 $child->setAttribute($matches[1], $obj[$key]);
81 if ($key == $tag || (isset($id) && $key == ('#'.$id))) {
82 XML_Template_process($child, $obj[$key], $clean);
89 XML_Template_process($child, $obj, $clean);
93 $doc = $node->ownerDocument;
94 $frag = $doc->createDocumentFragment();
96 while ($node->childNodes->length > 0) {
97 $child = $node->childNodes->item(0);
98 $node->removeChild($child);
99 $frag->appendChild($child);
102 foreach ($obj as $instance) {
103 if (!isset($instance)) {
107 $newnode = own_clone_node($frag, $frag->ownerDocument);
108 XML_Template_process($newnode, $instance, $clean);
110 $node->appendChild($newnode);
112 XML_Template_clean($newnode);
118 XML_Template_clean($node);
122 function XML_Template_clean($node)
124 if ($node->nodeType == XML_ELEMENT_NODE) {
125 if ($node->namespaceURI != 'http://template.sesse.net/') {
129 # as this is a dummy node, we want to remove it and move everything further up
130 # after we've done any required replacements
131 $doc = $node->ownerDocument;
132 $parent = $node->parentNode;
134 while ($node->childNodes->length > 0) {
135 $child = $node->childNodes->item(0);
136 $node->removeChild($child);
137 $parent->insertBefore($child, $node);
139 $parent->removeChild($node);
143 # FIXME: use varargs here
144 function XML_Template_alternate($tag, $array, $elems)
146 $num = count($elems);
148 for ($i = 0, $j = 0; $i < count($array); $i++) {
149 if (isset($array[$i])) {
150 $array[$i][$tag] = $elems[$j++ % $num];
157 # Ideally, this would be "return $obj->clone_node(true)". But surprise,
158 # surprise, PHP is buggy; it does not preserve the prefix information on
159 # attributes properly during a clone. (PHP4 and PHP5 are broken in different
160 # ways here...) IOW, we'll have to clone everything ourselves.
161 function own_clone_node($node, $doc)
163 // we only need these two
164 if ($node->nodeType == XML_DOCUMENT_FRAG_NODE) {
165 $newnode = $doc->createDocumentFragment();
167 for ($i = 0; $i < $node->childNodes->length; ++$i) {
168 $child = $node->childNodes->item($i);
169 $newnode->appendChild(own_clone_node($child, $doc));
172 } else if ($node->nodeType == XML_ELEMENT_NODE) {
173 $newnode = own_clone_element($node, $doc);
174 for ($i = 0; $i < $node->childNodes->length; ++$i) {
175 $child = $node->childNodes->item($i);
176 $newnode->appendChild(own_clone_node($child, $doc));
180 return $node->cloneNode(true);
184 function own_clone_element($node, $doc)
186 $nsuri = $node->namespaceURI;
187 if (isset($nsuri) && $node->prefix != "default") {
188 $newnode = $doc->createElementNS($node->namespaceURI, $node->nodeName, $node->prefix);
190 // remove useless empty text child
191 $newnode->removeChild($newnode->firstChild);
193 $newnode = $doc->createElement($node->localName);
196 $attrs = $node->attributes;
198 foreach ($node->attributes as $attr) {
199 $nsuri = $attr->namespaceURI;
200 if (isset($nsuri) && $attr->prefix != "default") {
201 $attr2 = $doc->createAttributeNS($nsuri, $attr->prefix . ":" . $attr->name);
203 $attr2 = $doc->createAttribute($attr->localName);
205 $attr2->value = $attr->value;
206 $newnode->appendChild($attr2);
212 function is_associative_array($arr)
214 if (!is_array($arr)) {
217 $diff = array_diff(range(0, count($arr) - 1), array_keys($arr));
218 return (count($diff) > 0);