Welcome to The Coding College, where we make coding concepts easy to understand! In this tutorial, we’ll focus on XSLT Transformation — one of the most important aspects of working with XML data. XSLT (Extensible Stylesheet Language Transformations) is used to transform XML documents into other formats such as HTML, plain text, or even another XML document.
What Is XSLT Transformation?
XSLT Transformation is the process of applying an XSLT stylesheet to an XML document to produce a desired output.
- The XML document contains structured data.
- The XSLT stylesheet defines the rules and templates for how to transform that data.
- The output can be HTML for web pages, plain text, or another structured XML.
How XSLT Transformation Works
XSLT transformation is performed in three key steps:
- Input XML: The source document containing raw XML data.
- XSLT Stylesheet: A file containing the transformation rules written in XSLT.
- XSLT Processor: A tool (browser or software) that applies the XSLT rules to the XML and generates the desired output.
XSLT Transformation Workflow
- Load the XML document.
- Load the XSLT stylesheet.
- Apply the stylesheet to the XML using an XSLT processor.
- Output the transformed result (HTML, text, or another XML document).
XSLT Syntax for Transformation
A basic XSLT stylesheet is structured like this:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<!-- Transformation logic goes here -->
</xsl:template>
</xsl:stylesheet>
Key Elements of XSLT:
<xsl:stylesheet>
: The root element of the XSLT stylesheet.<xsl:template>
: Defines transformation rules for specific XML nodes.<xsl:value-of>
: Extracts and outputs the value of a selected XML node.<xsl:apply-templates>
: Applies templates to child nodes recursively.
Example: Transform XML to HTML
Input XML:
<catalog>
<product>
<name>Laptop</name>
<price>1200</price>
</product>
<product>
<name>Smartphone</name>
<price>800</price>
</product>
</catalog>
XSLT Stylesheet:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<h1>Product Catalog</h1>
<table border="1">
<tr>
<th>Name</th>
<th>Price</th>
</tr>
<xsl:for-each select="catalog/product">
<tr>
<td><xsl:value-of select="name" /></td>
<td><xsl:value-of select="price" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output HTML:
<html>
<body>
<h1>Product Catalog</h1>
<table border="1">
<tr>
<th>Name</th>
<th>Price</th>
</tr>
<tr>
<td>Laptop</td>
<td>1200</td>
</tr>
<tr>
<td>Smartphone</td>
<td>800</td>
</tr>
</table>
</body>
</html>
Key XSLT Transformation Elements
<xsl:template>
: Defines rules for transforming specific nodes.
<xsl:template match="/">
<!-- Transformation logic -->
</xsl:template>
<xsl:value-of>
: Outputs the value of an XML node.
<xsl:value-of select="node_name" />
<xsl:for-each>
: Iterates over a set of nodes.
<xsl:for-each select="node_set">
<!-- Logic for each node -->
</xsl:for-each>
<xsl:apply-templates>
: Applies templates to child nodes.
<xsl:apply-templates select="child_nodes" />
<xsl:if>
: Adds conditional logic to transformations.
<xsl:if test="condition">
<!-- Logic for true condition -->
</xsl:if>
<xsl:choose>
: Similar to a switch-case statement.
<xsl:choose>
<xsl:when test="condition">
<!-- Logic for when condition is true -->
</xsl:when>
<xsl:otherwise>
<!-- Logic for all other cases -->
</xsl:otherwise>
</xsl:choose>
Advantages of XSLT Transformations
- Separation of Concerns: Keeps data (XML) separate from its presentation (HTML).
- Reusable Templates: XSLT stylesheets can be reused across multiple XML documents.
- Cross-Platform: Works on any platform with an XSLT processor.
- Powerful Querying: XPath integration allows precise data selection.
XSLT Processors
Common Tools for XSLT Transformations:
- Web Browsers: Modern browsers like Chrome, Firefox, and Edge have built-in XSLT processors.
- Server-Side Tools: Use libraries like Apache Xalan, Saxon, or libxslt for server-based transformations.
- Online Editors: Tools like XML Grid or Altova XMLSpy allow you to test XSLT transformations interactively.
Conclusion
XSLT is a powerful tool for transforming XML data into usable formats like HTML, plain text, or another XML structure. By mastering XSLT syntax and workflow, you can create dynamic, data-driven applications that separate content from presentation.