XSL and XSLT

XSL an acronym for the eXstensible Stylesheet Language. The XSL Transformation Language makes it possible to convert XML documents into other formats such as HTML, regular text or any other document type that is described by an XML vocabulary. XSLT is part of XSL and is meant to transform documents and because of that reason it only deals with how the information in an XML document is displayed. XSL and XSLT will handle the presentation while the information is within the XML document. In this way it becomes possible to separate the data (usually stored within an XML document) from the presenation (stored within the XSL document).

Actually an XSL document is an XML document, but with a set of rules of how another XML document should be treated during conversion. An XML/XSL parser can be used to transform XML document A using XSL document B into another document C. The result document, document C, can be a text document, an HTML or XHTML document, another XML document or a document of any format that is defined by an XML vocabulary. Since an XSL document is an XML document, the same rules of an XML document apply to an XSL document. An XSL document has to be well-structured just as an XML document. All elements have a beginning and a closing tag. The values of all attributes have to placed between quotes, etc. And ofcourse:

Each XSL document starts with the XML declaration which is similar to this:

<?xml version="1.0" standalone="yes"?>

XSL is a language that is used to express stylesheets. XSL consists of three parts:

  • XSLT Transformations (XSLT), a language used to transform XML documents
  • XML Path Language also known as XPath, an expression language used by XSLT to access or refer to parts of an XML document. XPath is also used by the XML Linking specification.
  • XSL Formatting Objects: an XML vocabulary for specifying formatting semantics.

An XSL stylesheet specifies the presentation of a class of XML documents by describing how an instance of the class is transformed into an XML document that uses the formatting vocabulary. Below is shown how an example XML document and a corresponding XSL document are used to create a resulting XHTML document.

An example XML document:
<?xml version="1.0" encoding="iso-8859-1" standalone="yes" ?>

<booklist>
    <book title="About the island Java" price="12.95"  />
    <book title="The history of Suriname" price="34.99" />
    <book title="Carnaval in Rio de Janeiro" price="6.75" />
</booklist>
An XSLT document used to transform the XML document above:
<?xml version="1.0" encoding="iso-8859-1" standalone="yes" ?>

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

    <xsl:output
        method="html"
        indent="yes"
        encoding="iso-8859-1"
        media-type="text/xml"
        doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
        doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" />

    <xsl:template match="booklist">
        <html>
            <head>
                <title>Our Booklist</title>
            </head>
            <body>
                Below is a list of books currently available:
                <table border="0">
                    <tr>
                        <td>Title</td>
                        <td>Price</td>
                    </tr>
                    <xsl:for-each select="book">
                        <tr>
                            <td>
                                <xsl:value-of select="@title"/>
                            </td>
                            <td>
                                <xsl:value-of select="@price"/>
                            </td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
         </html>
    </xsl:template>

</xsl:stylesheet>
Transforming the XML document using the XSLT document resuls in this HTML document:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <title>Our Booklist</title>
    </head>
    <body>
        Below is a list of books currently available:
        <table border="0">
            <tr>
                <td><em>Title</em></td>
                <td><em>Price</em></td>
            </tr>
            <tr>
                <td>About the island Java</td>
                <td>12.95</td>
            </tr>
            <tr>
                <td>The history of Suriname</td>
                <td>34.99</td>
            </tr>
            <tr>
                <td>Carnaval in Rio de Janeiro</td>
                <td>6.75</td>
            </tr>
       </table>
   </body>
</html>
The generated XHTML above can be rendered by a browser and will look like this:
Our Booklist
Below is a list of books currently available:
Title Price
About the island Java 12.95
The history of Suriname 34.99
Carnaval in Rio de Janeiro 6.75

If you have an XSLT compliant browser it can be used to transform XML into XHTML. In that case clicking on this link will show you the result of the transformation of the XML in the example above using the XSLT as displayed above.

The example above is just meant to give an idea of what XSLT does and how it's used. To fully understand what happens here, you should think of the structure of the XML document as a tree. The <booklist> element is the root element and also the root of the corresponding tree. Each <book> element is a branch of the tree (and can have one or more leaves). To make it more clear: <booklist> element is a parent element and the <book> elements are children of the <booklist> element.

The XSLT document has a rule matching for each <booklist> element in the XML document. Everything within that <xsl:template match="booklist"> tag will be processed. So all the HTML tags are displayed in the result. But within that element is another XSL element, the <xsl:for-each select="book"> element. This rule says that for each <book> element in the XML document the contents of that tag should be parsed.

The <xsl:for-each select="book"> element in the XSL document has two <xsl:value-of> children. An <xsl:value-of> element is to display the value of a specified element or attribute within the XML document. The <xsl:value-of select="@title"> element tells that the value of the title attribute of the current <book> tag within the XML document should be displayed and the <xsl:value-of select="@price"> tag is to display the value of the price attribute. Since the <xsl:value-of> elements are children of a <xsl:for-each select="book"> all of the <book> elements will be parsed.

Relevant links

See Also

Contact the author: Send an electronic mail to: pajtroon@dds.nl.
Peter's ICQ Number is: #3900785.

This page: Copyright © 2002 - 2005 Peter A. J. Troon

Note: This page is part of the Peter Troon Site.