XSL Examples for Transforming XML
XML (eXtensible Markup Language) is a flexible text format derived from SGML (Standard Generalized Markup Language) and is widely utilized for data representation and transmission. While XML efficiently describes structured data, it does not define how data should be presented. This is where XSL (eXtensible Stylesheet Language) comes into play. Specifically, XSLT (XSL Transformations) allows developers to transform XML documents into various formats, including HTML, plain text, and other XML structures. This article will walk you through various XSLT examples to help you to understand the transformation process effectively.
I. Introduction
A. Overview of XML and XSL
XML provides a structured way to represent data, while XSL enables the transformation of that data into a desired format. By combining these two technologies, you can create dynamic and responsive web applications.
B. Importance of XSL for XML transformation
XSLT is crucial for converting XML data to other formats. It enhances the development process by allowing for easy data manipulation and presentation, catering to various user interfaces.
II. XSLT Example – Basic Transformation
A. Explanation of XSLT
XSLT is a declarative language that uses templates to transform XML documents into other formats. It works by matching XML elements with defined templates and generating the appropriate output.
B. Example of basic XSLT transformation
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="transform.xsl" ?>
<bookstore>
<book>
<title>Learning XML</title>
<author>John Doe</author>
</book>
</bookstore>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/bookstore">
<html>
<body>
<h2>Bookstore</h2>
<table border="1">
<tr>
<th>Title</th>
<th>Author</th>
</tr>
<xsl:apply-templates select="book"/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="book">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="author"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
III. XSLT Example – Applying Templates
A. Overview of templates in XSLT
Templates are fundamental in XSLT, allowing for reusable code segments that match specific XML nodes for transformation.
B. Example of using templates for XML transformation
<fruits>
<fruit>Apple</fruit>
<fruit>Banana</fruit>
<fruit>Cherry</fruit>
</fruits>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/fruits">
<html>
<body>
<h2>Fruits List</h2>
<ul>
<xsl:apply-templates select="fruit"/>
</ul>
</body>
</html>
</xsl:template>
<xsl:template match="fruit">
<li><xsl:value-of select="."/></li>
</xsl:template>
</xsl:stylesheet>
IV. XSLT Example – Using XSLT with Attributes
A. Explanation of handling attributes
XSLT allows you to access and manipulate XML attributes easily. Attributes are accessed using the @ symbol.
B. Example of transforming XML with attributes
<person>
<name first="John" last="Doe"/>
<age>30</age>
</person>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/person">
<html>
<body>
<h2>Person Information</h2>
<p>Name: <xsl:value-of select="name/@first"/> <xsl:value-of select="name/@last"/></p>
<p>Age: <xsl:value-of select="age"/></p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
V. XSLT Example – Using Conditions
A. Overview of conditional statements in XSLT
Conditional statements allow dynamic transformations based on data conditions, enhancing flexibility in your XSLT stylesheets.
B. Example of conditional transformations
<employees>
<employee>
<name>John</name>
<role>developer</role>
</employee>
<employee>
<name>Jane</name>
<role>manager</role>
</employee>
</employees>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/employees">
<html>
<body>
<h2>Employees</h2>
<ul>
<xsl:apply-templates select="employee"/>
</ul>
</body>
</html>
</xsl:template>
<xsl:template match="employee">
<li>
<xsl:value-of select="name"/> -
<xsl:choose>
<xsl:when test="role='developer' ">Developer</xsl:when>
<xsl:when test="role='manager' ">Manager</xsl:when>
<xsl:otherwise>Unknown Role</xsl:otherwise>
</xsl:choose>
</li>
</xsl:template>
</xsl:stylesheet>
VI. XSLT Example – Using Loops
A. Explanation of looping structures in XSLT
Looping structures, achieved through the xsl:for-each, let you iterate over the nodes in the XML document for processing.
B. Example of applying loops for XML processing
<products>
<product id="001">
<name>Laptop</name>
<price>1000</price>
</product>
<product id="002">
<name>Smartphone</name>
<price>500</price>
</product>
</products>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/products">
<html>
<body>
<h2>Product List</h2>
<table border="1">
<tr><th>ID</th><th>Name</th><th>Price</th></tr>
<xsl:for-each select="product">
<tr>
<td><xsl:value-of select="@id"/></td>
<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>
VII. XSLT Example – Sorting Elements
A. Importance of sorting in data presentation
Sorting XML elements is essential for user-friendly data presentation and enhanced readability.
B. Example of sorting XML elements with XSLT
<students>
<student>
<name>Alice</name>
<grade>90</grade>
</student>
<student>
<name>Bob</name>
<grade>75</grade>
</student>
<student>
<name>Charlie</name>
<grade>85</grade>
</student>
</students>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/students">
<html>
<body>
<h2>Student Grades</h2>
<table border="1">
<tr><th>Name</th><th>Grade</th></tr>
<xsl:for-each select="student">
<xsl:sort select="grade" data-type="number" order="descending"/>
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="grade"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
VIII. XSLT Example – Grouping Elements
A. Explanation of grouping in XSLT
Grouping allows you to compile related elements into organized sections, providing clarity in the output.
B. Example of grouping XML elements for organized output
<orders>
<order>
<customer>John</customer>
<total>100.00</total>
</order>
<order>
<customer>Jane</customer>
<total>150.00</total>
</order>
<order>
<customer>John</customer>
<total>200.00</total>
</order>
</orders>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="groupByCustomer" match="order" use="customer"/>
<xsl:template match="/orders">
<html>
<body>
<h2>Orders by Customer</h2>
<xsl:for-each select="order[generate-id() = generate-id(key('groupByCustomer', customer)[1]) ]">
<h3><xsl:value-of select="customer"/></h3>
<ul>
<xsl:for-each select="key('groupByCustomer', customer)">
<li>Order Total: <xsl:value-of select="total"/></li>
</xsl:for-each>
</ul>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
IX. Conclusion
A. Summary of key XSLT examples
Throughout this article, you have learned how to use XSLT for basic transformations, applying templates, handling attributes, utilizing conditions, implementing loops, sorting, and grouping elements. Each example showcases the versatility and power of XSLT to present XML data meaningfully.
B. Encouragement to explore further and practice XSLT transformations
We encourage you to explore further XSLT capabilities and practice these transformations. Experimenting with real XML data will help solidify your understanding and enhance your skills.
FAQ
What is the difference between XML and XSLT?
XML is a markup language used to store and transport data, while XSLT is a language used to transform XML documents into different formats.
Can XSLT be used to convert XML to JSON?
While XSLT is primarily used for transforming XML into other formats, it’s possible to generate JSON-like structures, but there are more straightforward methods for XML to JSON transformation.
Is XSLT still relevant in modern web development?
Yes, although newer technologies such as JavaScript frameworks have gained popularity for client-side processing, XSLT is still valuable, especially in scenarios involving extensive XML data manipulation.
Leave a comment