Welcome to The Coding College! In this tutorial, we’ll explore the <xsl:template>
element in XSLT. This element is the foundation of XSLT transformations, allowing you to define rules for how to process and transform specific parts of an XML document.
What Is the <xsl:template>
Element?
The <xsl:template>
element in XSLT defines a template for transforming an XML node or set of nodes.
- It matches nodes using the
match
attribute. - Templates define how to process the matched nodes and produce output.
When the XSLT processor encounters a node that matches the match
pattern, the corresponding template is applied.
Syntax
<xsl:template match="pattern">
<!-- Transformation logic here -->
</xsl:template>
Attributes:
match
: Specifies the nodes in the XML document to which the template applies.name
(optional): Assigns a unique name to the template, allowing it to be called explicitly.
How It Works
- The XSLT processor begins by looking for a template with
match="/"
(the root node). - It applies the matching templates to process and transform the XML.
- If no template matches a node, the processor uses default behavior (e.g., copying text content).
Example: Basic <xsl:template>
Usage
Input XML:
<bookstore>
<book>
<title>XML Basics</title>
<author>John Doe</author>
</book>
<book>
<title>Advanced XSLT</title>
<author>Jane Smith</author>
</book>
</bookstore>
XSLT Stylesheet:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- Template for the root node -->
<xsl:template match="/">
<html>
<body>
<h1>Bookstore</h1>
<xsl:apply-templates select="bookstore/book" />
</body>
</html>
</xsl:template>
<!-- Template for individual book nodes -->
<xsl:template match="book">
<p>
<strong>Title:</strong> <xsl:value-of select="title" /><br />
<strong>Author:</strong> <xsl:value-of select="author" />
</p>
</xsl:template>
</xsl:stylesheet>
Output HTML:
<html>
<body>
<h1>Bookstore</h1>
<p>
<strong>Title:</strong> XML Basics<br />
<strong>Author:</strong> John Doe
</p>
<p>
<strong>Title:</strong> Advanced XSLT<br />
<strong>Author:</strong> Jane Smith
</p>
</body>
</html>
Using Named Templates
Templates can also be called by name using <xsl:call-template>
.
Syntax for Named Templates:
<xsl:template name="templateName">
<!-- Transformation logic -->
</xsl:template>
Example: Calling a Named Template
Input XML:
<greeting>
<message>Hello, World!</message>
</greeting>
XSLT Stylesheet:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- Template for the root node -->
<xsl:template match="/">
<html>
<body>
<xsl:call-template name="displayMessage" />
</body>
</html>
</xsl:template>
<!-- Named template -->
<xsl:template name="displayMessage">
<h1><xsl:value-of select="greeting/message" /></h1>
</xsl:template>
</xsl:stylesheet>
Output HTML:
<html>
<body>
<h1>Hello, World!</h1>
</body>
</html>
Default Templates
If no <xsl:template>
matches a node, the processor applies default rules:
- Element nodes: Process their children.
- Text nodes: Output their content.
Example of Default Behavior:
Input XML:
<note>
<to>John</to>
<from>Jane</from>
<message>Hello, John!</message>
</note>
XSLT Stylesheet Without Templates:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
</xsl:stylesheet>
Output:
JohnJaneHello, John!
Combining <xsl:template>
with <xsl:apply-templates>
Use <xsl:apply-templates>
to recursively apply templates to child nodes.
Example: Recursive Processing
Input XML:
<company>
<employee>
<name>John</name>
<position>Manager</position>
</employee>
<employee>
<name>Jane</name>
<position>Developer</position>
</employee>
</company>
XSLT Stylesheet:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- Root template -->
<xsl:template match="/">
<html>
<body>
<h1>Company Employees</h1>
<xsl:apply-templates select="company/employee" />
</body>
</html>
</xsl:template>
<!-- Template for employee nodes -->
<xsl:template match="employee">
<p>
<strong>Name:</strong> <xsl:value-of select="name" /><br />
<strong>Position:</strong> <xsl:value-of select="position" />
</p>
</xsl:template>
</xsl:stylesheet>
Output HTML:
<html>
<body>
<h1>Company Employees</h1>
<p>
<strong>Name:</strong> John<br />
<strong>Position:</strong> Manager
</p>
<p>
<strong>Name:</strong> Jane<br />
<strong>Position:</strong> Developer
</p>
</body>
</html>
Best Practices for <xsl:template>
- Use Specific Match Patterns:
Always use precisematch
attributes to avoid unexpected results. - Combine with XPath:
Use XPath expressions inmatch
attributes to target specific nodes. - Avoid Hardcoding:
Use<xsl:value-of>
and<xsl:apply-templates>
to dynamically process data. - Leverage Named Templates:
Use named templates to reuse logic and improve maintainability.
Conclusion
The <xsl:template>
element is the backbone of XSLT transformations, enabling you to define how specific XML nodes are processed. By mastering templates and combining them with XPath and other XSLT elements, you can create powerful, dynamic transformations for any XML data.