Hello World Example

TL is used to write templates for program generators whose input uses XML. TL has two forms: a simple form using the character '#' as the delimiter, and a somewhat more verbose XML form. The two forms are easily converted to the other with two translators: Simple2XML and XML2simple (see figure below).

Consider the family of Hello programs that print "Hello X" where X is somebody's name. You need to specify the name in a specification file using XML. We'll use a very simple XML file whose root element is "name".

spec.xml
<?xml version="1.0"?>
<name>Craig</name>

From this specification we'd like to generate a Java program that prints out "Hello Craig". Note that the characters "Hello " will always be written out by the program, and the characters "Craig" come from the XML file and will be different if we provide a different XML file. The program we want to generate is:

HelloWorld.java
public class HelloWorld {
	public static void main(String[] args) {
		System.out.println("Hello Craig");
	}
}

Expressing this program generator in TL using the simple form is shown next.

hello.template
public class HelloWorld {
	public static void main(String[] args) {
		System.out.println("Hello #"//name"#");
	}
}

Note that the template is identical to the generated program except for the variable part in red. Each TL construct begins and ends with a sharp character: #. This particular construct, #"//name"#, is an XPath expression that says to extract the text content from the first "name" element in the XML file.

The XML form of the template file is shown next and can be used with XML tools. The XML element tags are shown in red.

hello.xml
<?xml version="1.0"?>
<tl>public class HelloWorld {
	public static void main(String[] args) {
		System.out.println("Hello <value path="//name"/>");
	}
}</tl>

The diagram below shows the relationship between various files and the tools (redish oval boxes) that are used for translating the files. The colors are used to represent different types of files (java files are yellow, xml files are peach, etc.).