XSLT Template Rules 🎯

beginner
20 min

XSLT Template Rules 🎯

Welcome to the XSLT Template Rules tutorial! In this lesson, we'll dive deep into XSLT (eXtensible Stylesheet Language Transformations), a powerful tool used for transforming XML documents into other formats like HTML, plain text, or even XML itself.

What are XSLT Template Rules? 📝

Template rules are the building blocks of XSLT stylesheets. They define how an XML document's elements and attributes should be processed. Each template rule consists of a template, which contains a match pattern and a template body, where the processing logic resides.

Match Pattern 💡

The match pattern specifies the XML elements or attributes that the template rule should apply to. It can be as simple as an element name, or more complex expressions using XPath.

Here's a basic example:

xml
<xsl:template match="book"> <!-- Template body goes here --> </xsl:template>

In this example, the template rule will apply to any book element in the XML document.

Template Body 💡

The template body contains the instructions for transforming the matched elements. It can contain any number of XSLT instructions, such as <xsl:value-of>, <xsl:copy>, or <xsl:apply-templates>.

Let's modify the previous example to output the book's title:

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

In this example, the template body creates an <h1> element and inserts the book's title using the <xsl:value-of> instruction.

Example Project 🎯

Let's create an XSLT stylesheet to transform a simple book catalog XML into HTML.

XML Document (book-catalog.xml)

xml
<catalog> <book id="bk101"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description>An in-depth look at creating applications with XML.</description> </book> <!-- More books here --> </catalog>

XSLT Stylesheet (book-catalog.xsl)

xml
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/catalog"> <html> <head> <title><xsl:value-of select="title"/></title> </head> <body> <h1><xsl:value-of select="title"/></h1> <ul> <xsl:apply-templates select="book"/> </ul> </body> </html> </xsl:template> <xsl:template match="book"> <li> <h2><xsl:value-of select="title"/></h2> <h3>Author: <xsl:value-of select="author"/></h3> <h4>Genre: <xsl:value-of select="genre"/></h4> <h4>Price: <xsl:value-of select="price"/></h4> <h4>Publish Date: <xsl:value-of select="publish_date"/></h4> <h4>Description: <xsl:value-of select="description"/></h4> </li> </xsl:template> </xsl:stylesheet>

When you apply the XSLT transformation, you'll get an HTML document as output:

sh
xsltproc book-catalog.xsl book-catalog.xml > book-catalog.html

Quiz 🎯

Quick Quiz
Question 1 of 1

Which XSLT instruction creates an `<h1>` element with the book's title?

That's it for this lesson on XSLT Template Rules! In the next lesson, we'll dive into more advanced XSLT concepts. Happy coding! 🚀