XSLT Built-in Templates: A Comprehensive Guide 🎯

beginner
16 min

XSLT Built-in Templates: A Comprehensive Guide 🎯

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!

Understanding XSLT and Built-in Templates 📝

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.

Why Use XSLT Built-in Templates? 💡

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.

Basic Structure of an XSLT Document 📝

An XSLT document consists of three parts:

  1. <?xml version="1.0" encoding="UTF-8"?> - XML declaration
  2. <xsl:stylesheet> - The root element of the XSLT document
  3. <xsl:template match="root"> - A template that matches the root element of the input XML document

Getting Started with Built-in Templates 💡

To use built-in templates, you need to include the exslt namespace in your XSLT document. Here's how to do it:

xml
<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.

Template for Elements 💡

The xsl:apply-templates template is used to apply other templates to the current node, recursively processing its children.

xml
<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.

Template for Attributes 💡

To process attributes, you can use the xsl:attribute built-in template.

xml
<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.

Practical Example 💡

Let's transform an XML document containing books into HTML using built-in templates.

XML Document

xml
<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

xml
<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
<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>

Quiz 🎯

Quick Quiz
Question 1 of 1

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! 🥳