XSLT Templates

Templates are the building blocks of every XSLT stylesheet. Each template defines what to do when the processor encounters a specific part of the XML source — an element, an attribute, or the root of the document. Without templates, an XSLT stylesheet does nothing.

Think of XSLT templates like customer service scripts. When a customer says "I want to return an item," the agent follows the returns script. When the customer says "I want to check my order status," the agent follows the order-status script. Each script applies to a specific situation — just like each XSLT template applies to a specific XML node.

The xsl:template Element

<xsl:template match="XPath-expression">
  <!-- Output goes here -->
</xsl:template>

The match attribute uses an XPath expression to specify which node or nodes this template applies to.

The Root Template

The most common template matches the root of the XML document using a forward slash. This template fires first and controls the overall output structure.

<xsl:template match="/">
  <html>
    <body>
      <h1>My Document</h1>
    </body>
  </html>
</xsl:template>

Templates for Specific Elements

You can write separate templates for each element type. Use xsl:apply-templates to tell the processor to find and apply the right template for each child element.

XML Source: library.xml

<library>
  <book>
    <title>XML Made Easy</title>
    <author>Priya Nair</author>
  </book>
  <book>
    <title>XSLT in Practice</title>
    <author>Rajan Verma</author>
  </book>
</library>

XSLT with Multiple Templates

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

  <!-- Root template -->
  <xsl:template match="/">
    <html>
      <body>
        <h2>Library Catalog</h2>
        <xsl:apply-templates select="library/book"/>
      </body>
    </html>
  </xsl:template>

  <!-- Template for each book element -->
  <xsl:template match="book">
    <div>
      <h3><xsl:value-of select="title"/></h3>
      <p>by <xsl:value-of select="author"/></p>
    </div>
  </xsl:template>

</xsl:stylesheet>

Output

Library Catalog

XML Made Easy
by Priya Nair

XSLT in Practice
by Rajan Verma

How Template Matching Works — Step by Step

XSLT Processor starts at document root ( / )
              |
              v
Finds template match="/" → applies it
              |
              v
Inside that template: finds xsl:apply-templates select="library/book"
              |
              v
Processor selects all <book> elements
              |
              v
For each <book>: finds template match="book" → applies it
              |
              v
Output is built from all applied templates

Named Templates

A template can have a name instead of (or in addition to) a match pattern. Named templates are called explicitly using xsl:call-template — like calling a function.

<xsl:template name="showHeader">
  <h1>Company Report</h1>
  <p>Generated automatically from XML data.</p>
</xsl:template>

<xsl:template match="/">
  <html>
    <body>
      <xsl:call-template name="showHeader"/>
      <xsl:apply-templates select="report/section"/>
    </body>
  </html>
</xsl:template>

Template Priority

When two templates can match the same node, XSLT uses priority rules to decide which one fires. The more specific match wins. You can also set priority explicitly.

Match PatternDefault Priority
*-0.5 (lowest — matches any element)
elementName0
parent/child0.5
id("value")0.5

Example with Explicit Priority

<xsl:template match="title" priority="1">
  <h2><xsl:value-of select="."/></h2>
</xsl:template>

<xsl:template match="book/title" priority="2">
  <h3>Book: <xsl:value-of select="."/></h3>
</xsl:template>

A title inside a book uses the second template (priority 2). A title anywhere else uses the first template (priority 1).

Template Parameters

Named templates accept parameters, making them reusable with different values — like function parameters.

<xsl:template name="formatPrice">
  <xsl:param name="amount"/>
  <xsl:param name="symbol" select="'INR'"/>
  <span><xsl:value-of select="$symbol"/> <xsl:value-of select="$amount"/></span>
</xsl:template>

<xsl:template match="product">
  <p>
    <xsl:call-template name="formatPrice">
      <xsl:with-param name="amount" select="price"/>
    </xsl:call-template>
  </p>
</xsl:template>

Built-In Default Templates

XSLT defines default templates that fire when no matching template exists. The default template for elements applies templates to all children. The default template for text nodes outputs the text.

<!-- Default template for elements (built-in) -->
<xsl:template match="* | /">
  <xsl:apply-templates/>
</xsl:template>

<!-- Default template for text nodes (built-in) -->
<xsl:template match="text()">
  <xsl:value-of select="."/>
</xsl:template>

This is why an XSLT stylesheet with only a root template still outputs text from all nested elements — the built-in default templates handle everything you didn't write a rule for.

Key Points to Remember

  • xsl:template defines a transformation rule for a specific XML node or pattern.
  • The match attribute uses XPath to identify which nodes activate the template.
  • match="/" targets the document root — the starting point of every XSLT stylesheet.
  • xsl:apply-templates tells the processor to find and apply templates for child nodes.
  • Named templates are called with xsl:call-template and accept parameters.
  • More specific match patterns win over general ones when two templates could match.
  • Built-in default templates handle unmatched nodes automatically.