Introduction To XSL
XSL is a powerful way to transform XML documents into other formats.
This article explains what XSL is and how to use it for common tasks. More emphasis is on
transforming XML documents into XHTML documents
What is XSL?
- XSL stands for eXtensible Style sheet Language
- XSL is used to transform XML documents into another formats.
- For example, XSL can be used to convert one XML document
into another XHTML document or even to another XML document
- XSL style sheets are also XML documents and are stored
generally with extension .xsl
- The general principle behind XSL transformation is - search
for a pattern in XML document and apply specified template to it
- Many times when we say - XSL we are actually referring one
of the following things :
- XSLT which is nothing but eXtensible
Style sheet Language Transformations. These specifications deal with transforming XML to
other formats
- XPath which is a standard for describing
path or address of various XML elements like elements, attributes
How XSL works?
- The work of transforming XML to other format is done by a piece of software called XSL
processor
- Note that XSL processor is not the same as XML parser.
- IE 5+ comes with in-built XSL processor. So, you can test your work with IE 5+
- XSL parser generates one tree from the XML document under consideration. This tree is
called as Source Tree
- It also generates second tree from the XSL style sheet file which you will write.
- It then applies the rules from the XSL style sheet to the source tree and generates
third tree called result tree
What are Patterns?
- Patterns are nothing but the way various elements from the XML document are arranged.
- For example pattern "books/book" means a book element who is child of books
element
- Your pattern can contain elements, attributes and some filtering criteria as well like
book elements with subject as programming
- Patterns are expressed using a specification called XPath
What are templates?
- Templates are like a mould which gets applied to XML elements matching certain criteria.
- Thus you can create a template which will be applied to book elements and display them
in a HTML table
Let's start !
Now that we know the general idea behind XSL, let us try to implement our knowledge.
For working with the examples you will need a XML document called catalog.xml. The
document is given below :
<?xml version="1.0"?>
<?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
<catalog>
<book isbn="100">
<author>Author 1</author>
<subject>Title 1</subject>
</book>
<book isbn="200">
<author>Author 2</author>
<subject>Title 2</subject>
</book>
<book isbn="300">
<author>Author 3</author>
<subject>Title 3</subject>
</book>
</catalog>
We will use this as Source document and see how it can be transformed into XHTML
document.
Your First XSL style sheet
Our first example shows details of books from catalog.xml as plain HTML table
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<HTML>
<BODY>
<table border="1">
<xsl:for-each select="catalog">
<xsl:apply-templates select="book" />
</xsl:for-each>
</table>
</BODY>
</HTML>
</xsl:template>
<xsl:template match="book">
<tr>
<td><xsl:value-of select="author"/></td>
<td><xsl:value-of select="subject"/></td>
</tr>
</xsl:template>
Enter above text into your text editor and save the file as catalog.xsl. Before going
further just look at the top of our XML document (catalog.xml) and note how our stylesheet
( catalog.xsl) is linked using processing instruction - <?xml-stylesheet
href="catalog.xsl" type="text/xsl"?>
Dissecting Our XSL style sheet
- <?xml version='1.0'?>
Our XSL file starts with already familiar processing instruction. This
indicates that XSL style sheet in also XML document
- <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
This line starts the root element of the stylesheet. The namespace used is
http://www.w3.org/TR/WD-xsl. Note that in earlier versions of IE this namespace was
http://www.w3.org/1999/XSL/Transform . So, check with your browser which one works !
- <xsl:template match="/">
This line shows the usage of template element of XSL namespace. In plain english
this line means - "apply following instructions to element matching given
criteria". In our case the criteria is "/" meaning the template will be
applied to root element of the XML document. Note that it is not same as
"catalog" element
- Next few lines are HTML opening tags for creating table
- <xsl:for-each select="catalog">
Our aim is to display details of all books available in the catalog.xml
file. In plain English this line means - "select all the catalog nodes which are
under the root element and perform following operations on them"
- <xsl:apply-templates select="book" />
In side the "for-each" we are applying a template which actually
displays the book details. This line would mean - "apply a template designed for the
book node"
- <xsl:template match="book">
Finally comes the book template. All the displaying of book details happen inside
this template. This template is same as one we saw previously but the only difference is
instead of root node we are matching it against book node. This template will be applied
to all the book nodes from the XML document.
- <xsl:value-of select="author"/>
Inside the book template we output the values of the author and subject elements
using "value-of" XSL element. This simply means - "output the value of
author node". What if my node has child nodes? In such case values of those will also
be displayed. Similarly we output the value of subject node.
Accessing attributes
Our previous example is rather incomplete. We have not displayed the ISBN of each book.
For doing that we need to access the isbn attribute of book element. Following style sheet
adds this functionality:
........please refer previous example...........
<xsl:template match="book">
<tr>
<td><xsl:value-of select="@isbn"/></td>
<td><xsl:value-of select="author"/></td>
<td><xsl:value-of select="subject"/></td>
</tr>
</xsl:template>
Note the line marked with bold. We access isbn attribute by specifying @isbn as the
select criteria.