XSLT - Calling Named Templates with <xsl:call-template>?

In XSLT, the <xsl:call-template> instruction is used to explicitly call a named template that has been defined using <xsl:template name="...">. Named templates are useful when you want to create a reusable block of XSLT instructions and execute that block from different places in your stylesheet.

Unlike templates selected by matching XML nodes, a named template is called directly by its name. This makes it useful for reusable operations such as generating headings, formatting values, creating common sections, or performing repeated transformation tasks.

1. What Is a Named Template?

A named template is an XSLT template that has a name attribute.

Basic syntax:

<xsl:template name="templateName">
    <!-- XSLT instructions -->
</xsl:template>

For example:

<xsl:template name="welcomeMessage">
    <p>Welcome to our website.</p>
</xsl:template>

Here, welcomeMessage is the name of the template.

The template does not automatically execute simply because it has been defined. It needs to be explicitly called using <xsl:call-template>.

2. Using <xsl:call-template>

The basic syntax is:

<xsl:call-template name="templateName"/>

For example:

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

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

                <xsl:call-template name="welcomeMessage"/>
            </body>
        </html>
    </xsl:template>

    <xsl:template name="welcomeMessage">
        <p>Welcome to our website.</p>
    </xsl:template>

</xsl:stylesheet>

When the transformation is performed, the main template calls welcomeMessage, and the instructions inside that named template are executed.

The output will be approximately:

<html>
    <body>
        <h1>My Website</h1>
        <p>Welcome to our website.</p>
    </body>
</html>

3. Why Use Named Templates?

Named templates are mainly used for reusability.

Suppose the same piece of output needs to be generated several times. Instead of writing the same XSLT instructions repeatedly, you can put them inside a named template and call that template whenever required.

For example:

<xsl:template name="companyName">
    <strong>ABC Technologies</strong>
</xsl:template>

You can call it multiple times:

<xsl:call-template name="companyName"/>

and later:

<xsl:call-template name="companyName"/>

This avoids duplication and makes the stylesheet easier to maintain.

4. Named Template with Parameters

A named template can also receive values from the calling location.

For example:

<xsl:template name="displayName">
    <xsl:param name="name"/>

    <p>
        Name:
        <xsl:value-of select="$name"/>
    </p>
</xsl:template>

The template defines a parameter called name.

The template can then be called with a value:

<xsl:call-template name="displayName">
    <xsl:with-param name="name" select="'John'"/>
</xsl:call-template>

The output will be:

<p>Name: John</p>

Here, <xsl:call-template> calls the named template, while <xsl:with-param> supplies a value to its parameter.

5. Calling the Same Template with Different Values

One important advantage is that the same named template can be reused with different parameters.

For example:

<xsl:template name="displayName">
    <xsl:param name="name"/>

    <p>
        <xsl:value-of select="$name"/>
    </p>
</xsl:template>

It can be called several times:

<xsl:call-template name="displayName">
    <xsl:with-param name="name" select="'John'"/>
</xsl:call-template>

<xsl:call-template name="displayName">
    <xsl:with-param name="name" select="'Mary'"/>
</xsl:call-template>

<xsl:call-template name="displayName">
    <xsl:with-param name="name" select="'David'"/>
</xsl:call-template>

The result is:

<p>John</p>
<p>Mary</p>
<p>David</p>

The template itself is written only once.

6. Difference Between xsl:call-template and xsl:apply-templates

These two instructions are important but work differently.

<xsl:apply-templates> generally tells the XSLT processor to process selected XML nodes according to matching templates.

For example:

<xsl:apply-templates select="book"/>

The processor looks for templates whose match patterns correspond to the selected nodes.

In contrast:

<xsl:call-template name="displayBook"/>

directly calls the template named displayBook.

For example:

<xsl:template match="book">
    ...
</xsl:template>

is normally used with:

<xsl:apply-templates select="book"/>

Whereas:

<xsl:template name="displayBook">
    ...
</xsl:template>

is called with:

<xsl:call-template name="displayBook"/>

The key difference is that apply-templates uses template matching, while call-template uses an explicit template name.

7. Named Templates Do Not Automatically Change the Current Node

This is an important concept.

Consider:

<xsl:template match="book">
    <xsl:call-template name="showTitle"/>
</xsl:template>

When showTitle is called, it does not automatically select a different XML node. The current context remains available to the called template.

For example:

<xsl:template match="book">
    <xsl:call-template name="showTitle"/>
</xsl:template>

<xsl:template name="showTitle">
    <xsl:value-of select="title"/>
</xsl:template>

If the current node is:

<book>
    <title>XML Fundamentals</title>
</book>

then:

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

can access the title element of the current book.

8. Reusing a Common Header

Named templates are particularly useful for common page components.

For example:

<xsl:template name="header">
    <header>
        <h1>Student Portal</h1>
        <p>Learning Management System</p>
    </header>
</xsl:template>

It can be called from the main template:

<xsl:template match="/">
    <html>
        <body>

            <xsl:call-template name="header"/>

            <main>
                <p>Welcome to the student portal.</p>
            </main>

        </body>
    </html>
</xsl:template>

This approach is useful when several parts of a stylesheet need to produce the same header.

9. Using Named Templates for Formatting

Named templates can also centralize formatting logic.

For example:

<xsl:template name="formatPrice">
    <xsl:param name="price"/>

    <span>
        $
        <xsl:value-of select="$price"/>
    </span>
</xsl:template>

The template can then be called wherever a price needs to be displayed:

<xsl:call-template name="formatPrice">
    <xsl:with-param name="price" select="29.99"/>
</xsl:call-template>

Output:

<span>$29.99</span>

This is useful because the formatting rule exists in one location.

10. Named Templates and Reusable Logic

Named templates can contain more than simple output.

They can contain instructions such as:

<xsl:if>
<xsl:choose>
<xsl:for-each>
<xsl:value-of>

and other XSLT instructions.

For example:

<xsl:template name="checkStatus">
    <xsl:param name="status"/>

    <xsl:choose>
        <xsl:when test="$status = 'active'">
            <p>Account is active.</p>
        </xsl:when>

        <xsl:otherwise>
            <p>Account is inactive.</p>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

It can be called as:

<xsl:call-template name="checkStatus">
    <xsl:with-param name="status" select="'active'"/>
</xsl:call-template>

The named template therefore acts as a reusable transformation component.

11. Complete Example

Consider the following XML:

<students>
    <student>
        <name>Rahul</name>
        <course>Java</course>
    </student>

    <student>
        <name>Anita</name>
        <course>Python</course>
    </student>
</students>

An XSLT stylesheet can define a named template:

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

    <xsl:template match="/">
        <html>
            <body>

                <h1>Student Information</h1>

                <xsl:for-each select="students/student">

                    <xsl:call-template name="studentDetails"/>

                </xsl:for-each>

            </body>
        </html>
    </xsl:template>

    <xsl:template name="studentDetails">

        <div>
            <p>
                Name:
                <xsl:value-of select="name"/>
            </p>

            <p>
                Course:
                <xsl:value-of select="course"/>
            </p>
        </div>

    </xsl:template>

</xsl:stylesheet>

The studentDetails template is called for every student.

The output will contain:

<h1>Student Information</h1>

<div>
    <p>Name: Rahul</p>
    <p>Course: Java</p>
</div>

<div>
    <p>Name: Anita</p>
    <p>Course: Python</p>
</div>

12. Advantages of <xsl:call-template>

Using named templates provides several advantages:

Reusability: The same transformation logic can be used multiple times.

Reduced duplication: Repeated XSLT instructions do not have to be copied throughout the stylesheet.

Maintainability: If a common operation needs to change, you can update the named template in one place.

Clear organization: Complex stylesheets can be divided into logical reusable templates.

Parameter support: Named templates can receive different values using <xsl:with-param>.

Explicit control: The developer decides exactly when a named template should execute.

13. Important Syntax to Remember

Defining a named template:

<xsl:template name="myTemplate">
    ...
</xsl:template>

Calling the template:

<xsl:call-template name="myTemplate"/>

Defining a parameter:

<xsl:param name="value"/>

Passing a parameter:

<xsl:call-template name="myTemplate">
    <xsl:with-param name="value" select="'Example'"/>
</xsl:call-template>

14. Summary

<xsl:call-template> provides a direct way to execute a named XSLT template. The named template is created with <xsl:template name="..."> and invoked using <xsl:call-template name="..."/>.

Its main purpose is to create reusable transformation logic. Named templates are especially useful for common page sections, formatting operations, repeated output structures, and reusable processing logic. They can also accept parameters, allowing the same template to produce different results based on the values supplied by the caller.

The most important distinction to remember is:

<xsl:apply-templates>
        |
        |-- selects nodes and finds matching templates

<xsl:call-template>
        |
        |-- directly calls a named template

Thus, <xsl:call-template> is an important XSLT mechanism for organizing reusable and modular transformation logic.