Extensible Stylesheet Language (XSL) Basics
By Stephen Bucaro
XSL is an XML language that is used to "transform" an XML document into a different
form. Generally, XSL is used to create a "template" that a Web browser uses to
transform an XML document into an HTML webpage. In this article, you'll learn how
easy it is to do powerful things with XSL.
First of all, you'll need some basic knowledge of XML, nothing advanced, but you
should know how to create a "well-formed" XML document. You can learn basic XML
from articles on this website and other sources. You need a basic knowledge of XML
because XSL works on XML and XSL itself is XML.
An XSL document, referred to as an "XSL style sheet", allows you to select, filter,
and sort the elements of an XML document for display as HTML in a Web browser. You
can even include scripts in an XSL style sheet that let you modify or add information
to the XML document. We'll stick with selecting, filtering, and sorting in this article.
Shown below is a very simple XML document. This document represents a book list that
contains only one "book" element. The book element has child nodes that contain the
"title, publisher, author, isbn, pubdate, and price of the book.
<?xml version="1.0"?>
<book>
<title>XML in a Nutshell</title>
<publisher>O'Reilly</publisher>
<author>
<firstname>Scott</firstname>
<lastname>Means</lastname>
</author>
<isbn>0596007647</isbn>
<pubdate>2004</pubdate>
<price>26.37</price>
</book>
If you would like to follow along, create a new file in a simple ASCII text editor,
like Windows Notepad, paste the above XML into the text file, and save it with the
file name "booklist.xml".
Shown below is a very simple XSL document to display the data in the booklist XML document.
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<p>Book List</p>
<table>
<tr><td>Title: </td><td>
<xsl:value-of select="book/title" /></td></tr>
<tr><td>Publisher: </td><td>
<xsl:value-of select="book/publisher" /></td></tr>
<tr><td>Author: </td><td>
<xsl:value-of select="book/author" /></td></tr>
<tr><td>ISBN: </td><td>
<xsl:value-of select="book/isbn" /></td></tr>
<tr><td>Pub Date: </td><td>
<xsl:value-of select="book/pubdate" /></td></tr>
<tr><td>Price: </td><td>
<xsl:value-of select="book/price" /></td></tr>
</table>
</xsl:template>
</xsl:stylesheet>
If you would like to follow along, paste the above XSL into the text file and save
it with the file name "example1.xsl".
|