You are on page 1of 16

Introduction to XSL

XSL, the style sheet language of XML is far more sophisticated than CSS.
CSS - The Style Sheet of HTML
Because HTML uses predefined tags, the meanings of these tags are well understood: The <p> element defines a
paragraph and the <h1> element defines a heading, and the browser knows how to display these elements.
Adding display style characteristics to HTML elements with CSS is a simple process. Telling the browser to display each
element using a special font or color, is easy to do and easy for a browser to understand.
XSL - The Style Sheet of XML
Because XML does not use predefined tags (we can use any tags we want), the meanings of these tags are not understood:
<table> could mean an HTML table or maybe a piece of furniture. Because of the nature of XML, the browser does not
know how to display an XML document.
In order to display XML documents, it is necessary to have a mechanism to describe how the document should be
displayed. One of these mechanisms is CSS, but XSL (the eXtensible Stylesheet Language) is the preferred style sheet
language of XML, and XSL is far more sophisticated than the CSS used by HTML.
XSL - More than a Style Sheet
XSL consists of three parts:
a method for transforming XML documents
a method for defining XML parts and patterns
a method for formatting XML documents
If you don't understand the meaning of this, think of XSL as a language that can transform XML into HTML, a language
that can filter and sort XML data, a language that can address parts of an XML document, a language that can format
XML data based on the data value, like displaying negative numbers in red, and a language that can output XML data to
different devices, like screen, paper or voice.
XSL is a World Wide Web Standard
XSL is a standard recommended by the World Wide Web Consortium.
The first two parts of the language became a W3C Recommendation in November 1999. The full XSL Recommendation
including XSL formatting became a W3C Candidate Recommendation in November 2000.
You can read more about the W3C activities in our W3C School.
XSL Languages
XSL actually consists of three languages. The most important is XSLT.
XSL is Three Languages
XSL actually consists of three languages:
XSLT is a language to transform XML
XPath is a language to define XML parts or patterns
XSL Formatting Objects is a language to define XML display
XSLT is a language for transforming XML documents into other types of documents, or into other XML documents.
XPath is a language for addressing parts of an XML document. XPath was designed to be used by XSLT.
Formatting is the process of turning the result of an XSL transformation into a suitable output form for a reader or listener.
XSLT and XPath were released as two separate W3C Recommendations 16. November 1999. No separate W3C document
exists for XSL Formatting Objects, but a description can be found inside the XSL 1.0 Recommendation.
You can read more about the W3C XSL activities in our W3C XSL section.
XSLT - XSL Transformations
XSLT is the most important part of the XSL Standard. It is the part of XSL that is used to transform an XML document
into another XML document, or another type of document.

XSLT can be used to transform an XML document into a format that is recognizable to a browser. One such format is
HTML. Normally XSLT does this by transforming each XML element into an HTML element.
XSLT can also add completely new elements into the output file, or remove elements. It can rearrange and sort the
elements, and test and make decisions about which elements to display, and a lot more.
A common way to describe the transformation process is to say that XSL uses XSLT to transform an XML source tree into
an XML result tree (or an XML source document into an XML result document)
How does it work?
In the transformation process, XSLT uses XPath to define parts of the source document that match one or more predefined
templates. When a match is found, XSLT will transform the matching part of the source document into the result
document. The parts of the source document that do not match a template will (as a general rule) end up unmodified in the
result.
This Tutorial will focus on XSLT and XPath
Most of the chapters in this tutorial will focus on XSLT and XPath. We will use XSLT to define XML transformations and
XPath to define the matching patterns for the transformations.
Even though XSL consists of three different parts with three different names, we will use the general term XSL in this
tutorial.
XSL Browsers
Very few browsers support XSL at the moment.
In this tutorial we will use Internet Explorer 5.0 to demonstrate XSL.
The Internet Explorer XML Parser
In order to process an XML document using XSL, you need an XML parser with an XSL Engine. Internet Explorer 5.0 is
currently the only widely available browser that contains an XML parser with an XSL engine.
At the moment, the example code in this tutorial will only work in Internet Explorer 5.0 or later.
The Internet Explorer XSL Engine
XSL in Internet Explorer 5.0 is not 100% compatible with the latest released W3C XSL standard.
Internet Explorer 5 was released before the XSL standard was completely settled (when the XSL standard was still a W3C
Working Draft), but Microsoft has promised to solve this problem in the next release.
The examples in this tutorial are only slightly different from the official W3C XSL Recommendation, and the examples are
still very well suited to learn XSL.
The only visible difference you will find in our tutorial examples is the XSL stylesheet declaration:
This is the standard way from the W3C XSL Recommendation:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
This is the (incorrect) Internet Explorer way (from the XSL Working Draft):
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
Internet Explorer MSXML
MSXML 2.0 is the name of the XML parser that shipped with IE 5.0.
MSXML 2.5 is the name of the parser that shipped with Windows 2000.
MSXML 3.0 is the latest release of the XML parser. It can be downloaded from Microsoft, and will ship with future
versions of Internet Explorer and Windows.
According to Microsoft, MSXML 3.0 is 100% compatible with the official W3C XSL Recommendation:
"MSXML 3.0 offers a significant advancement over MSXML 2.5: server-safe HTTP access, complete implementation of
XSLT and XPath, changes to SAX (Simple API for XML), higher conformance with W3C standards, and a number of bug
fixes"
For more info:
http://msdn.microsoft.com/xml/general/xmlparser.asp
You can read more about the latest releases of IE in our Browser Section.

XSL - Transformation
Example study: How to transform XML into HTML using XSL.
The details of this example will be explained in the next chapter.
Start with your XML Document
First you start with the XML document that you want to transform into HTML:
<?xml version="1.0"?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
...
If you use Internet Explorer 5.0 or later, you can view the XML file.
Create an XSL Style Sheet Document
Then you create an XSL Style Sheet with a transformation template:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
<body>
<table border="2" bgcolor="yellow">
<tr>
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="CATALOG/CD">
<tr>
<td><xsl:value-of select="TITLE"/></td>
<td><xsl:value-of select="ARTIST"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
<xsl:template>
</xsl:stylesheet>
If you use Internet Explorer 5.0 or later, you can view the XSL file
Link the Style Sheet to the XML document
Then you add an XSL Style Sheet reference to your XML document:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="cd_catalog.xsl"?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>

<YEAR>1985</YEAR>
</CD>
...
If you have an XSL compliant browser, like Internet Explorer 5.0 or later, your browser will nicely transform your XML
into HTML.
XSL - Templates
XSL uses TEMPLATES to describe how to output XML.
CSS uses Rules
If you have studied our CSS School, you will know that CSS uses one or more rules to define the output of HTML
elements. A selector is used to associate the rule with an HTML
element.
The p selector in this CSS rule tells that a <p> element should be displayed using a font named arial:
p { font-family: arial }
XSL uses Templates
XSL uses one or more templates to define how to output XML elements. A match attribute is used to associate the template
with an XML element. (The match attribute can also be used to define a template for a whole branch of the XML
document).
Look at the following XSL Style Sheet that contains a template to output the XML CD Catalog from the previous chapter:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr>
<th>Title</th>
<th>Artist</th>
</tr>
<tr>
<td>.</td>
<td>.</td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Since the style sheet is an XML document itself, the document begins with an xml declaration:
<?xml version='1.0'?>
The xsl:stylesheet tag in the second line defines the start of the stylesheet.
The xsl:template tag in the third line defines the start of a template. The template attribute match="/" associates (matches)
the template to the root (/) of the XML source document.
The rest of the document contains the template itself, except for the last two lines that defines the end of the template and
the end of the style sheet.
The result of the transformation will look (a little disappointing) like this:
Title
.

Artist
.

With Internet Explorer 5, you can view the XML file, the XSL file, and the Result
The <xsl:value-of> Element
The result from the previous sample was a little disappointing, because no data was copied from the XML document to the
output.
The XSL <xsl:value-of> element can be used to select XML elements into the output stream of the XSL transformation:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr>
<th>Title</th>
<th>Artist</th>
</tr>
<tr>
<td><xsl:value-of select="CATALOG/CD/TITLE"/></td>
<td><xsl:value-of select="CATALOG/CD/ARTIST"/></td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Note: the syntax for the select attribute value is called an XSL Pattern. It works like navigating a file system where a
forward slash (/) selects subdirectories.
The result of the transformation will look like this:
Title
Empire Burlesque

Artist
Bob Dylan

With Internet Explorer 5, you can view: the XML file, the XSL file, and the Result
The <xsl:for-each> Element
The result from the previous sample was a also a little disappointing, because only one line of data was copied from the
XML document to the output.
The XSL <xsl:for-each> element can be used to select every XML element into the output stream of the XSL
transformation:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr>
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="CATALOG/CD">
<tr>
<td><xsl:value-of select="TITLE"/></td>
<td><xsl:value-of select="ARTIST"/></td>
</tr>
</xsl:for-each>
</table>
</body>

</html>
</xsl:template>
</xsl:stylesheet>
The xsl:for-each element locates elements in the XML document and repeats a part of the template for each one.
The result of the transformation will look like this:
Title
Artist
Empire Burlesque
Bob Dylan
Hide your heart Bonnie
Tyler
Greatest Hits
Dolly Parton
Still got the blues
Gary More
Eros
Eros Ramazzotti
One night only
Bee Gees
Sylvias Mother
Dr.Hook
Maggie May
Rod Stewart
Romanza
Andrea Bocelli
When a man loves a woman
Percy Sledge
Black angel
Savage Rose
1999 Grammy Nominees Many
For the good times
Kenny Rogers
Big Willie style
Will Smith
Tupelo Honey
Van Morrison
Soulsville
Jorn Hoel
The very best of
Cat Stevens
Stop
Sam Brown
Bridge of Spies
T`Pau
Private Dancer
Tina Turner
Midt om natten
Kim Larsen
Pavarotti Gala Concert
Luciano Pavarotti
The dock of the bay
Otis Redding
Picture book
Simply Red
Red
The Communards
Unchain my heart
Joe Cocker
With Internet Explorer 5, you can view: the XML file, the XSL file, and the Result
XSL - On the Client
If your browser supports XML, XSL can be used to transform the document to HTML in your browser.
A JavaScript Solution
In the previous chapter we explained how XSL can be used to transform a document from XML to HTML. The trick was
to add an XSL stylesheet to the XML file, and to let the browser do the transformation.
Even if this works fine, it is not always desirable to include a stylesheet reference in the XML file, and the solution will not
work in a non XSL aware browser.
A more versatile solution would be to use a JavaScript to do the XML to HTML transformation.
By using a JavaScript we are more open for these possibilities:
Allowing the JavaScript to do browser specific testing
Using different style sheets according to browser and/or user needs
That's the beauty of XSL. One of the design goals for XSL was to make it possible to transform data from one format to
another, supporting different browsers and different user needs.
XSL transformation on the client side is bound to be a major part of the browsers work tasks in the future, as we will see a
growth in the specialized browser market (think: Braille, Speaking Web, Web Printers, Handheld PCs, Mobile Phones .....).

The XML file and the XSL file


Take a new look at the XML document that you saw in the previous chapter:
<?xml version="1.0"?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD> . . .
If you use Internet Explorer 5.0 or later, you can click here to view the XML file.
And the accompanying XSL stylesheet:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
<body>
<table border="2" bgcolor="yellow">
<tr>
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="CATALOG/CD">
<tr>
<td><xsl:value-of select="TITLE"/></td>
<td><xsl:value-of select="ARTIST"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
If you use Internet Explorer 5.0 or later, you can click here to view the XSL file.
The syntax of the above XSL document was explained in the previous chapter, so it will not be explained here. But be sure
to notice that the XML file does not have a reference to the XSL file, and the XSL file does not have a reference to the
XML file.
IMPORTANT: The above sentence indicates that an XML file could be transformed using many different XSL files.
Transforming XML to HTML in your Browser
Here is the simple source code needed to transform the XML file to HTML on the client:
<html>
<body>
<script language="javascript">
// Load XML
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.load("cd_catalog.xml")
// Load the XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("cd_catalog.xsl")
// Transform

document.write(xml.transformNode(xsl))
</script>
</body>
</html>
If you use Internet Explorer 5.0 or later, you can try it yourself.
(The example above uses JavaScript. If you don't know how to write JavaScript, you should visit our JavaScript School.)
The first block of code creates an instance of the Microsoft XML parser (XMLDOM), and loads the XML document into
memory. The second block of code creates another instance of the parser and loads the XSL document into memory. The
last line of code transforms the XML document using the XSL document, and writes the result to the HTML document.
XSL - On the Server
Since not all browsers support XML and XSL, a solution is to transform the XML to HTML on the server.
A Cross Browser Solution
In the previous chapter we explained how XSL can be used to transform a document from XML to HTML in the browser.
The trick was to let the JavaScript use an XML parser to do the transformation.
This solution will not work with a browser that doesn't support an XML parser.
To make XML data available to all kinds of browsers, we have to transform the XML document on the SERVER and send
it as pure HTML to the BROWSER.
That's another beauty of XSL. One of the design goals for XSL was to make it possible to transform data from one format
to another on a server, returning readable data to all kinds of future browsers.
XSL transformation on the server is bound to be a major part of the Internet Information Server work tasks in the future,
as we will see a growth in the specialized browser market (think: Braille, Speaking Web, Web Printers, Handheld PCs,
Mobile Phones .....).
The XML file and the XSL file
Take a new look at the XML document that you saw in the previous chapter:
<?xml version="1.0"?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
...
If you use Internet Explorer 5.0 or later, you can click here to view the XML file.
And the accompanying XSL stylesheet:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
<body>
<table border="2" bgcolor="yellow">
<tr>
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="CATALOG/CD">
<tr>

<td><xsl:value-of select="TITLE"/></td>
<td><xsl:value-of select="ARTIST"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
If you use Internet Explorer 5.0 or later, you can click here to view the XSL file.
The syntax of the above XSL document was explained in the previous chapter, so it will not be explained here. But be sure
to notice that the XML file does not have a reference to the XSL file, and the XSL file does not have a reference to the
XML file.
IMPORTANT: The above sentence indicates that an XML file on the server could be transformed using many different
XSL files.
Transforming XML to HTML on the Server
Here is the simple source code needed to transform the XML file to HTML on the server:
<% 'Load the XML
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load(Server.MapPath("cd_catalog.xml"))
'Load the XSL
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load(Server.MapPath("cd_catalog.xsl"))
'Transform the file
Response.Write(xml.transformNode(xsl)) %>
(The example is an ASP file, and the code is a VBScript. If you don't know ASP or VBScript you should take a trip to ASP
School.)
The first block of code creates an instance of the Microsoft XML parser (XMLDOM), and loads the XML file into
memory. The second block of code creates another instance of the parser and loads the XSL document into memory. The
last line of code transforms the XML document using the XSL document, and returns the result to the browser.
XSL Sort
XSL can be used to sort an XML document.
Where to put the Sort Information
Take a new look at the XML document that you have seen in many chapters:
<?xml version="1.0"?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD> . . .
If you use Internet Explorer 5.0 or later, you can click here to view the XML file
To output this XML file as an ordinary HTML file, and sort it at the same time, simply add an order-by attribute to your
for-each element in your XSL file like this:
<xsl:for-each select="CATALOG/CD" order-by="+ ARTIST">

The order-by attributes takes a plus (+) or minus (-) sign, to define an ascending or descending sort order, and an element
name to define the sort element.
Now take a look at your slightly adjusted XSL stylesheet:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
<body>
<table border="2" bgcolor="yellow">
<tr>
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="CATALOG/CD" order-by="+ ARTIST">
<tr>
<td><xsl:value-of select="TITLE"/></td>
<td><xsl:value-of select="ARTIST"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
If you use Internet Explorer 5.0 or later, you can click here to view the XSL file.
Transforming it in your Browser
Here is the simple source code needed to transform the XML file to HTML in your browser:
<html>
<body>
<script language="javascript">
// Load XML
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.load("cd_catalog.xml")
// Load the XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("cd_catalog_sort.xsl")
// Transform
document.write(xml.transformNode(xsl))
</script>
</body>
</html>
If you use Internet Explorer 5.0 or later, you can try it yourself.
XSL Filter Query
XSL can be used to filter an XML file.
Where to put the Filter Information
Take a new look at the XML document that you have seen so many times:
<?xml version="1.0"?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>

<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD> . . .
If you use Internet Explorer 5.0 or later, you can click here to view the XML file
To filter the XML file, simply add a filter to the select attribute in your for-each element in your XSL file like this:
<xsl:for-each select="CATALOG/CD[ARTIST='Bob Dylan']">
Legal filter operators are:
= (equal)
!= (not equal)
&lt; less than
&gt; greater than
Now take a look at your slightly adjusted XSL stylesheet
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
<body>
<table border="2" bgcolor="yellow">
<tr>
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="CATALOG/CD[ARTIST='Bob Dylan']">
<tr>
<td><xsl:value-of select="TITLE"/></td>
<td><xsl:value-of select="ARTIST"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
If you use Internet Explorer 5.0 or later, you can click here to view the XSL file.
Transforming it in your Browser
Here is the simple source code needed to transform the XML file to HTML in your browser. Insert the source code below
into an .htm or .html file:
<html>
<body>
<script language="javascript">
// Load XML
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.load("cd_catalog.xml")
// Load the XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("cd_catalog_filter.xsl")
// Transform
document.write(xml.transformNode(xsl))
</script>
</body>
</html>

If you use Internet Explorer 5.0 or later, you can try it yourself.
XSL Conditional IF
XSL can use an IF test to filter information from an XML document.
Where to put the IF condition
Take a new look at the XML document that you have seen so many times:
<?xml version="1.0"?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
...
If you use Internet Explorer 5.0 or later, you can click here to view the XML file
To put a conditional if test against the content of the file, simply add an xsl:if element to your XSL document like this:
<xsl:if match=".[ARTIST='Bob Dylan']">
... some output ...
</xsl:if>
Now take a look at your slightly adjusted XSL stylesheet:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
<body>
<table border="2" bgcolor="yellow">
<tr>
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="CATALOG/CD">
<xsl:if match=".[ARTIST='Bob Dylan']">
<tr>
<td><xsl:value-of select="TITLE"/></td>
<td><xsl:value-of select="ARTIST"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
If you use Internet Explorer 5.0 or later, you can click here to view the XSL file.
Transforming it in your Browser
Here is the simple source code needed transform the XML file to HTML in your browser:
<html>
<body>

<script language="javascript">
// Load XML
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false xml.load("cd_catalog.xml")
// Load the XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("cd_catalog_if.xsl")
// Transform
document.write(xml.transformNode(xsl))
</script>
</body>
</html>
XSL Conditional Choose
XSL can use a conditional choose to filter an XML document.
Where to put the Choose Condition
Take a new look at the XML document that you have seen in almost every chapter:
<?xml version="1.0"?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
...
If you use Internet Explorer 5.0 or later, you can click here to view the XML file
To insert a conditional choose test against the content of the file, simply add the xsl:choose, xsl:when and xsl:otherwise
elements to your XSL document like this:
<xsl:choose>
<xsl:when test=".[ARTIST='Bob Dylan']">
... some code ...
</xsl:when>
<xsl:otherwise>
... some code ....
</xsl:otherwise>
</xsl:choose>
Now take a look at your slightly adjusted XSL stylesheet:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
<body>
<table border="2" bgcolor="yellow">
<tr>
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="CATALOG/CD">
<tr>

<td><xsl:value-of select="TITLE"/></td>
<xsl:choose>
<xsl:when test=".[ARTIST='Bob Dylan']">
<td bgcolor="#ff0000">
<xsl:value-of select="ARTIST"/>
</td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="ARTIST"/></td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
If you use Internet Explorer 5.0 or later, you can click here to view the XSL file.
Transforming it in your Browser
Here is the simple source code needed to transform the XML file to HTML in your browser:
<html>
<body>
<script language="javascript">
// Load XML
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.load("cd_catalog.xml")
// Load the XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("cd_catalog_choose.xsl")
// Transform
document.write(xml.transformNode(xsl))
</script>
</body>
</html>
If you use Internet Explorer 5.0 or later
XSLT Elements Reference (IE5)
Internet Explorer 5 supports 20 XSLT Elements.
The IE5 implementation is not compatible with the W3C standard.
Internet Explorer 5.0 XSLT Elements
Element Name Description
Attributes
xsl:apply-templates
Applies a template to the current element. order-by="+|-pattern"
select="pattern"
xsl:attribute
Adds a new attribute to the current output
element.name
="attribute-name"
xsl:cdata
Adds a new CDATA section to the output.
xsl:choose
Provides a selection mechanism based on conditions.
xsl:comment
Adds a comment node to the output.
xsl:copy
Copies the current node to the output.
xsl:define-template-set
Defines a new set of templates
xsl:element
Adds a new element node to the output.
name="name"

xsl:entity-ref
xsl:eval

Adds a new entity reference node to the output.


name="name"
Provides an evaluation mechanism to evaluate output content.language=
"language"
xsl:for-each
Provides a mechanism to create a loop in the output
stream. select="pattern" order-by="-|+ pattern"
xsl:if
Provides a conditional branch mechanism based on a
condition.match="pattern"
xsl:node-name
Adds the name of the current node to the output.
xsl:otherwise
Part of the choose mechanism (see xsl:choose).
xsl:pi
Adds a processing instruction to the output. name="name"
xsl:script
Defines a script area inside a template.
language="language"
xsl:stylesheet
Defines the root element of the style sheet. xmlns:xml="namespace"
language="language"
indent-result="yes|no"
xsl:template
Defines a template.
match="pattern"
language="language"
xsl:value-of
Defines a node to insert into the output.
select="pattern"
xsl:when
Part of the choose mechanism (see xsl:choose)
test="expression"
The Internet Explorer XSLT Engine
XSLT in Internet Explorer 5.0 is not compatible with the released W3C XSL Recommendation.
Internet Explorer 5 was released before the XSL standard was completely settled (when the XSL standard was still a W3C
Working Draft), but Microsoft has promised to solve this problem in the next release.
XSLT Elements Reference (W3C)
This chapter lists the XSLT elements from the W3C Recommendation.
W3C XSLT Elements
The links in the "Element" columns point to attributes and more useful information about the specific element.
Element
Description
xsl:apply-imports
Applies a template from an imported stylesheet
xsl:apply-templates
Applies a template to the current element
xsl:attribute
Adds an attribute to the nearest containing element
xsl:attribute-set
Defines a named set of attributes
xsl:call-template
Provides a way to call a named template
xsl:choose
Provides a way to choose between a number of alternatives
based on conditions
xsl:comment
Creates an XML comment
xsl:copy
Copies the current node without childnodes and attributes to the
output
xsl:copy-of
Copies the current node with childnodes and attributes to the
output
xsl:decimal-format
Defines the character/string to be used when converting
numbers into strings, with the format-number function
xsl:element
Adds a new element node to the output
xsl:fallback
Provides a way to define an alternative for not implemented
instructions
xsl:for-each
Provides a way to create a loop in the output stream.
xsl:if
Provides a way to write a conditional statement
xsl:import
Imports a stylesheet
xsl:include
Includes a stylesheet
xsl:key
Provides a way to define a key
xsl:message
Writes a message to the output
xsl:namespace-alias
Provides a way to map a namespace to another namespace
xsl:number
Writes a formatted number to the output
xsl:otherwise
Indicates what should happen when none of the <xsl:when>
elements inside an <xsl:choose> element is satisfied
xsl:output
Provides a way to control the transformed output

xsl:param
Provides a way to define parameters
xsl:preserve-space
Provides a way to define the handling of white-space
xsl:processing-instruction Writes a processing instruction to the output
xsl:sort
Provides a way to define sorting
xsl:strip-space
Provides a way to define the handling of white-space
xsl:stylesheet
Defines the root element of the style sheet
xsl:template
Defines a template for output
xsl:text
Writes text to the output
xsl:transform
Defines the root element of the style sheet
xsl:value-of
Creates a text node and inserts a value into the result tree
xsl:variable
Provides a way to declare a variable
xsl:when
Defines a condition to be tested and performs an action if the
condiition is true. This element is always a child element of
<xsl:choose>
xsl:with-param
Provides a way to pass parameters to templates