|
![]() |
|
Article from July, 2001. XSL Tutorial, Part 2: XSLT OperationBy Brian E. Travis Brian Travis is founder and Chief Technical Officer of Architag International Corporation and Managing Editor of <TAG>. Abstract
This is part 2 of a multiple-part series on XSLT, a very important W3C standard that provides a way of transforming XML documents from one structure to another. XSLT can be used to create HTML, so your XML documents can be viewed in a Web browser, or XSL can be used to transform your XML documents to any other XML structure, or even non-XML structures. In this part, you will learn about the programming model for the XSLT programming language, and understand the event-driven, rules-based architecture of the language. Important Bulletin! XSLT is Not a Stylesheet LanguageWhat? But "Stylesheet Language" is its middle name. Right? In last month's article ( http://architag.com/tag/Article.html?v=15&i=6&p=1&s=2 ), I gave a little background about XSL and XSLT. I would define a stylesheet language as some syntax for allowing you to format an information set for delivery using styles. While XSL is a stylesheet language because it provides that formatting information ability, XSLT is clearly not a stylesheet language. XSLT has nothing to do with formatting information. You can use XSLT to create a formatted representation of an information set. In fact, we will do that in this article, and more in depth next month. But, you can also use XSLT where no formatting is ever to take place. For example, in transforming a business document like an invoice or medical record, from one vendor's schema to another. XSLT can do this easily, and there is no intent to create a formatted output, in this case. Rules-Based, Event-Driven Programming LanguageXSLT is nothing less than a programming language. But not a programming language like C or Java. XSLT is not a procedural language like ones you might be used to. XSLT is a declarative language based on rules. This makes XSLT "rules-based". Rules are self-contained objects that do something once they are started. But XSLT's rules are not started by a traditional function call or program invocation. XSLT rules are fired when certain events occur in the XML document being converted. This makes XSLT "event-driven" XSLT rules are called "templates", and are specified using the <xsl:template> element. A template will only execute (fire) when an event occurs in the XML document being transformed. The event that the template is to catch is defined using the match attribute on the xsl:template start tag. Each template contains processing instructions to tell the XSLT processor how to treat the object that fired the rule. I like to think of an XSLT template rule as black box that has known inputs and outputs, but performs some set of processes that can be used by the transformation program or other templates. My black box is illustrated in Figure 1.
Figure 1: An XSLT template is like a black box with known inputs and outputs.
Each XSLT template rule is defined by the match attribute. This is what the XSLT event-processor looks at when it is deciding which template to activate for a given event. A Room Full of Boxes
Figure 2: An XSLT transformation program is like a room full of boxes that are processed by the XSLT processor according to the rules inside the templates.
The processing happens in a box. Most of the time something comes out of that box. Depending on what the box does, this output might be static text, the result of calculations, or an element structure. The result that comes out will either go directly to the output or will be exposed to other boxes, which will process it further. I will show later how this process is done using the XSL transformation engine. In the following example, I'll walk you through the code shown bwloe with the simple XML document:
<?xml version="1.0"?>
<weather type="Current conditions">
<temp>76
<wind>5
</weather>
The the XSLT style sheet is shown below.
1 <?xml version="1.0"?> 2 <xsl:stylesheet 3 version="1.0" 4 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 5 6 <xsl:template match="weather"> 7 <H1><xsl:value-of select="@type"/></H1> 8 <xsl:apply-templates/> 9 <HR/> 10 </xsl:template> 11 12 <xsl:template match="temp"> 13 <LI>Temperature <xsl:apply-templates/>°F</LI> 14 </xsl:template> 15 16 <xsl:template match="wind"> 17 <LI>Wind <xsl:apply-templates/>mph</LI> 18 </xsl:template> 19 20 <xsl:template match="text()"> 21 <xsl:value-of select="."/> 22 </xsl:template> 23 24 </xsl:stylesheet> The process of creating output is described in the table below. The numbers in the first column correspond to the numbers in Figure 2.
Step Description
1 One of the boxes is designed to process the weather element
in the XML document. Since the weather element is the root
element, the weather element and all of its descendants
(an important fact to remember) are stuffed into the box.
This box, with a square hole (as shown in Figure 2), is
defined in the template rule on lines 6-10.
2 The first thing the box produces is some output. Using the
<xsl:value-of/> element creates the text contents of the
object in the select attribute. In this case, the content
of the type attribute (@type) are sent out inside an HTML
<H1> element. The XSL processor processes any element with
the xsl: namespace prefix. Anything without the xsl:
namespace prefix is sent verbatim to the output. The next
command, <xsl:apply-templates/>, spits out the content of
the weather element so that other boxes in the room can
process it.
3 The first item contained inside weather is <temp>76</temp>.
This is an element, and a box is waiting to process it—the
one with a diamond-shaped hole. This box maps to the template
rule on lines 12-14.
4 The HTML list item (<LI>) element start tag and the literal
text "Temperature" do not have the xsl: namespace prefix,
so they are sent directly to the output.
5 Now the box notices the <xsl:apply-templates/> element and,
as with the weather element, spits out the content of the
temp element, "76". This content can now be processed by
another box.
6 The box with a donut hole is waiting. This box processes
all text exposed by the other boxes. It is the template
rule on lines 20-22. The single <xsl:value-of/> element
will send the textual content of the thing that was
captured (which can only be a textual element) directly
to the output, which is indicated by the round hole in the
front of the box.
7 After the text is processed, control returns to the "temp"
rule, which outputs the end tag for the list item element.
Once the temperature element has been processed, control
reverts back to the weather template rule, where it waits
to process the next child.
8-12 The wind element is that child, so the weather box spits
out the wind element just like it spat out the temperature
element, exposing the element to the boxes in the room.
13 After all children of the weather element have been
processed, the <xsl:apply-templates/> element is finished
and the rule continues to process the commands following it.
In this case, there is a single HTML horizontal rule tag.
It is sent to the output and processing terminates.
Notice the syntax of the HTML horizontal rule tag in line 9. Remember that an XSLT style sheet is a well-formed XML document. That means that it must adhere to the well-formedness constraints defined by the XML specification Normally, the tag looks like this:
<HR>
. However, the horizontal rule tag is an empty element. Since the horizontal rule tag is included in line with the other XSL elements, it must also be well-formed, so it takes a slightly different form:
<HR/>
.
Next MonthNext month, we will use XSLT to create a program to convert a newsletter article into an HTML document for delivery on the Web. We will be using the Architag product, XRay, which has built-in XML parting, XSLT processing, and HTML viewing. |


