XML XSLT
Transform XML documents into different formats
🔄 What is XSLT?
XSLT (eXtensible Stylesheet Language Transformations) transforms XML documents into HTML, text, or other XML formats. It uses templates to match and transform XML elements into desired output formats.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<h1>Welcome to XSLT!</h1>
</xsl:template>
</xsl:stylesheet>
Key XSLT Concepts
Templates
Define transformation rules
<xsl:template match="book">
</xsl:template>
XPath
Select XML elements
<xsl:value-of select="title"/>
Loops
Iterate through elements
<xsl:for-each select="item">
</xsl:for-each>
Output
Generate HTML or XML
<xsl:output method="html"/>
🔹 Basic XSLT Example
XSLT transforms XML data into HTML for web display. The stylesheet defines templates that match XML elements and specify how to transform them. This example shows converting a simple XML book list into an HTML table.
XML Data:
<?xml version="1.0"?>
<catalog>
<book>
<title>XML Basics</title>
<author>John Doe</author>
<price>29.99</price>
</book>
</catalog>
XSLT Stylesheet:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Book Catalog</h2>
<xsl:for-each select="catalog/book">
<p>
<strong><xsl:value-of select="title"/></strong><br/>
Author: <xsl:value-of select="author"/><br/>
Price: $<xsl:value-of select="price"/>
</p>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
HTML Output:
Book Catalog
XML Basics
Author: John Doe
Price: $29.99
🔹 XSLT Elements
XSLT provides various elements for transforming XML data. These elements allow you to select values, create loops, apply conditions, and sort data. Understanding these core elements is essential for effective XML transformations.
<!-- Select value -->
<xsl:value-of select="name"/>
<!-- Loop through elements -->
<xsl:for-each select="student">
<li><xsl:value-of select="name"/></li>
</xsl:for-each>
<!-- Conditional -->
<xsl:if test="price > 30">
<p>Expensive</p>
</xsl:if>
<!-- Sort -->
<xsl:sort select="price" order="ascending"/>
🔹 Creating HTML Tables
XSLT excels at converting XML data into formatted HTML tables. This is useful for displaying structured data on web pages. The for-each loop iterates through XML elements to create table rows dynamically.
<xsl:template match="/">
<table border="1">
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<xsl:for-each select="students/student">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="age"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
🔹 XSLT Choose Statement
The choose statement provides multiple conditional branches, similar to switch-case in programming languages. It allows you to test multiple conditions and execute different transformations based on the results.
<xsl:choose>
<xsl:when test="price < 20">
<p style="color: green;">Budget Friendly</p>
</xsl:when>
<xsl:when test="price < 50">
<p style="color: orange;">Moderate Price</p>
</xsl:when>
<xsl:otherwise>
<p style="color: red;">Premium</p>
</xsl:otherwise>
</xsl:choose>