XML to HTML table
These scripts illustrate
ways in which a tabular XML document can be transformed to an HTML table
A script specific to this table.
It is tedious to write a hard-coded script. If the structure of the file
changes then the script would also have to change. An alternative is write a
generic function which, using reflection on the data itself, could transform any tabular
XML. To do this we use the XPath function fn:name() to obtain the name of an element, and the expression $row/*[name(.)= $header] to access the element in a sequence with a variable name.
In this script we assume all items in the sequence have the same simple element
structure determined by the structure of the first item.
If there are no missing elements in any rows, a faster algorithm is possible.
Using element constructors
The generic function produces a table but headings and the order of columns is
determined by the data alone. To get more control over the format, we can create a
configuration file. This also allows us to define an alternative column heading.
You might also want to transform data for presentation. We could add XPath expressions to the schema.
There is a performance penalty for using reflection. An alternative would be
to generate code which could then be stored and compiled.
The generated code is included in a script.
We could generated the code and use util:eval to evaluate it in one script.