XSLT - Using Modes (mode Attribute) in XSLT Templates

In XSLT, templates define how specific XML elements should be processed and transformed into the desired output. Normally, when XSLT encounters an XML node, it searches for the best matching template and applies it. However, there are situations where the same XML element needs to be processed in different ways depending on the context. This is where the mode attribute becomes valuable. Modes allow developers to create multiple templates that match the same XML element but perform different transformations based on the processing mode being used.

The mode attribute provides flexibility by separating different transformation tasks within the same stylesheet. Without modes, developers often need to write complicated conditional statements or duplicate code to handle different output formats. By assigning different modes to templates, XSLT can process the same XML document in multiple ways without conflicts. This makes stylesheets cleaner, easier to maintain, and more organized.

Why Modes Are Needed

Consider an XML document containing information about books in a library. Sometimes you may want to display only the book titles, while other times you may need detailed information including the author, publisher, and price. Without modes, handling these different requirements can become difficult because only one matching template can normally be applied for a particular element.

Modes solve this problem by allowing different templates to match the same XML element while performing separate tasks.

For example:

  • One mode displays a summary.

  • Another mode displays detailed information.

  • A third mode generates data for printing.

  • A fourth mode creates output for a web application.

Each template is activated only when its corresponding mode is specified.

Syntax of the Mode Attribute

The mode attribute is added to both xsl:template and xsl:apply-templates.

General syntax:

<xsl:template match="element" mode="modeName">
    ...
</xsl:template>

Applying templates with a specific mode:

<xsl:apply-templates select="element" mode="modeName"/>

The mode name can be any meaningful identifier chosen by the developer.

Example XML Document

<library>
    <book>
        <title>Learning XML</title>
        <author>John Smith</author>
        <price>650</price>
    </book>
    <book>
        <title>XSLT Essentials</title>
        <author>Mary Brown</author>
        <price>850</price>
    </book>
</library>

Example Without Modes

A normal template may look like this:

<xsl:template match="book">
    <p>
        <xsl:value-of select="title"/>
    </p>
</xsl:template>

This template always displays only the title.

Output:

Learning XML
XSLT Essentials

If later you need another version showing author and price, you cannot simply create another template matching book because both templates would conflict.

Example Using Modes

Summary Mode

<xsl:template match="book" mode="summary">
    <p>
        <xsl:value-of select="title"/>
    </p>
</xsl:template>

Detail Mode

<xsl:template match="book" mode="detail">
    <div>
        <h2><xsl:value-of select="title"/></h2>
        <p>Author:
            <xsl:value-of select="author"/>
        </p>
        <p>Price:
            <xsl:value-of select="price"/>
        </p>
    </div>
</xsl:template>

The same book element now has two separate templates.

Calling the Summary Mode

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

Output:

Learning XML
XSLT Essentials

Calling the Detail Mode

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

Output:

Learning XML
Author: John Smith
Price: 650

XSLT Essentials
Author: Mary Brown
Price: 850

The XML data remains the same, but different templates generate different outputs depending on the selected mode.

Processing the Same XML Multiple Times

One of the biggest advantages of modes is that the same XML document can be processed multiple times during a single transformation.

For example:

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

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

The first processing generates a summary, while the second produces detailed information.

Real-World Applications

Modes are widely used in enterprise XML processing.

Web and Print Output

A company stores product information in XML.

Using one mode:

  • Generate HTML pages.

Using another mode:

  • Generate printable reports.

Using another mode:

  • Produce PDF-ready XML.

The same XML source supports multiple output formats.

Multi-Language Documents

An XML document contains content in several languages.

Different modes can:

  • Display English content.

  • Display French content.

  • Display Spanish content.

Each mode applies templates appropriate to the selected language.

Report Generation

Business reports often require:

  • Executive summaries.

  • Financial details.

  • Technical appendices.

Separate modes generate each report section independently.

Data Validation

One mode validates XML data.

Another mode formats the same data for display.

This separation keeps validation logic independent from presentation logic.

Advantages of Using Modes

Modes offer several important benefits:

  • Allow multiple templates to process the same XML element differently.

  • Improve stylesheet organization by separating transformation logic.

  • Reduce code duplication and unnecessary conditional statements.

  • Simplify maintenance by isolating different processing tasks.

  • Enable multiple output formats from a single XML source.

  • Improve readability in large and complex XSLT projects.

  • Support modular stylesheet design.

Best Practices

When using modes in XSLT, consider the following recommendations:

  • Choose descriptive mode names such as summary, detail, print, or mobile.

  • Keep each mode focused on a single transformation purpose.

  • Avoid mixing unrelated processing logic within the same mode.

  • Use comments to describe the purpose of each mode in large stylesheets.

  • Combine modes with modular stylesheets to improve maintainability.

Common Mistakes

Developers often encounter these issues when working with modes:

  • Forgetting to specify the correct mode in xsl:apply-templates, resulting in the intended template not being used.

  • Using inconsistent mode names, which makes the stylesheet difficult to understand.

  • Creating unnecessary modes for simple transformations, adding needless complexity.

  • Assuming that templates with different modes are applied automatically; a mode must be explicitly invoked.

  • Overusing modes when a single template with straightforward logic would be sufficient.

Conclusion

The mode attribute is a powerful feature in XSLT that allows the same XML elements to be processed in multiple independent ways. It enables developers to organize complex transformations into separate, reusable processing paths, making stylesheets easier to maintain, extend, and understand. Whether generating summaries, detailed reports, print-ready documents, or different output formats from the same XML source, modes provide a structured and efficient solution that is widely used in professional XSLT development.