Welcome to our in-depth tutorial on XSLT Built-in Templates! In this lesson, we'll delve into the world of XSLT, exploring how to use built-in templates to transform XML documents. Let's get started!
XSLT (Extensible Stylesheet Language Transformations) is a language used to transform XML documents into different formats, such as HTML, PDF, and plain text. XSLT stylesheets define the transformation rules using templates, which are reusable blocks of XSLT code.
Built-in templates are a collection of predefined templates provided by XSLT to handle common XML structures like elements, attributes, and namespaces.
Built-in templates simplify the process of creating XSLT stylesheets by providing a set of pre-built templates that handle common XML structures. This not only saves time but also makes it easier to write and maintain XSLT code.
An XSLT document consists of three parts:
<?xml version="1.0" encoding="UTF-8"?> - XML declaration<xsl:stylesheet> - The root element of the XSLT document<xsl:template match="root"> - A template that matches the root element of the input XML documentTo use built-in templates, you need to include the exslt namespace in your XSLT document. Here's how to do it:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
version="2.0">Now, let's explore some commonly used built-in templates.
The xsl:apply-templates template is used to apply other templates to the current node, recursively processing its children.
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>In this example, * matches any element, local-name() gets the local name of the element, and @*|node() selects all attributes and child nodes.
To process attributes, you can use the xsl:attribute built-in template.
<xsl:template match="@*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>In this example, @* matches all attributes, and local-name() gets the local name of the attribute.
Let's transform an XML document containing books into HTML using built-in templates.
XML Document
<books>
<book id="001">
<title>Book Title 1</title>
<author>Author 1</author>
<year>2020</year>
</book>
<book id="002">
<title>Book Title 2</title>
<author>Author 2</author>
<year>2019</year>
</book>
</books>XSLT Document
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
version="2.0">
<xsl:template match="books">
<html>
<body>
<h1>List of Books</h1>
<xsl:apply-templates select="book"/>
</body>
</html>
</xsl:template>
<xsl:template match="book">
<div>
<h2><xsl:value-of select="@id"/>: <xsl:value-of select="title"/></h2>
<p>Author: <xsl:value-of select="author"/></p>
<p>Year: <xsl:value-of select="year"/></p>
</div>
</xsl:template>
<!-- Include built-in templates for elements and attributes -->
<xsl:import href="exsl:built-in.xsl"/>
</xsl:stylesheet>When you apply this XSLT to the XML document, you'll get the following HTML output:
<html>
<body>
<h1>List of Books</h1>
<div>
<h2>001: Book Title 1</h2>
<p>Author: Author 1</p>
<p>Year: 2020</p>
</div>
<div>
<h2>002: Book Title 2</h2>
<p>Author: Author 2</p>
<p>Year: 2019</p>
</div>
</body>
</html>What does the `xsl:apply-templates` template do?
That's it for our XSLT Built-in Templates tutorial! We hope this comprehensive guide has helped you understand the basics and advanced aspects of using built-in templates in XSLT. Happy transforming! 🥳