Introduction to XSLT

eXtensible Stylesheet Language Transformations (XSLT) is a powerful language for transforming XML documents into other formats, such as HTML, plain text, or other XML documents. It's a W3C Recommendation, designed to work with XML documents, enabling you to define how XML data should be presented or restructured.

What is XSLT?

XSLT is part of the XSL family, which also includes XSL-FO (Formatting Objects) for document presentation and XPath (XML Path Language) for navigating XML structures. XSLT uses XPath expressions to select parts of an XML document and then applies templates to transform the selected nodes.

Key Concept: XSLT is a declarative language. You describe what the output should look like, rather than the step-by-step procedure to create it.

Core Components of XSLT

An XSLT stylesheet is itself an XML document, typically with the .xsl or .xslt extension. It contains instructions on how to process an input XML document. The main components include:

<xsl:stylesheet> or <xsl:transform>

The root element of an XSLT stylesheet. It declares the XSLT namespace and specifies the version of XSLT being used.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- Stylesheet content goes here -->
</xsl:stylesheet>

<xsl:template>

Templates are the core of an XSLT stylesheet. Each template matches specific nodes in the input XML document and defines how those nodes should be transformed.

<xsl:template match="book">
    <!-- Transformation for a 'book' element -->
</xsl:template>

<xsl:output>

Specifies the format of the output document (e.g., html, text, xml). You can also specify the method, encoding, and indentation.

<xsl:output method="html" indent="yes"/>

<xsl:value-of>

Used to output the value of a selected node or attribute. It typically uses an XPath expression.

<p>Title: <xsl:value-of select="title"/></p>

<xsl:for-each>

Used to iterate over a set of nodes. It's similar to a loop in programming languages.

<ul>
    <xsl:for-each select="book">
        <li><xsl:value-of select="title"/></li>
    </xsl:for-each>
</ul>

<xsl:apply-templates>

Recursively processes child nodes or other nodes selected by an XPath expression. This is a fundamental instruction for navigating and transforming the XML tree.

<xsl:template match="catalog">
    <h1>Book Catalog</h1>
    <xsl:apply-templates select="book"/>
</xsl:template>

A Simple Example

Let's consider an input XML document:

<catalog>
    <book id="bk101">
        <title>XML Developer's Guide</title>
        <author>Gambardella, Matthew</author>
        <price>44.95</price>
    </book>
    <book id="bk102">
        <title>Midnight Rain</title>
        <author>Ralls, Kim</author>
        <price>5.95</price>
    </book>
</catalog>

And a simple XSLT stylesheet to transform it into an HTML list:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>

    <xsl:template match="/">
        <html>
            <head>
                <title>Book List</title>
            </head>
            <body>
                <h1>My Book Collection</h1>
                <ul>
                    <xsl:apply-templates select="catalog/book"/>
                </ul>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="book">
        <li>
            <strong><xsl:value-of select="title"/></strong> by <xsl:value-of select="author"/> (<xsl:value-of select="@id"/>) - $<xsl:value-of select="price"/>
        </li>
    </xsl:template>
</xsl:stylesheet>

The output of this transformation would be:

<html>
    <head>
        <title>Book List</title>
    </head>
    <body>
        <h1>My Book Collection</h1>
        <ul>
            <li><strong>XML Developer's Guide</strong> by Gambardella, Matthew (bk101) - $44.95</li>
            <li><strong>Midnight Rain</strong> by Ralls, Kim (bk102) - $5.95</li>
        </ul>
    </body>
</html>

Benefits of XSLT

Note: XSLT is a powerful tool for handling XML transformations. Understanding XPath is crucial, as it's used extensively within XSLT stylesheets to select nodes.

Further Reading