SyntaxStudy
Sign Up
XML Introduction to XSLT Templates
XML Beginner 1 min read

Introduction to XSLT Templates

XSLT (Extensible Stylesheet Language Transformations) is a language for transforming XML documents into other XML documents, HTML, plain text, or any other text-based format. An XSLT stylesheet is itself an XML document whose root element is or . The core processing model is template-based: the processor starts at the root node and recursively applies templates to nodes as it traverses the source tree. A template rule is declared with where the match attribute is an XPath pattern. When the XSLT processor encounters a node that matches the pattern, it instantiates that template. The template body is a mix of literal result elements (which are copied to the output as-is), XSLT instructions (prefixed xsl:), and extension instructions. The built-in templates handle nodes with no matching rule by default: elements and the root have a built-in that applies templates to children, and text nodes copy their content to the output. The instruction triggers template matching for the selected node-set, continuing the recursive descent. The instruction computes the string value of an XPath expression and inserts it into the output. Together, these two instructions plus form the minimal toolkit for most XML-to-HTML transformations. XSLT 1.0 is universally supported; XSLT 2.0 and 3.0 (implemented by Saxon) add grouping, sequences, functions, and schema awareness.
Example
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <!-- Output method: produce HTML -->
    <xsl:output method="html" encoding="UTF-8" indent="yes"/>

    <!-- Template matching the root node -->
    <xsl:template match="/">
        <html>
            <head><title>Library Catalogue</title></head>
            <body>
                <h1>Library Catalogue</h1>
                <table border="1">
                    <tr>
                        <th>ID</th>
                        <th>Title</th>
                        <th>Author</th>
                        <th>Year</th>
                        <th>Price</th>
                    </tr>
                    <!-- Apply templates to all book elements -->
                    <xsl:apply-templates select="//book"/>
                </table>
            </body>
        </html>
    </xsl:template>

    <!-- Template matching each book element -->
    <xsl:template match="book">
        <tr>
            <td><xsl:value-of select="@id"/></td>
            <td><xsl:value-of select="title"/></td>
            <td><xsl:value-of select="author"/></td>
            <td><xsl:value-of select="year"/></td>
            <td>
                <xsl:choose>
                    <xsl:when test="price">
                        $<xsl:value-of select="price"/>
                    </xsl:when>
                    <xsl:otherwise>N/A</xsl:otherwise>
                </xsl:choose>
            </td>
        </tr>
    </xsl:template>

</xsl:stylesheet>