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
|
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
|
Expressing this program generator in TL using the simple form is shown next.
hello.template
|
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
|
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.).