From 64c2c7d969fd5c4a10945edf19d29d8f915a327f Mon Sep 17 00:00:00 2001 From: "sgunderson@bigfoot.com" <> Date: Sat, 12 Aug 2006 00:20:52 +0200 Subject: [PATCH] Start on a PHP translation. simple.php works! --- simple.php | 9 ++++++ xml-template.php | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 simple.php create mode 100644 xml-template.php diff --git a/simple.php b/simple.php new file mode 100644 index 0000000..85614d5 --- /dev/null +++ b/simple.php @@ -0,0 +1,9 @@ + 'A very basic example', + '#hello' => 'Hello world!' +)); +print $doc->dump_mem(); +?> diff --git a/xml-template.php b/xml-template.php new file mode 100644 index 0000000..b394b44 --- /dev/null +++ b/xml-template.php @@ -0,0 +1,75 @@ +child_nodes() as $child) { + $node->remove_child($child); + } + $doc = $node->owner_document(); + $node->add_child($doc->create_text_node($obj)); + # handle overwrite with a DOM object here + } else if (is_associative_array($obj)) { # substitute + foreach ($node->child_nodes() as $child) { + $processed = false; + + if ($child->node_type() == XML_ELEMENT_NODE) { + $tag = $child->node_name(); + $attrs = $child->attributes(); + if ($attrs != null) { + foreach ($child->attributes() as $attr) { + # FIXME: xmlns, nsuri? + if ($attr->name() == 'id') { + $id = $attr->value(); + if ($clean) { + $child->remove_attribute($attr->name()); + } + } + } + } + + # 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) || + ($id != null && preg_match('/^#' . $id . '\.(.*)$/', $key, $matches))) { + $child->set_attribute($matches[0], $obj[$key]); + } + + if ($processed) { + continue; + } + if ($key == $tag || ($id != null && $key == ('#'.$id))) { + XML_Template_process($child, $obj[$key], $clean, $nsup); + $processed = true; + } + } + } + + if (!$processed) { + XML_Template_process($child, $obj, $clean, $nsup); + } + } + } + + # FIXME: repeat + # FIXME: clean +} + +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); +} +?> -- 2.39.5