What is XSLT, and how does it transform XML data? Provide an example demonstrating its usage.
XSLT (eXtensible Stylesheet Language Transformations) is a language designed for transforming XML documents into other formats, such as XML, HTML, plain text, or other structured documents. It operates by defining rules in a stylesheet that can manipulate and arrange the data contained in XML.
### How XSLT Transforms XML Data
The transformation process involves several key components:
1. **XML Document**: This is the source data that you want to transform.
2. **XSLT Stylesheet**: This defines the rules and templates for how the XML data should be transformed.
3. **XSLT Processor**: This is a tool or library that reads the XML document and the XSLT stylesheet, applies the transformation rules, and outputs the result.
The transformation is generally performed as follows:
- The XSLT processor reads the XML document and the associated XSLT stylesheet.
- It applies the templates defined in the XSLT to the elements of the XML document, generating the desired output format.
### Example of XSLT Transformation
#### XML Input
Here is an example of a simple XML document representing a list of books:
```xml
<catalog>
<book>
<title>Harry Potter and the Philosopher's Stone</title>
<author>J.K. Rowling</author>
<price>29.99</price>
</book>
<book>
<title>The Lord of the Rings</title>
<author>J.R.R. Tolkien</author>
<price>39.99</price>
</book>
</catalog>
```
#### XSLT Stylesheet
Now, let’s create an XSLT stylesheet to transform the XML into an HTML table:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8" indent="yes"/>
<xsl:template match="/catalog">
<html>
<head>
<title>Book Catalog</title>
</head>
<body>
<h2>Book Catalog</h2>
<table border="1">
<tr>
<th>Title</th>
<th>Author</th>
<th>Price</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="price"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
```
### Explanation of the XSLT Stylesheet
1. The `xsl:stylesheet` element declares the document as an XSLT stylesheet.
2. The `xsl:output` element specifies that the output will be HTML and UTF-8 encoded.
3. The first `xsl:template` matches the root element of the XML (`catalog`) and creates the structure of an HTML document, including a header and a table.
4. The `xsl:apply-templates` instruction processes every `book` element in the XML, passing control to the second template.
5. The second `xsl:template` matches each `book` element and generates a table row with the book's title, author, and price.
#### Resulting HTML Output
When processed, the above XSLT will generate the following HTML:
```html
<html>
<head>
<title>Book Catalog</title>
</head>
<body>
<h2>Book Catalog</h2>
<table border="1">
<tr>
<th>Title</th>
<th>Author</th>
<th>Price</th>
</tr>
<tr>
<td>Harry Potter and the Philosopher's Stone</td>
<td>J.K. Rowling</td>
<td>29.99</td>
</tr>
<tr>
<td>The Lord of the Rings</td>
<td>J.R.R. Tolkien</td>
<td>39.99</td>
</tr>
</table>
</body>
</html>
```
### Conclusion
XSLT is a powerful tool for transforming XML into different formats, allowing you to represent and present structured data in various ways. It is widely used in web development, data interchange, and document management.