Welcome to our comprehensive guide on XSLT (eXtensible Stylesheet Language Transformations)! In this tutorial, we'll explore the world of XML transformation, making it easy for beginners and providing enough depth for intermediates. Let's dive in! 🐳
XML (eXtensible Markup Language) is a markup language used to store and transport data. XSLT, on the other hand, is a language for transforming XML documents into other formats like HTML, PDF, or even XML itself.
XML uses tags to structure data. Unlike HTML, XML doesn't have predefined tags; instead, you can create your own. Here's a simple XML example:
<book>
<title>The Catcher in the Rye</title>
<author>J.D. Salinger</author>
<year>1951</year>
</book>XSLT allows you to transform an XML document into another format using templates. These templates define how to process each element in the XML document.
An XSLT document, also known as a stylesheet, consists of three parts:
Here's a simple XSLT example:
<!-- XSLT Stylesheet -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Template for the book element -->
<xsl:template match="book">
<html>
<body>
<h1><xsl:value-of select="title"/></h1>
<h2>Author: <xsl:value-of select="author"/></h2>
<h2>Year: <xsl:value-of select="year"/></h2>
</body>
</html>
</xsl:template>
</xslt:stylesheet>match: Specifies which XML elements the template applies to.select: Returns the elements or attributes within the matched element.Templates can be recursive, allowing you to process complex XML structures.
XSLT provides several built-in functions for various purposes, such as formatting, string manipulation, and date/time operations.
Combine the XML and XSLT examples from earlier to create a complete transformation:
<!-- XML Data -->
<book>
<title>The Catcher in the Rye</title>
<author>J.D. Salinger</author>
<year>1951</year>
</book><!-- XSLT Stylesheet -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Template for the book element -->
<xsl:template match="book">
<html>
<body>
<h1><xsl:value-of select="title"/></h1>
<h2>Author: <xsl:value-of select="author"/></h2>
<h2>Year: <xsl:value-of select="year"/></h2>
</body>
</html>
</xsl:template>
</xslt:stylesheet>This transformation will output an HTML page displaying the book's title, author, and year.
Now that you've learned the basics, it's time to practice! Try transforming different XML documents using XSLT.
:::quiz Question: Given the following XML:
<library>
<book id="1">
<title>The Catcher in the Rye</title>
<author>J.D. Salinger</author>
<year>1951</year>
</book>
<book id="2">
<title>To Kill a Mockingbird</title>
<author>Harper Lee</author>
<year>1960</year>
</book>
</library>Write an XSLT stylesheet to output an HTML table displaying the book's title, author, and year.
A:
<!-- XSLT Stylesheet -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Template for the library element -->
<xsl:template match="library">
<html>
<body>
<table border="1">
<tr>
<th>Title</th>
<th>Author</th>
<th>Year</th>
</tr>
<!-- Template for each book element -->
<xsl:apply-templates select="book"/>
</table>
</body>
</html>
</xsl:template>
<!-- Template for the book element -->
<xsl:template match="book">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="author"/></td>
<td><xsl:value-of select="year"/></td>
</tr>
</xsl:template>
</xslt:stylesheet>Correct: A Explanation: The given XSLT stylesheet matches the library element and creates an HTML table with a header row and applies templates to each book element. Each book is represented as a table row with the title, author, and year values.