Practical XSLT Transform Examples: Streamlining Your WorkflowExtensible Stylesheet Language Transformations (XSLT) is a powerful tool for transforming XML documents into various formats, including HTML, text, and even other XML documents. By leveraging XSLT, developers can streamline workflows, enhance data presentation, and automate repetitive tasks. This article will explore practical examples of XSLT transformations that can help optimize your workflow.
Understanding XSLT
XSLT is a declarative language used to define a set of rules for transforming XML documents. It employs a tree structure to navigate through elements, attributes, and text nodes in the XML data. With its built-in templates and functions, XSLT can manipulate XML documents efficiently.
Key Components of XSLT
- Stylesheet: An XSLT file that contains the transformation rules.
- Templates: Rules that define how each element in the source XML is transformed.
- XPath: A language used to navigate XML documents, allowing for selection of nodes.
Example 1: Simple XML to HTML Transformation
Let’s start with a straightforward example: converting XML data into an HTML table. Given the following XML structure containing a list of books:
<library> <book> <title>Learning XSLT</title> <author>John Doe</author> <year>2021</year> </book> <book> <title>Mastering Java</title> <author>Jane Smith</author> <year>2020</year> </book> </library>
You can use the following XSLT stylesheet to transform this XML into an HTML table:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/library"> <html> <body> <h2>Book List</h2> <table border="1"> <tr> <th>Title</th> <th>Author</th> <th>Year</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> <td><xsl:value-of select="year"/></td> </tr> </xsl:template> </xsl:stylesheet>
Output
This transformation results in:
<html> <body> <h2>Book List</h2> <table border="1"> <tr> <th>Title</th> <th>Author</th> <th>Year</th> </tr> <tr> <td>Learning XSLT</td> <td>John Doe</td> <td>2021</td> </tr> <tr> <td>Mastering Java</td> <td>Jane Smith</td> <td>2020</td> </tr> </table> </body> </html>
This example showcases how you can easily organize XML data in a readable format, facilitating better presentation on web pages.
Example 2: Filtering Data with XSLT
Now, let’s look at how XSLT can be used to filter data based on specific criteria. Suppose we want to extract only the books published after 2020 from the previous XML structure. Here’s an updated XSLT stylesheet:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/library"> <html> <body> <h2>Books Published After 2020</h2> <ul> <xsl:apply-templates select="book[year > 2020]"/> </ul> </body> </html> </xsl:template> <xsl:template match="book"> <li> <xsl:value-of select="title"/> by <xsl:value-of select="author"/> </li> </xsl:template> </xsl:stylesheet>
Output
The output for this transformation would yield:
”`html
<body> <h2>Books Published After 2020</h2> <ul> <li>Learning XSLT by John Doe</li> </ul> </body>
Leave a Reply