With a sound foundation in XML theory behind you, it's now time to address the other half of the jigsaw - actually converting all that marked-up data into something useful. This first article in a two-part series examines the need, rationale and basic concepts of XSLT, the Extensible Stylesheet Language for Transformations, with sample code and examples.
Now, since this data follows a very simple structure, and moreover I'm not very concerned about the order in which the various records appear, I can format it and present it as HTML with just two XSLT template rules:
The first rule locates the document element and places the standard HTML headers and footers in the corresponding positions in the result tree. Next, the
<xsl:apply-templates />
instruction processes all the children of this node - in this case, these are
all "record" elements, for which there is a corresponding template rule. Each time a record is located via the "record" element, the template rule is invoked and a new fragment added to the result tree. At the end of the process, a composite tree is built out of all the different chunks - and it looks like this:
And that just about covers the essential concepts behind XSL transformations. In the second part of this article, I will be looking at a few of XSLT's more advanced constructs, demonstrating how to add loops and conditional tests to your XSLT templates.
Note: All examples in this article have been tested on Microsoft Internet Explorer 5.5 and Saxon 6.4.3. Examples are illustrative only, and are not meant for a production environment. YMMV!