Intranet Journal   Earthweb  
Events Jobs Premium Services Media Kit Network Map E-mail Offers Vendor Solutions Webcasts

   Intranet Journal Subjects
Search Earthweb

Privacy Policy



internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

internet commerce
Be a Commerce Partner
















 

[ Home | Discussion Forum | How Do I... | Lotus Notes Intranets | Microsoft SharePoint | Products | Shopping  ]

free news!


XML Basics, Part IV: Formatting Output


P.G. Daly
3/15/2004

Go to page: 1 2 

Printer Friendly Version

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.

Go to page: 1 2

Printer Friendly Version

Of Interest
Intranet eXchange Discussion Board
XML Basics and Benefits
XML Basics, Part II: The Key Concepts
XML Basics Part III: An Example of Well-Formed and Valid XML
XML Basics, Part IV: Formatting Output

email this page

Tutorials
and more at:
Intranet Journal's Tutorials
Intranet Journal Favorites

Creating a PHP-Based Content Management System

The Spyware Guide

Introduction to Microsoft SharePoint Portal

Intranet Journal
Part of the EarthWeb Network

Managing Editor
Intranet Journal

Tom Dunlap

EarthWeb Home Page
Jupitermedia Home Page

Media Kit




The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers