XML and Scripting Languages
By Parand Tony Darugar2005-05-18
Simple substitution
A simple method for transforming the XML source into HTML is to define pieces of HTML to be substituted for each XML tag. Using the popular XML::Parser module for Perl (see Resources), based on James Clark's Expat parser, we can parse the XML document and define callback routines for performing the substitutions.
Here is a simple invocation of the XML::Parser:
|
This parses the given file, invoking the function start_handler each time a tag is started, and end_handler each time a tag is ended. The contents of the tag are processed by the char_handler function.
Given these callback functions, we can implement our simple substitution algorithm. First, we define a few substitutions:
|
And now we write the handlers to perform the substitutions:
|
The start_handler function simply prints the value to be substituted for the given tag. char_handler outputs the data it receives, which is the content of the tags. (The full program, with a few additions to handle attributes, is listed separately.) Running the program on our XML file, we get the following output:
|
The full output is available. Using this methodology, we can make simple XML to HTML transformations by defining substitutions.
Tutorial Pages:
» Converting XML to HTML
» Simple substitution
» Function-based substitution
» Tree-based processing
» Active XML documents
» Storing tag contents
» Retrieving the rules
» Acting on the rules
» Next steps
» Resources
First published by IBM DeveloperWorks
