XSLT Elements

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 and patterns to restructure and format data efficiently.


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <html><body>Hello XSLT!</body></html>
    </xsl:template>
</xsl:stylesheet>
                                    

Core XSLT Elements

XSLT provides essential elements for transforming XML data. These elements work together to select, iterate, sort, and format content, enabling powerful document transformations with minimal code.

📋

xsl:template

Define transformation rules

<xsl:template match="/">
🎯

xsl:value-of

Extract element values

<xsl:value-of select="title"/>
🔁

xsl:for-each

Loop through elements

<xsl:for-each select="book">
↕️

xsl:sort

Sort elements

<xsl:sort select="price"/>

🔹 Basic XSLT Transformation

Transform XML to HTML using XSLT:

XML Document:

<?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>
                <p>Title: <xsl:value-of select="catalog/book/title"/></p>
                <p>Author: <xsl:value-of select="catalog/book/author"/></p>
            </body>
        </html>
    </xsl:template>
    
</xsl:stylesheet>

HTML Output:

Book Catalog

Title: XML Basics

Author: John Doe

🔹 xsl:for-each Loop

Iterate through multiple elements:

<xsl:template match="/">
    <html>
        <body>
            <h2>Book List</h2>
            <table border="1">
                <tr>
                    <th>Title</th>
                    <th>Author</th>
                </tr>
                
                <xsl:for-each select="catalog/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:sort Element

Sort elements by specific criteria:

<xsl:for-each select="catalog/book">
    <!-- Sort by price (ascending) -->
    <xsl:sort select="price" data-type="number" order="ascending"/>
    
    <p>
        <xsl:value-of select="title"/> - 
        $<xsl:value-of select="price"/>
    </p>
</xsl:for-each>

Sort Attributes:

  • select: Element to sort by
  • order: "ascending" or "descending"
  • data-type: "text" or "number"

🔹 xsl:if Conditional

Apply conditions to transformations:

<xsl:for-each select="catalog/book">
    <xsl:if test="price &gt; 30">
        <p style="color: red;">
            <xsl:value-of select="title"/> - Expensive!
        </p>
    </xsl:if>
    
    <xsl:if test="price &lt;= 30">
        <p style="color: green;">
            <xsl:value-of select="title"/> - Affordable
        </p>
    </xsl:if>
</xsl:for-each>

🔹 xsl:choose (Switch)

Multiple conditional branches:

<xsl:for-each select="catalog/book">
    <xsl:choose>
        <xsl:when test="price &gt; 50">
            <p style="color: red;">Premium: <xsl:value-of select="title"/></p>
        </xsl:when>
        
        <xsl:when test="price &gt; 30">
            <p style="color: orange;">Standard: <xsl:value-of select="title"/></p>
        </xsl:when>
        
        <xsl:otherwise>
            <p style="color: green;">Budget: <xsl:value-of select="title"/></p>
        </xsl:otherwise>
    </xsl:choose>
</xsl:for-each>

🔹 xsl:apply-templates

Apply templates to selected nodes:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="/">
        <html>
            <body>
                <xsl:apply-templates select="catalog/book"/>
            </body>
        </html>
    </xsl:template>
    
    <xsl:template match="book">
        <div>
            <h3><xsl:value-of select="title"/></h3>
            <p>By: <xsl:value-of select="author"/></p>
        </div>
    </xsl:template>
    
</xsl:stylesheet>

🔹 xsl:attribute

Add attributes to output elements:

<xsl:template match="book">
    <div>
        <xsl:attribute name="class">book-item</xsl:attribute>
        <xsl:attribute name="id">
            <xsl:value-of select="@id"/>
        </xsl:attribute>
        
        <h3><xsl:value-of select="title"/></h3>
    </div>
</xsl:template>

HTML Output:

<div class="book-item" id="1">...</div>

🔹 xsl:element

Create elements dynamically:

<xsl:template match="book">
    <xsl:element name="div">
        <xsl:element name="h2">
            <xsl:value-of select="title"/>
        </xsl:element>
    </xsl:element>
</xsl:template>

🔹 Complete XSLT Example

Full transformation with multiple elements:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="/">
        <html>
            <head>
                <title>Book Catalog</title>
            </head>
            <body>
                <h1>Available Books</h1>
                <xsl:apply-templates select="catalog/book"/>
            </body>
        </html>
    </xsl:template>
    
    <xsl:template match="book">
        <div class="book">
            <h2><xsl:value-of select="title"/></h2>
            <p>Author: <xsl:value-of select="author"/></p>
            <p>Price: $<xsl:value-of select="price"/></p>
            
            <xsl:if test="price &lt; 30">
                <span style="color: green;">On Sale!</span>
            </xsl:if>
        </div>
    </xsl:template>
    
</xsl:stylesheet>

💡 XSLT Best Practices:

  • Use templates for reusable transformations
  • Keep stylesheets modular and organized
  • Use xsl:apply-templates for complex structures
  • Comment your XSLT code for clarity
  • Test transformations with various XML inputs

🧠 Test Your Knowledge

Which XSLT element is used to extract values?