Extensible Markup Language (XML) Tutorial

XSL Stylesheets

As content is separated from style, you may specify multiple views of an XML document. Stylesheets determine how information is to be presented in the document. The Extensible Stylesheet Language (XSL) achieves this by transforming an XML source document into an XML result document that is suitable for display. XSL consists of three component languages.

  • XML Path Language (XPath)
  • XSL Transformations (XSLT)
  • XSL Formatting Objects (XSLFO)

XSLT describes how to transform an XML document to another XML document. As XML data is hierarchical, the two documents are referred to as trees - a source tree and result tree. XMLPath is used to reference specific parts of an XML document based on predefined templates. The matching part of the XML source document is transferred to the result document. XSLT can be used to add to the content, restrict content or modify the content. For all this to work you need an XML parser with an XSL engine, such as Internet Explorer 5 or higher. If you're using server side scripting, such as ASP, you can format the content on the server and send it back as plain html. See the ASP XSL tutorial for more information on server side XSL when you've finished this tutorial.

To illustrate how all this works, we shall put together a CD collection. There are three files that make up the solution, cd.dtd, cd.xml, and cd.xsl.

The Document Type Definition (DTD)

The following is the DTD that is used to describe the data for cd.xml.

CD.DTD

< !ELEMENT collection (cd+)>
<!ELEMENT cd (title, band, tracks)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT band (#PCDATA)>
<!ELEMENT tracks (track+)>
<!ELEMENT track (#PCDATA)>

The XML Document

The style sheet is specified with a href attribute in the xml-stylesheet element of the XML document.

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

The following is the XML document containing the information for a CD collection.

CD.XML

< ?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="cd.xsl"?>
<!DOCTYPE collection SYSTEM "cd.dtd">
<collection>
    <cd>
        <title>The Hour of Bewilderbeast</title>
        <band>Badly Drawn Boy</band>
        <tracks>
            <track>The Shining</track>
            <track>Everybodys Talking</track>
            <track>Bewilder</track>
            <track>Fall in a River</track>
            <track>Camping Next to Water</track>
            <track>Stone on the Water</track>
            <track>Another Pearl</track>
            <track>Body Rap</track>
            <track>Once Around The Block</track>
            <track>This Song</track>
            <track>Bewilderbeast</track>
            <track>Magic in the Air</track>
            <track>Cause a Rockslide</track>
            <track>Pissing in the Wind</track>
            <track>Blistered Heart</track>
            <track>Disillusion</track>
            <track>Say it Again</track>
            <track>Epitaph</track>
        </tracks>
    </cd>
    <cd>
        <title>Gorillaz</title>
        <band>Gorillaz</band>
        <tracks>
            <track>Re-Hash</track>
            <track>Five Four</track>
            <track>Tomorrow Comes Today</track>
            <track>New Genious</track>
            <track>Clint Eastwood</track>
            <track>Man Research</track>
            <track>Punk</track>
            <track>Sound Check</track>
            <track>Double Bass</track>
            <track>Rock the House</track>
            <track>19-2000</track>
            <track>Latin Simone</track>
            <track>Starshine</track>
            <track>Slow Country</track>
            <track>M1 A1</track>
        </tracks>
    </cd>
</collection>

The Stylesheet

The stylesheet contains the instructions on how to display the page. The namespace is provided by the following declaration.

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

Internet Explorer 5 was released before the XSL standard was finalised, so uses the following non-standard declaration.

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

The following is the XSL document that describes how to display the XML document, cd.xml. The pattern matching and transformations are explained in the next pages in this tutorial.

CD.xsl

<?xml version="1.0" ?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<head>
    <title>CD Collection</title>
</head>
<body>
<xsl:for-each select="/collection/cd">
<p>
    <b>Band:</b> <xsl:value-of select="band" />
    <b>Title:</b> <xsl:value-of select="title" />
</p>
<ul>
<xsl:for-each select="tracks/track">
<li>
    <xsl:value-of select="." />
</li>
</xsl:for-each>
</ul>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

If your browser has an XML Parser, you can View the Rendered XML Document here.

XSLT Pattern Matching

XSL uses pattern matching to match elements in the tree. The syntax is similar to a directory tree.

Pattern Matching
Pattern Example Description
/ /collection/cd Matches child nodes. This example matches the childnode, cd in collection.
// /collection//cd Matches any of the descendant nodes, not just the child nodes. This example matches any cd, under the collection node.
| band | title Matches all of the nodes under the current node that match the words separated by the '|'. This example matches band and title beneath the current node.
. . (A single dot) Matches the current node.
.. .. (Two dots) Matches the parent node.
* /collection/cd/* The asterisk is used as a wildcard. This example matches all nodes under /collection/cd.
id(n) id("64") Matches elements with a unique id. This example matches the element with a unique id of 64.

You might also like...

Comments

About the author

Gez Lemon United Kingdom

I'm available for contract work. Please visit Juicify for details.

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“In order to understand recursion, one must first understand recursion.”