Intranet Journal
The online resource for intranet professionals

Back to Article | Home | Discussion Board | Tutorials | Special Reports/Series ]

XML Basics, Part IV: Formatting Output


P.G. Daly

3/15/2004

Welcome to the fourth and final part in this series of XML Basics. We will complete our exploration by using XSLT to format the output of your XML files.

To recap our journey thus far, Part I explored the definition and benefits of XML, Part II examined the detailed concepts of writing and working with XML, and Part III tied it all together with an easy to follow example.

What is XSL?

XSL is an XML-based language used for stylesheets that can be used to transform XML documents into other document types and formats. It consists of the transformation language, abbreviated XSLT, and the formatting language called XSL Formatting Objects. This combination enables two of the key benefits of XML: transforming one document into multiple channels of output and system interoperability.

In order for XSLT to do its magic, you provide it with an input document (source tree) so it can do its thing (transformation) and make it look a particular way in a specified output document (result tree).

XSLT is built on a structure known as an XSL template. This structure is represented by the <xsl:template> element. There are two important pieces to every template element: the match attribute and the actual contents of the template.

The match attribute specifies the pattern in the source tree that will be transformed by a particular template element. In the contents of the template element, you specify what should go in the results tree for each occurrence of the specified pattern.

There are a number of XSLT elements that you could insert into a template to perform a variety of actions. One that is most common is the <xsl:value-of> element. With this element, you can take information from the source tree and add it to the result tree.

For example, let's use an abbreviated piece of our example from Part III:

<movie>
   <title>The Matrix</title>
</movie>

If we wanted to use XSLT to select each occurrence of the <title> element and output its content in the results tree, the XSLT would look as follows:

<xsl:template match="/movie/title"><xsl:value-of select="."/></xsl:template>

The output in this example would be: "The Matrix".

Common XSLT Elements

Templates are the heart and soul of XSLT. A stylesheet is really just a collection of one or more templates that get applied to the input document (the source tree XML document) to create the output document (the result tree that can be a variety of document types).

If an XSL document has more than one template, the order of operations is such that the processor starts by matching the document root to the stylesheet template that best matches it. If there is more than one match, the following order applies: If matching the root element, the first match applies; otherwise, the second (last) match is used.

An important point to remember about the match attribute is that the item matched becomes the context node for that template. What that means is that the matched element becomes the root for that particular template section. Essentially, the match simply creates a subsection of the overall document hierarchy and works from that perspective for the specific template section.

As with many things in the XML world, there are quite a number of XSLT elements that can be used within a template. I will describe the most common ones here and use some of them in the example to follow.

<xsl:stylesheet>
This element is used as the root element of nearly all XSLT stylesheets. Its format is <xsl:stylesheet version="version number" xmlns="path to W3c namespace">. Version number is the current number of the XSLT specification from the W3C. The xmlns is the path to the XML Namespace defined by the W3C for the XSLT Transformation language. Currently, that path is http://www.w3.org/1999/XSL/Transform.

<xsl:template>
As we have already discussed, this element is used to define the templates which make up our XSLT stylesheet. The syntax is <xsl:template match="pattern expression to match" name="template name" priority="number" mode="mode name">

The match attribute is used to indicate the pattern from the source tree in the input document that we wish to select. The name, priority, and mode attributes are optional. The name attribute is used to explicitly name the template, the priority attribute is used to force calling one template over another when more than one can satisfy the match, and mode is used when more than one template satisfies the match but performs different transformations on the content.

<xsl:apply-templates>
This element is used within an XSL template to call other templates.

<xsl:value-of>
As mentioned earlier, this element is used to insert the content specified within the matched element from the source tree into the result tree.

<xsl:output>
This element allows us to specify the output method we will be using, for example, xml, text, or html. It affords us greater control over the actual output.

<xsl:element>
If we don't know ahead of time what elements we need to create in the result tree, <xsl:element> allows us to dynamically create elements during the transformation process.

<xsl:text>
This element simply allows us to insert some specified text (PCDATA) into the output.

Two elements are available in XSLT for conditional processing. These elements allow you to make choices in your stylesheets: <xsl:if> and <xsl:choose>.

<xsl:if>
This is the simpler of the two elements and allows you to evaluate an expression. If it is true, the contents of the <xsl:if> element are evaluated. The syntax looks as follows: <xsl:if test="boolean expression">Some content or XSL elements here</xsl:if>.

<xsl:choose>
This element provides more flexibility than the <xsl:if>. With it, we can make one of any number of choices and even have a default choice. As soon as one of the test expressions evaluate to true, control leaves the <xsl:choose> block. An example looks as follows:

<xsl:choose>
<xsl:when test="element name[test criteria 1]"> Result number one.</xsl:when>
<xsl:when test=="element name[test criteria 2]"> Result number two.</xsl:when>
<xsl:otherwise>The default result.</xsl:otherwise>
</xsl:choose>

<xsl:for-each>
When we need to do particular processing for a number of nodes within the source tree, we need to loop. This element allows us to form a template within a template. Syntax looks as follows: <xsl:for-each select="expression">. Everytime the expression is satisfied, the template associated with the "for-each" is instantiated. See the MovieCatalog example below for this element in action.

<xsl:copy-of>
If you wish to take sections of the source tree and copy them directly to the result tree, you can use this element to do so easily. Simply specify an expression to point to the node(s) required and it will insert the node(s) directly into the result tree along with any attributes and child elements.

XSLT Applied to the Movie Catalog Example

Let's wrap up our movie catalog example by creating a basic XSLT stylesheet to output the results in HTML. The source tree for our example is the MovieCatalog.xml from Part III of this series. The original XML code is:

<?xml version="1.0" ?>
<!DOCTYPE MovieCatalog SYSTEM "file:///h:/Paula/MovieCatalog.dtd">
<?xml-stylesheet type="text/xsl" href="MovieCatalog.xsl"?>

<MovieCatalog>

  <movie>
   <title>The Matrix</title>
   <length>136</length>
   <genre>Sci-Fi and Fantasy</genre>
    <actors>
     <actor>Keanu Reeves</actor>
     <actor>Laurence Fishburne</actor>
     <actor>Carrie Ann Moss</actor>
    </actors>
   <datereleased>1999</datereleased>
   <director>Wachowski Brothers</director>
   <format>DVD</format>

  </movie>

  <movie>
   <title>Titanic</title>
   <length>194</length>
   <genre>Drama</genre>
    <actors>
    <actor>Leonardo DiCaprio</actor>
    <actor>Kate Winslet</actor>
    </actors>
   <datereleased>1999</datereleased>
   <director>James Cameron</director>
   <format>DVD</format>

  </movie>

  <movie>
   <title>The Sixth Sense</title>
   <length>106</length>
   <genre>Thriller</genre>
    <actors>
     <actor>Bruce Willis</actor>
     <actor>Haley Joel Osment</actor>
    </actors>
   <datereleased>1999</datereleased>
   <director>M. Night Shyamalan</director>
   <format>VHS</format>

  </movie>

</MovieCatalog>

Before I discuss the actual stylesheet code, let me show you a snapshot of page one of the resulting output:

The following XSLT stylesheet, when applied to our XML document and opened in Internet Explorer, will produce the result we see above:

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

<xsl:template match="/">
<html>
<head>
<title>Movie Catalog Transformed</title>
</head>
<body>

<h1>My Movie Catalog</h1>
<hr/>

<xsl:for-each select="/MovieCatalog/movie">

<h2><xsl:value-of select="title" /></h2>

<table border="1">
<tr>
<th>Length</th>
<td><xsl:value-of select="length" /> minutes</td>
</tr>

<tr>
<th>Genre</th>
<td><xsl:value-of select="genre" /></td>
</tr>

<xsl:for-each select="actors/actor">
<tr>
<th>Actor</th>
<td><xsl:value-of select="." /></td>
</tr>
</xsl:for-each>

<tr>
<th>Date Released</th>
<td><xsl:value-of select="datereleased" /></td>
</tr>
<tr>
<th>Director</th>
<td><xsl:value-of select="director" /></td>
</tr>
<tr>
<th>Format</th>
<td><xsl:value-of select="format" /></td>
</tr>
</table>

</xsl:for-each>

</body>
</html>

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

The only change I made to our original XML file in order for it to reference the stylesheet was the following line (inserted at line 3 of the original XML):

<?xml-stylesheet type="text/xsl" href="MovieCatalog.xsl"?>

This line of code simply tells the XML document to apply a stylesheet and where to find it.

As you can see from the output and the XSLT code, the stylesheet is a combination of straight HTML with XSLT elements strategically placed where we need to retrieve content from the XML document. While this stylesheet is fairly straightforward, it does illustrate the power of using the <xsl:for-each> element to loop through multiple instances of a particular node in the XML document. As a result, this stylesheet reacts dynamically to whatever the XML content is at a given moment in time (i.e., whether you have three or three thousand movies in your catalog).

Why is This So Powerful?

One of the reasons this functionality is so powerful is that it allows different organizations (internal or external) to easily communicate using the same format. For example, Division A of your company can use its own source information in whatever format it chooses. Division B can use a different format for its information. When they need to interact and communicate with each other, they can easily use XSLT to transform their data (XML) to a common format. As a result, you maintain the most flexibility between the two divisions while allowing for seamless cross-division communication and interoperability.

Wrap-Up

As you can see, XML and its related technologies are extremely powerful. While these past four articles only discuss the basics, it should give you enough knowledge to get started and move forward with confidence.



Back to Article | Home | Discussion Board | Tutorials ]

Copyright 2002 Jupitermedia Corporation, All Rights Reserved.
Legal Notices | Licensing, Reprints, & Permissions | Privacy Policy | Advertising on Intranet Journal
Home | eXchange | F A Q | Find | Register |