XSLT <template>

Define transformation rules with templates

📋 What is xsl:template?

The xsl:template element defines reusable transformation rules in XSLT. Templates match specific XML patterns and specify how to transform matched nodes into the desired output format using the match attribute.


<!-- Basic Template -->
<xsl:template match="book">
    <div>
        <h2><xsl:value-of select="title"/></h2>
    </div>
</xsl:template>
                                    

Template Features

🎯

Match Pattern

Select nodes to transform

<xsl:template match="/">
🏷️

Named Templates

Reusable template functions

<xsl:template name="header">
📊

Priority

Control template precedence

<xsl:template priority="2">
🔄

Mode

Apply templates conditionally

<xsl:template mode="summary">

🔹 Basic Template Structure

Templates use the match attribute to specify which XML nodes they transform. The template body contains the output structure and XSLT instructions for processing the matched nodes.

<!-- Input XML -->
<library>
    <book>
        <title>XML Guide</title>
        <author>John Doe</author>
    </book>
</library>

<!-- XSLT with Templates -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <!-- Root template -->
    <xsl:template match="/">
        <html>
            <body>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>
    
    <!-- Book template -->
    <xsl:template match="book">
        <div class="book">
            <h2><xsl:value-of select="title"/></h2>
            <p>By: <xsl:value-of select="author"/></p>
        </div>
    </xsl:template>
    
</xsl:stylesheet>

Output:

XML Guide

By: John Doe

🔹 Named Templates

Named templates work like functions that you can call from anywhere in your stylesheet. Use xsl:call-template to invoke them, making your code more modular and reusable.

<!-- Define named template -->
<xsl:template name="formatPrice">
    <xsl:param name="amount"/>
    <span class="price">$<xsl:value-of select="$amount"/></span>
</xsl:template>

<!-- Use named template -->
<xsl:template match="product">
    <div>
        <h3><xsl:value-of select="name"/></h3>
        <xsl:call-template name="formatPrice">
            <xsl:with-param name="amount" select="price"/>
        </xsl:call-template>
    </div>
</xsl:template>

<!-- Input XML -->
<product>
    <name>Laptop</name>
    <price>999.99</price>
</product>

Output:

Laptop

$999.99

🔹 Template Matching Patterns

Templates can match different node types and patterns using XPath expressions. This allows precise control over which elements get transformed and how.

<!-- Match root -->
<xsl:template match="/">
    <html><body><xsl:apply-templates/></body></html>
</xsl:template>

<!-- Match specific element -->
<xsl:template match="title">
    <h1><xsl:value-of select="."/></h1>
</xsl:template>

<!-- Match with attribute -->
<xsl:template match="book[@category='fiction']">
    <div class="fiction-book">
        <xsl:apply-templates/>
    </div>
</xsl:template>

<!-- Match any element -->
<xsl:template match="*">
    <p><xsl:value-of select="."/></p>
</xsl:template>

<!-- Match text nodes -->
<xsl:template match="text()">
    <xsl:value-of select="."/>
</xsl:template>

Pattern Types:

/ - Document root

element - Specific element

element[@attr] - With attribute

* - Any element

🔹 Template Priority

When multiple templates match the same node, XSLT uses priority to determine which template to apply. You can set explicit priority values or rely on default precedence rules.

<!-- Low priority (default = 0) -->
<xsl:template match="item">
    <p>Generic item</p>
</xsl:template>

<!-- High priority -->
<xsl:template match="item[@special='true']" priority="2">
    <p class="special">Special item!</p>
</xsl:template>

<!-- Input XML -->
<items>
    <item>Regular</item>
    <item special="true">Premium</item>
</items>

Output:

Generic item

Special item!

🔹 Template Modes

Modes allow you to process the same nodes differently in different contexts. This is useful when you need multiple representations of the same data in your output.

<!-- Normal mode -->
<xsl:template match="book">
    <div class="book-full">
        <h2><xsl:value-of select="title"/></h2>
        <p><xsl:value-of select="description"/></p>
        <p>Price: $<xsl:value-of select="price"/></p>
    </div>
</xsl:template>

<!-- Summary mode -->
<xsl:template match="book" mode="summary">
    <li><xsl:value-of select="title"/> - $<xsl:value-of select="price"/></li>
</xsl:template>

<!-- Usage -->
<xsl:template match="/">
    <h1>Quick List</h1>
    <ul>
        <xsl:apply-templates select="//book" mode="summary"/>
    </ul>
    
    <h1>Full Details</h1>
    <xsl:apply-templates select="//book"/>
</xsl:template>

Output:

Quick List

  • XML Guide - $29.99

Full Details

XML Guide

Complete guide to XML

Price: $29.99

🔹 Complete Template Example

This comprehensive example demonstrates multiple templates working together to transform a complex XML structure into a formatted HTML page with different sections.

<!-- Input XML -->
<store>
    <category name="Electronics">
        <product featured="true">
            <name>Laptop</name>
            <price>999</price>
        </product>
        <product>
            <name>Mouse</name>
            <price>25</price>
        </product>
    </category>
</store>

<!-- XSLT -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="/">
        <html>
            <body>
                <h1>Product Catalog</h1>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>
    
    <xsl:template match="category">
        <div class="category">
            <h2><xsl:value-of select="@name"/></h2>
            <xsl:apply-templates select="product"/>
        </div>
    </xsl:template>
    
    <xsl:template match="product">
        <div class="product">
            <h3><xsl:value-of select="name"/></h3>
            <p>$<xsl:value-of select="price"/></p>
        </div>
    </xsl:template>
    
    <xsl:template match="product[@featured='true']" priority="1">
        <div class="product featured">
            <h3>⭐ <xsl:value-of select="name"/></h3>
            <p>$<xsl:value-of select="price"/></p>
        </div>
    </xsl:template>
    
</xsl:stylesheet>

HTML Output:

Product Catalog

Electronics

⭐ Laptop

$999

Mouse

$25

🧠 Test Your Knowledge

Which attribute specifies what nodes a template matches?