An Introduction to XSLT and XPath (cont.)
Dissecting The Stylesheet
Let's now take a closer look at the stylesheet. The first line of the XSLT stylesheet is:
Because an XSLT stylesheet is an XML document by itself, we have the above processing instruction.
Next we define the namespace for the XSL stylesheet with the <xsl:stylesheet> element. Note that the newer MSXML3
parser supports both the MS XSL standard as well as the W3C XSLT Recommendation. The difference is in the namespace. To use
the MS XSL standard, change the namespace to:
The older MS XSL specification does not conform to the current XSLT Specification 1.0. As such, it contains
some XSLT elements that do not work with other XSLT processors. Also note that using the W3C XSLT recommendation requires
an addition attribute named version to be included.
Note: The MSXML3 parser was, at the time of this writing, in beta release and as such Microsoft does not recommend that you use it on a production server.
With this namespace declaration, all those elements that begin with the prefix would be treated as
XSL elements. The XSL processor would operate according to the functions of the XSL elements.
Let's take a look at the next XSL element in our stylesheet:
The <xsl:template> element is used as a template to match against the source XML document. What this element
is doing is basically matching against the root of the XML document. The root of the XML document is the start of the
XML document (not to be confused with the root element of the XML document). This match is indicated by the attribute
match in the <xsl:template> element. The value of "/" indicates the "root" of the XML document.
The value of the match attribute contains an XPath expression. XPath is a language for addressing parts of an XML document.
As its name implies, XPath is like your directory path that you used to locate files in your hard disk drive. It specifies
an expression to locate the required elements in your XML document. In addition to the expression, it also supports functions
to perform actions on a group of elements. We will look at XPath functions shortly.
Back to the discussion, when the root of the XML document is located, the current element that is selected is known as the
context node. The context node is analogous to your current directory.
Next we see that we have some familiar tags:
Since these tags do not begin with the "xsl" prefix, they are copied to the output tree. The output tree is the final document
produced by the transformation.
The next line contains another XSL element, <xsl:value-of>. Basically, the <xsl:value-of> element displays the value
of the element that is indicated by the select attribute. In this case, it is "Course/Title", which means look under the
context node (root of the XML document, in this case) for the element <Course> which contains the child element <Title>.
Once the required element is located, the text of the element (<Title>) would be printed in the output tree.
The next line prints the text contained within the <Synopsis> element.
The next line is interesting. It shows that besides locating element within an XML document, XPath can also be used to
perform mathematical operations. In this case, since the duration of a course is represented in hours, we divide (by using the
div operator) it by eight to return the number of days per course.
An XPath expression can also perform decision making. In the next line, we select all the <Course>/<CourseDates>/<Date>
elements whose <Day> does not contain an empty string. We also use the <xsl:for-each> element to perform looping.
This element is very much like the looping construct found in typical programming languages. And so it reads: "Find all <Course>/
<CourseDates>/<Date> elements whose <Day> element is non empty and loop through each element.
Once in the loop, the context node is now at the first <Course>/<CourseDates>/<Date> element.
So the XPath expression in the select attribute of the <xsl:value-of> element can simply be written relative
to the context node. Thus the attribute value select="Month" is functionally equivalent to "/Course/CourseDates/Date/Month",
which specifies the absolute path.
The next line calculates the number of days the course is being run. It calculates the value based on the number
of hours allocated to this course (/Course/Duration) divided by the duration of the course per day.
We then close off our HTML document:
And finally we close off the <xsl:template> and <xsl:styleheet> elements.
Note: One interesting thing to note is that when you do a View Source, you will see XML as the
source, not HTML. In IE5, you won't be able to see the HTML code if you are performing client-side transformation.
Coming Next Week: Transforming XML Into WML
Wei Meng LEE is the co-author of the upcoming book Beginning WAP: WML and WMLScript (ISBN: 1-861004-58-3),
to be published by Wrox Press. This book will be available in December, 2000. He can be reached via email at email@example.com.