Proceed to WirelessDevNet Home Page
Publications, e-books, and more! Community Tutorials Store Downloads, tools, & Freebies! IT Career Center News Home
newnav.gif

Newsletters
EMail Address:



   Content
  - Articles
  - Columns
  - Training
  - Library
  - Glossary
 
   Career Center
  - Career Center Home
  - View Jobs
  - Post A Job
  - Resumes/CVs
  - Resource Center
 
   Marketplace
  - Marketplace Home
  - Software Products
  - Wireless Market Data
  - Technical Books
 
   News
  - Daily News
  - Submit News
  - Events Calendar
  - Unsubscribe
  - Delivery Options
 
   Community
  - Discussion Boards
  - Mailing List
  - Mailing List Archives
 
   About Us
  - About WirelessDevNet
  - Wireless Source Disks
  - Partners
  - About MindSites Group
  - Advertising Information
 
INDEX
>Introduction
>Microsoft XML Parser Technology
>A Real-World Example
>Creating The Stylesheet
>Dissecting The Stylesheet

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:

<?xml version="1.0"?>

Because an XSLT stylesheet is an XML document by itself, we have the above processing instruction.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

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:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">

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:

<xsl:template match="/">

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:

<HTML> <BODY bgcolor="wheat">

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.

<B>Course Title: </B><xsl:value-of select="Course/Title" /><BR/>

The next line prints the text contained within the <Synopsis> element.

<B>Synopsis: </B><CENTER><I><xsl:value-of select="Course/Synopsis" /></I></CENTER><BR/>

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.

<B>Duration: </B><xsl:value-of select="(Course/Duration) div 8"/> days <BR/> <B>Fees: </B>S$<xsl:value-of select="Course/Fees" /><BR/> <B>Dates: </B><BR/>

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.

<xsl:for-each select="Course/CourseDates/Date[Day!='']" >

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.

<xsl:value-of select="Day"/>/<xsl:value-of select="Month"/>/<xsl:value-of select="Year"/> From <xsl:value-of select="Time/From"/>hrs to <xsl:value-of select="Time/To" />hrs @ <xsl:value-of select="Venue"/> -

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.

<xsl:value-of select="(/Course/Duration) div (((Time/To) - (Time/From)) div 100)"/> <xsl:value-of select="Mode" /> <BR/> </xsl:for-each>

We then close off our HTML document:

</BODY> </HTML>

And finally we close off the <xsl:template> and <xsl:styleheet> elements.

</xsl:template> </xsl:stylesheet>

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 lwm@np.edu.sg.

Sponsors

Search

Eliminate irrelevant hits with our industry-specific search engine!









Wireless Developer Network - A MindSites Group Trade Community
Copyright© 2000-2010 MindSites Group / Privacy Policy
Send Comments to:
feedback@wirelessdevnet.com