XSLT Examples

Welcome to The Coding College! In this guide, we’ll explore a variety of XSLT examples that demonstrate how XSLT can transform XML into different formats like HTML, plain text, or another XML structure. Whether you’re a beginner or an experienced developer, these practical examples will help you understand how XSLT works and how it can be applied to solve real-world problems.

What Is XSLT?

XSLT (Extensible Stylesheet Language Transformations) is a language used to transform XML documents into other formats. By defining templates, XSLT specifies how elements, attributes, and data in an XML file should be processed and presented.

Why Use XSLT?

  • Separation of Concerns: Keeps data (XML) separate from presentation logic.
  • Portability: Easily adapt XML data to various output formats.
  • Efficiency: Automates transformations, reducing manual effort.

XSLT Examples

Example 1: Transform XML to HTML

Input XML: books.xml

<?xml version="1.0" encoding="UTF-8"?>
<library>
  <book>
    <title>Learning XML</title>
    <author>John Doe</author>
    <price>29.99</price>
  </book>
  <book>
    <title>Advanced XSLT</title>
    <author>Jane Smith</author>
    <price>39.99</price>
  </book>
</library>

XSLT File: books-to-html.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
    <html>
      <body>
        <h1>Book List</h1>
        <table border="1">
          <tr>
            <th>Title</th>
            <th>Author</th>
            <th>Price</th>
          </tr>
          <xsl:for-each select="library/book">
            <tr>
              <td><xsl:value-of select="title" /></td>
              <td><xsl:value-of select="author" /></td>
              <td><xsl:value-of select="price" /></td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Output HTML:

<html>
  <body>
    <h1>Book List</h1>
    <table border="1">
      <tr>
        <th>Title</th>
        <th>Author</th>
        <th>Price</th>
      </tr>
      <tr>
        <td>Learning XML</td>
        <td>John Doe</td>
        <td>29.99</td>
      </tr>
      <tr>
        <td>Advanced XSLT</td>
        <td>Jane Smith</td>
        <td>39.99</td>
      </tr>
    </table>
  </body>
</html>

Example 2: Transform XML to Plain Text

XSLT File: books-to-text.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="text" />
  <xsl:template match="/">
    <xsl:for-each select="library/book">
      Title: <xsl:value-of select="title" />
      Author: <xsl:value-of select="author" />
      Price: <xsl:value-of select="price" />
      ---------------------------
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

Output Text:

Title: Learning XML
Author: John Doe
Price: 29.99
---------------------------
Title: Advanced XSLT
Author: Jane Smith
Price: 39.99
---------------------------

Example 3: Adding a New Attribute

Input XML: products.xml

<?xml version="1.0" encoding="UTF-8"?>
<products>
  <product>
    <name>Phone</name>
    <price>699</price>
  </product>
  <product>
    <name>Laptop</name>
    <price>999</price>
  </product>
</products>

XSLT File: add-attribute.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
    <updatedProducts>
      <xsl:for-each select="products/product">
        <product type="Electronics">
          <name><xsl:value-of select="name" /></name>
          <price><xsl:value-of select="price" /></price>
        </product>
      </xsl:for-each>
    </updatedProducts>
  </xsl:template>
</xsl:stylesheet>

Output XML:

<?xml version="1.0" encoding="UTF-8"?>
<updatedProducts>
  <product type="Electronics">
    <name>Phone</name>
    <price>699</price>
  </product>
  <product type="Electronics">
    <name>Laptop</name>
    <price>999</price>
  </product>
</updatedProducts>

Example 4: Removing Elements

XSLT File: remove-elements.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
    <filteredProducts>
      <xsl:for-each select="products/product">
        <product>
          <name><xsl:value-of select="name" /></name>
        </product>
      </xsl:for-each>
    </filteredProducts>
  </xsl:template>
</xsl:stylesheet>

Output XML:

<?xml version="1.0" encoding="UTF-8"?>
<filteredProducts>
  <product>
    <name>Phone</name>
  </product>
  <product>
    <name>Laptop</name>
  </product>
</filteredProducts>

Example 5: Conditional Transformation

Input XML: users.xml

<?xml version="1.0" encoding="UTF-8"?>
<users>
  <user>
    <name>John</name>
    <age>28</age>
  </user>
  <user>
    <name>Jane</name>
    <age>17</age>
  </user>
</users>

XSLT File: conditional-transform.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
    <filteredUsers>
      <xsl:for-each select="users/user">
        <xsl:if test="age >= 18">
          <user>
            <name><xsl:value-of select="name" /></name>
            <age><xsl:value-of select="age" /></age>
          </user>
        </xsl:if>
      </xsl:for-each>
    </filteredUsers>
  </xsl:template>
</xsl:stylesheet>

Output XML:

<?xml version="1.0" encoding="UTF-8"?>
<filteredUsers>
  <user>
    <name>John</name>
    <age>28</age>
  </user>
</filteredUsers>

Conclusion

XSLT is a versatile tool for transforming XML into various formats and structures. These examples highlight its power in tasks such as formatting data for presentation, filtering content, adding new elements, and performing conditional transformations.

For more in-depth tutorials and examples, visit The Coding College and unlock the full potential of XML and XSLT!

Leave a Comment