associated documentation tweaks) and namespace tests.
to check out the code and this documentation.
There is a lot to be said about design philosophy, but let's first give a
-simple example to give you the feel of how it works. (The example is in
-Perl, but there are also functionally equivalent PHP and Python versions,
-and more languages should probably come soon.)
+simple example to give you the feel of how it works. (The example is in Perl,
+but there are also functionally equivalent PHP, Python and Ruby versions, and
+more languages should probably come soon.)
Template (simple.xml):
the entire DOM with wrappers for each language. (Thankfully, by relying on
the DOM support in each language, the code so far is under 200 lines per
implementation, so maintaining this hopefully shouldn't be much work.)
- As proof-of-concept, I've got Perl, PHP and Python implementations that work
- and feel largely the same -- Ruby and other implementations are welcome.
+ As proof-of-concept, I've got Perl, PHP, Python and Ruby implementations
+ that work and feel largely the same -- other implementations are welcome.
(This is backed up by a test suite, which ensures that all the different
implementations return structurally equivalent XML for a certain set of
test cases. Porting to a new language is not difficult, and once you've
--- /dev/null
+#! /usr/bin/perl
+use XML::Template;
+
+my $doc = XML::Template::process_file('../xml/namespace.xml', {
+ 'title' => 'Namespace tests',
+ '#hello' => 'Hello world!',
+ '#test' => 'Replaced.',
+ 'tagname' => 'foo',
+ '#moretest' => 'bar'
+});
+print $doc->toString;
--- /dev/null
+#! /usr/bin/perl
+use XML::Template;
+
+my $doc = XML::Template::process_file('../xml/namespace2.xml', {
+ 'title' => 'Namespace tests',
+ '#hello' => 'Replaced.',
+});
+print $doc->toString;
--- /dev/null
+<?php
+require('xml-template.php');
+
+$doc = XML_Template_process_file('../xml/namespace.xml', array(
+ 'title' => 'Namespace tests',
+ '#hello' => 'Hello world!',
+ '#test' => 'Replaced.',
+ 'tagname' => 'foo',
+ '#moretest' => 'bar'
+));
+print $doc->dump_mem();
+?>
--- /dev/null
+<?php
+require('xml-template.php');
+
+$doc = XML_Template_process_file('../xml/namespace2.xml', array(
+ 'title' => 'Namespace tests',
+ '#hello' => 'Replaced.',
+));
+print $doc->dump_mem();
+?>
--- /dev/null
+#! /usr/bin/python
+import xmltemplate
+
+doc = xmltemplate.process_file("../xml/namespace.xml", {
+ 'title': 'Namespace tests',
+ '#hello': 'Hello world!',
+ '#test': 'Replaced.',
+ 'tagname': 'foo',
+ '#moretest': 'bar'
+})
+print doc.toxml()
--- /dev/null
+#! /usr/bin/python
+import xmltemplate
+
+doc = xmltemplate.process_file("../xml/namespace2.xml", {
+ 'title': 'Namespace tests',
+ '#hello': 'Replaced.'
+})
+print doc.toxml()
if not attrs is None:
for i in range(attrs.length):
attr = attrs.item(0)
- if attr.namespaceURI == "http://template.sesse.net/" or attr.name == "id":
+ if attr.namespaceURI == "http://template.sesse.net/" and attr.localName == "id":
id = attr.value
if clean:
attrs_to_remove.append(attr.name)
--- /dev/null
+#! /usr/bin/ruby
+require "xmltemplate"
+
+doc = XMLTemplate.process_file("../xml/clone.xml", {
+ 'color' => 'blue',
+ '#things' => []
+});
+print doc.to_s
--- /dev/null
+#! /usr/bin/ruby
+require "xmltemplate"
+
+doc = XMLTemplate.process_file("../xml/clone.xml", {
+ 'color' => 'red',
+ '#things' => [
+ { 'li' => 'Raindrops on roses', 'li/class' => 'odd' },
+ { 'li' => 'Whiskers on kittens', 'li/class' => 'even' },
+ { 'li' => 'Bright copper kettles', 'li/class' => 'odd' },
+ { 'li' => 'Warm, woolen mittens', 'li/class' => 'even' }
+ ]
+});
+print doc.to_s
--- /dev/null
+#! /usr/bin/ruby
+require "xmltemplate"
+
+doc = XMLTemplate.process_file("../xml/clone.xml", {
+ 'color' => 'blue',
+ '#things' => XMLTemplate.alternate("li/class", [
+ { 'li' => 'Raindrops on roses' },
+ { 'li' => 'Whiskers on kittens' },
+ { 'li' => 'Bright copper kettles' },
+ { 'li' => 'Warm, woolen mittens' }
+ ], "odd", "even")
+});
+print doc.to_s
--- /dev/null
+#! /usr/bin/ruby
+require "xmltemplate"
+
+doc = XMLTemplate.process_file("../xml/clone.xml", {
+ 'color' => 'blue',
+ '#things' => XMLTemplate.alternate("li/class", [
+ { 'li' => 'Raindrops on roses' },
+ { 'li' => 'Whiskers on kittens' },
+ nil,
+ { 'li' => 'Bright copper kettles' },
+ { 'li' => 'Warm, woolen mittens' }
+ ], "odd", "even")
+});
+print doc.to_s
--- /dev/null
+#! /usr/bin/ruby
+require "xmltemplate"
+
+doc = XMLTemplate.process_file("../xml/clone.xml", {
+ 'color' => 'blue',
+ '#things' => [
+ { 'li' => 'Raindrops on roses' },
+ { 'li' => 'Whiskers on kittens' },
+ { 'li' => 'Bright copper kettles' },
+ { 'li' => 'Warm, woolen mittens'}
+ ]
+});
+print doc.to_s
--- /dev/null
+#! /usr/bin/ruby
+require "xmltemplate"
+
+doc = XMLTemplate.process_file("../xml/included.xml", {
+ "color" => "red"
+}, false)
+master = XMLTemplate.process_file("../xml/master.xml", {
+ "title" => "Main HTML title",
+ "h1" => "Nice heading here",
+ "contents" => doc
+});
+print master.to_s
--- /dev/null
+#! /usr/bin/ruby
+require "xmltemplate"
+
+doc = XMLTemplate.process_file("../xml/namespace.xml", {
+ 'title' => 'Namespace tests',
+ '#hello' => 'Hello world!',
+ '#test' => 'Replaced.',
+ 'tagname' => 'foo',
+ '#moretest' => 'bar'
+})
+print doc.to_s
--- /dev/null
+#! /usr/bin/ruby
+require "xmltemplate"
+
+doc = XMLTemplate.process_file("../xml/namespace2.xml", {
+ 'title' => 'Namespace tests',
+ '#hello' => 'Replaced.'
+})
+print doc.to_s
--- /dev/null
+#! /usr/bin/ruby
+require "xmltemplate"
+
+doc = XMLTemplate.process_file("../xml/passthru.xml", {})
+print doc.to_s
--- /dev/null
+#! /usr/bin/ruby
+require "xmltemplate"
+
+doc = XMLTemplate.process_file("../xml/simple.xml", {
+ 'title' => 'A very basic example',
+ '#hello' => 'Hello world!'
+})
+print doc.to_s
--- /dev/null
+require "rexml/document"
+
+module XMLTemplate
+ def XMLTemplate.process_file(filename, obj, clean = true)
+ file = File.new(filename)
+ doc = REXML::Document.new file
+ XMLTemplate.process!(doc, obj, clean)
+ return doc
+ end
+
+ def XMLTemplate.process!(node, obj, clean = true)
+ if obj.is_a?(String) # overwrite
+ node.children.delete_if { true }
+ node.text = obj
+ elsif obj.is_a?(REXML::Element) # overwrite
+ if obj.is_a?(REXML::Document)
+ obj = obj.root
+ end
+
+ node.children.delete_if { true }
+
+ newobj = obj.deep_clone
+ node.add(newobj)
+
+ process!(newobj, {}, clean)
+ elsif obj.is_a?(Hash) and node.is_a?(REXML::Element) # substitute
+ node.children.each do |child|
+ processed = false
+ attributes_to_delete = []
+
+ if child.is_a?(REXML::Element)
+ if child.namespace == "http://template.sesse.net/"
+ id = child.local_name
+ else
+ # workaround for .to_a being braindamaged
+ attr_list = []
+ child.attributes.each_attribute { |a| attr_list.push(a) }
+ id_attr = attr_list.find { |a|
+ a.namespace == "http://template.sesse.net/" and a.local_name == "id"
+ }
+ if id_attr.nil?
+ id = nil
+ else
+ id = id_attr.value
+ end
+ end
+
+ if clean
+ child.attributes.each_attribute do |a|
+ if a.namespace == "http://template.sesse.net/" then
+ attributes_to_delete.push(a)
+ elsif a.prefix == "xmlns" and a.value == "http://template.sesse.net/" then
+ attributes_to_delete.push(a)
+ end
+ end
+ end
+
+ # check all substitutions to see if we fuond anything appropriate
+ obj.keys.each do |key|
+ if (Regexp.new("^" + Regexp.escape(child.local_name) + "/") =~ key) or
+ (not id.nil? and Regexp.new("^#" + Regexp.escape(id) + "/") =~ key)
+ child.attributes[key.split("/")[1]] = obj[key]
+ end
+
+ if not processed
+ if key == child.local_name or (not id.nil? and key == "#" + id)
+ process!(child, obj[key], clean)
+ processed = true
+ end
+ end
+ end
+ end
+
+ if not processed
+ process!(child, obj, clean)
+ end
+
+ #
+ # We need to do this _after_ processing all the sub-parts,
+ # since if we remove xmlns: attributes before processing,
+ # REXML loses track of the namespaces of the child elements
+ # (since the prefix declaration is no longer there).
+ #
+ attributes_to_delete.each { |a| child.attributes.delete(a) }
+ end
+ elsif obj.is_a?(Array) # repeat
+ doc = node.document
+ frag = REXML::Element.new
+
+ node.children.each { |e| frag.add(e) }
+ node.children.delete_if { true }
+
+ obj.find_all { |x| not x.nil? } .each do |instance|
+ newnode = frag.deep_clone
+ node.add(newnode)
+ process!(newnode, instance, clean)
+ end
+ end
+
+ if clean then
+ clean!(node)
+ end
+ end
+
+ def XMLTemplate.alternate(tag, array, *elems)
+ i = 0
+ array_copy = array.clone
+
+ array_copy.each do |ref|
+ if not ref.nil?
+ ref[tag] = elems[i % elems.length]
+ i = i + 1
+ end
+ end
+
+ return array_copy
+ end
+
+ def XMLTemplate.clean!(node)
+ if node.is_a?(REXML::Element) and (node.namespace == "http://template.sesse.net/" or node.name == "UNDEFINED")
+ # as this is a dummy node, we want to remove it and move everything further up
+ # after we've done any required replacements
+ node.children.each { |n| node.parent.insert_before(node, n) }
+ node.parent.delete(node)
+ end
+ end
+end
+
--- /dev/null
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
+ <head>
+ <title>Namespace tests</title>
+ </head>
+ <body>
+ <p>Hello world!</p>
+ <p id="hello">This will not.</p>
+ <p id="test">Replaced.</p>
+ foo
+ </body>
+</html>
--- /dev/null
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
+ <head>
+ <title>Namespace tests</title>
+ </head>
+ <body>
+ <p id="test">Replaced.</p>
+ <p id="hello">This will not.</p>
+ </body>
+</html>
#! /bin/sh
if [ -z "$TESTS" ]; then
- TESTS="passthru simple clone include attribute attribute2 attribute3 attribute-empty"
+ TESTS="passthru simple clone include attribute attribute2 attribute3 attribute-empty namespace namespace2"
fi
if [ -z "$LANGUAGES" ]; then
- LANGUAGES="perl perl-sax php python"
+ LANGUAGES="perl perl-sax php python ruby"
fi
for L in $LANGUAGES; do
if [ "$L" = "python" ]; then
python ../python/$T.py > $TEMPFILE
fi
+ if [ "$L" = "ruby" ]; then
+ ruby -I../ruby ../ruby/$T.rb > $TEMPFILE
+ fi
perl ./xml-diff.pl $TEMPFILE reference/$T.xml
if [ $? = 0 ]; then
--- /dev/null
+<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
+<!DOCTYPE
+ html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
+ "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xmlns:t="http://template.sesse.net/" xml:lang="en">
+ <head>
+ <title />
+ </head>
+ <body>
+ <p t:id="hello">This will be replaced.</p>
+ <p id="hello">This will not.</p>
+ <p id="test" t:id="test">This will also be replaced, but the id will stay.</p>
+ <t:tagname id="moretest">This will say "foo", not "bar".</t:tagname>
+ </body>
+</html>
--- /dev/null
+<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
+<!DOCTYPE
+ html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
+ "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xmlns:t="http://template.sesse.net/" xml:lang="en">
+ <head>
+ <title />
+ </head>
+ <body>
+ <p t:id="hello" id="test">This will be replaced.</p>
+ <p t:id="test" id="hello">This will not.</p>
+ </body>
+</html>