XSLT Introduction
Transform XML documents into different formats
🔄 What is XSLT?
XSLT (Extensible Stylesheet Language Transformations) is a language for transforming XML documents into other formats like HTML, text, or different XML structures. It uses templates to define transformation rules.
<!-- XML Data -->
<person>
<name>John</name>
</person>
<!-- XSLT transforms to HTML -->
<h1>John</h1>
Output:
John
XSLT Key Concepts
Transform
Convert XML to other formats
XML → HTML
Templates
Define transformation rules
<xsl:template match="/">
XPath
Select XML nodes to transform
<xsl:value-of select="name"/>
Iterate
Loop through XML elements
<xsl:for-each select="item">
🔹 Basic XSLT Structure
Every XSLT document starts with an XML declaration and a stylesheet element. The stylesheet contains templates that define how to transform XML elements into the desired output format.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h1>My Transformation</h1>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Components:
xsl:stylesheet: Root element
xsl:template: Transformation rule
match="/": Matches document root
🔹 Simple XML to HTML Transformation
This example shows how XSLT transforms XML data into an HTML page. The template matches the root and extracts values from XML elements to create HTML output.
<!-- Input XML -->
<?xml version="1.0"?>
<catalog>
<book>
<title>Learning XML</title>
<author>John Smith</author>
</book>
</catalog>
<!-- XSLT Stylesheet -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2><xsl:value-of select="catalog/book/title"/></h2>
<p>By: <xsl:value-of select="catalog/book/author"/></p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
HTML Output:
Learning XML
By: John Smith
🔹 Using xsl:value-of
The xsl:value-of element extracts the value of an XML node and inserts it into the output. Use the select attribute with XPath to specify which node to extract.
<!-- Input XML -->
<student>
<name>Alice</name>
<grade>A</grade>
<age>20</age>
</student>
<!-- XSLT -->
<xsl:template match="/">
<div>
<p>Student: <xsl:value-of select="student/name"/></p>
<p>Grade: <xsl:value-of select="student/grade"/></p>
<p>Age: <xsl:value-of select="student/age"/></p>
</div>
</xsl:template>
Output:
Student: Alice
Grade: A
Age: 20
🔹 Why Use XSLT?
XSLT provides powerful capabilities for working with XML data in various scenarios:
Common Use Cases:
- Web Display: Convert XML data to HTML for web browsers
- Data Exchange: Transform XML between different formats
- Report Generation: Create formatted reports from XML data
- Data Migration: Convert legacy XML to new structures
- Content Publishing: Generate multiple output formats from single XML source
🔹 XSLT Processing Model
Understanding how XSLT processes XML documents helps you write effective transformations:
Processing Steps:
- Parse XML: XSLT processor reads the input XML document
- Load Stylesheet: XSLT stylesheet is loaded and parsed
- Match Templates: Processor finds templates matching XML nodes
- Apply Rules: Transformation rules are applied to matched nodes
- Generate Output: Result document is created in target format
<!-- Simple flow example -->
XML Input → XSLT Processor → HTML Output
<book>Title</book> → [XSLT] → <h1>Title</h1>