XSLT - XSLT Modes (mode attribute) – Detailed Explanation
In XSLT, the mode attribute allows you to apply different processing rules to the same XML nodes depending on the context. It is a powerful feature that helps you reuse templates while controlling how and when they are applied.
1. Why Modes Are Needed
Normally, when you use <xsl:apply-templates>, XSLT selects the best matching <xsl:template> for a node based on its match pattern.
However, sometimes you want:
-
One way to process a node in one context
-
A completely different way to process the same node in another context
Without modes, this becomes difficult because only one template can match a node effectively at a time.
Modes solve this by allowing multiple templates for the same node, each distinguished by a mode.
2. Basic Concept
A mode is simply a label attached to:
-
<xsl:template> -
<xsl:apply-templates>
Only templates with the matching mode will be applied.
3. Syntax
Defining a template with a mode:
<xsl:template match="elementName" mode="modeName">
<!-- processing -->
</xsl:template>
Applying templates with a mode:
<xsl:apply-templates select="elementName" mode="modeName"/>
4. How It Works
-
When
apply-templatesis called with a mode, XSLT looks only for templates with that same mode. -
Templates without a mode are ignored in that case.
-
If no matching template is found in that mode, default processing may occur.
5. Example Without Mode (Problem)
XML:
<book>
<title>Learn XSLT</title>
</book>
XSLT:
<xsl:template match="title">
<h1><xsl:value-of select="."/></h1>
</xsl:template>
This always produces:
<h1>Learn XSLT</h1>
But what if you want:
-
One output as
<h1> -
Another as
<p>
You cannot easily do this without modes.
6. Example With Modes
XSLT:
<xsl:template match="title" mode="header">
<h1><xsl:value-of select="."/></h1>
</xsl:template>
<xsl:template match="title" mode="paragraph">
<p><xsl:value-of select="."/></p>
</xsl:template>
Applying templates:
<xsl:apply-templates select="book/title" mode="header"/>
<xsl:apply-templates select="book/title" mode="paragraph"/>
Output:
<h1>Learn XSLT</h1>
<p>Learn XSLT</p>
Same XML node, two different outputs.
7. Real-World Use Cases
1. Multiple Views of Same Data
-
Summary view vs detailed view
-
Table format vs list format
2. Reusing Templates Cleanly
Instead of duplicating logic, you separate behavior using modes.
3. Layered Processing
You can process the same XML in multiple passes:
-
First pass: extract data
-
Second pass: format output
8. Default Mode vs Named Modes
-
If no
modeis specified, the template belongs to the default mode -
When you specify a mode, it becomes a named mode
Example:
<xsl:template match="title">
This is default mode.
<xsl:template match="title" mode="custom">
This is named mode.
9. Important Rules
-
Mode names must match exactly between template and apply-templates
-
Templates in different modes do not interfere with each other
-
You can have multiple templates for the same match pattern if modes differ
-
If you forget to specify mode in
apply-templates, the default mode is used
10. Advanced Concept (XSLT 2.0 and Above)
You can define modes globally:
<xsl:mode name="myMode"/>
And even control behavior like:
-
default processing
-
streamability
-
on-no-match behavior
11. Common Mistakes
1. Mode mismatch
mode="A"` vs `mode="a"
Modes are case-sensitive.
2. Forgetting mode in apply-templates
This causes the wrong template (or default template) to run.
3. Assuming default templates apply in modes
Default templates only apply in the default mode unless specified.
12. Summary
Modes in XSLT allow you to:
-
Apply different templates to the same XML node
-
Organize transformations more cleanly
-
Reuse logic without conflicts
-
Build multi-stage transformations
They are especially useful in complex XML transformations where the same data needs to be processed in different ways.