Welcome to The Coding College! In this guide, we’ll explore XSLT (Extensible Stylesheet Language Transformations), a powerful technology used to transform XML documents into other formats like HTML, plain text, or even another XML structure. If you’re working with XML and need to present or restructure data, XSLT is an essential tool in your toolbox.
What is XSLT?
XSLT (Extensible Stylesheet Language Transformations) is a language used to transform XML documents. It works by applying a set of rules (or templates) defined in an XSLT stylesheet to an XML document.
Key Features of XSLT:
- Transforming XML: Convert XML data into other formats (e.g., HTML for web pages).
- XPath Integration: XSLT uses XPath to navigate and select parts of the XML document.
- Declarative Language: You define templates and rules rather than writing imperative logic.
Why Use XSLT?
- Separate data (XML) from its presentation (HTML or text).
- Restructure or filter XML data based on specific needs.
- Support for complex transformations and calculations using XPath.
How XSLT Works
XSLT works by applying a stylesheet to an XML document. The transformation process involves:
- Input XML: The XML document you want to transform.
- XSLT Stylesheet: Contains rules and templates for transformation.
- XSLT Processor: Processes the XML and XSLT stylesheet to generate the output.
XSLT Workflow Diagram
- Input: XML Document
- Stylesheet: XSLT file with templates and rules
- Processor: Executes transformation logic
- Output: HTML, text, or another XML
Basic XSLT Syntax
An XSLT stylesheet is itself an XML document with the following structure:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<!-- Transformation rules go here -->
</xsl:template>
</xsl:stylesheet>
Key Elements:
<xsl:stylesheet>
or<xsl:transform>
: The root element of an XSLT document.<xsl:template>
: Defines templates for specific XML elements.<xsl:value-of>
: Extracts and outputs the value of an XML node.<xsl:apply-templates>
: Processes child elements based on templates.
Example: Transform XML to HTML
Input XML:
<library>
<book id="1">
<title>XML Basics</title>
<author>John Doe</author>
</book>
<book id="2">
<title>Learn JavaScript</title>
<author>Jane Smith</author>
</book>
</library>
XSLT Stylesheet:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<h2>Book List</h2>
<table border="1">
<tr>
<th>Title</th>
<th>Author</th>
</tr>
<xsl:for-each select="library/book">
<tr>
<td><xsl:value-of select="title" /></td>
<td><xsl:value-of select="author" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output HTML:
<html>
<body>
<h2>Book List</h2>
<table border="1">
<tr>
<th>Title</th>
<th>Author</th>
</tr>
<tr>
<td>XML Basics</td>
<td>John Doe</td>
</tr>
<tr>
<td>Learn JavaScript</td>
<td>Jane Smith</td>
</tr>
</table>
</body>
</html>
Commonly Used XSLT Elements
1. <xsl:template>
Defines a rule to match an XML element.
<xsl:template match="element_name">
<!-- Transformation logic -->
</xsl:template>
2. <xsl:value-of>
Outputs the value of an XML node.
<xsl:value-of select="node_name" />
3. <xsl:for-each>
Loops through a set of nodes.
<xsl:for-each select="node_set">
<!-- Logic for each node -->
</xsl:for-each>
4. <xsl:if>
Applies conditional logic.
<xsl:if test="condition">
<!-- Logic for true condition -->
</xsl:if>
5. <xsl:choose>
Similar to a switch statement.
<xsl:choose>
<xsl:when test="condition">
<!-- Logic for first condition -->
</xsl:when>
<xsl:otherwise>
<!-- Default logic -->
</xsl:otherwise>
</xsl:choose>
Advantages of XSLT
- Platform Independence: Works on any platform supporting XML.
- Reusability: XSLT stylesheets can be reused for different XML files.
- Powerful Transformations: Supports restructuring, filtering, and formatting XML data.
- Separation of Concerns: Separates data (XML) from presentation logic (XSLT).
Conclusion
XSLT is an invaluable tool for transforming XML into human-readable formats or reorganizing data. With its integration of XPath and template-based rules, XSLT simplifies complex transformations.