XML XSLT Introduction
- XSLT (Extensible Stylesheet Language Transformations) is a language used for transforming structured data into other formats, such as HTML, XML, and text.
- It is an XML-based language and is used for performing transformations of XML documents.
Syntax
The basic syntax for an XSLT stylesheet is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
...
</xsl:template>
</xsl:stylesheet>
- The
xsl:stylesheet
element is the root element of an XSLT stylesheet. - The
xmlns:xsl
attribute defines the namespace for XSLT. - The
version
attribute specifies the version of XSLT being used. - The
xsl:template
element is used to match nodes in the input document.
Example
Suppose we have an XML document, books.xml
, containing information about books as follows:
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>
<title>Alice in Wonderland</title>
<author>Lewis Carroll</author>
<year>1865</year>
</book>
<book>
<title>The Adventures of Tom Sawyer</title>
<author>Mark Twain</author>
<year>1876</year>
</book>
</books>
We want to transform this data into an HTML table. An XSLT stylesheet to achieve this could look like:
<?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>
<table>
<tr>
<th>Title</th>
<th>Author</th>
<th>Year</th>
</tr>
<xsl:for-each select="books/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:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Here, we have used the xsl:for-each
element to loop through each book element in the input document and generate an HTML table row for each.
Output
When this XSLT stylesheet is applied to the books.xml
document, we get an output like:
<html>
<body>
<table>
<tr>
<th>Title</th>
<th>Author</th>
<th>Year</th>
</tr>
<tr>
<td>Alice in Wonderland</td>
<td>Lewis Carroll</td>
<td>1865</td>
</tr>
<tr>
<td>The Adventures of Tom Sawyer</td>
<td>Mark Twain</td>
<td>1876</td>
</tr>
</table>
</body>
</html>
Explanation
In the stylesheet, we define a template with the match
attribute set to /
, which selects the root node of the input document. We then use XSLT elements to construct an HTML document.
We use the xsl:for-each
element to loop through each book element in the input document and generate an HTML table row for each. Inside the xsl:for-each
loop, we use the xsl:value-of
element to output the content of the title
, author
, and year
elements.
Use
XSLT can be used in a variety of scenarios, such as:
- Converting XML to HTML or another text-based format.
- Extracting specific data from an XML document.
- Filtering and restructuring XML content.
- Creating charts and diagrams from XML data.
Important Points
- XSLT is a language used for transforming structured data into other formats.
- It is an XML-based language and is used for performing transformations of XML documents.
- The basic syntax for an XSLT stylesheet consists of an
xsl:stylesheet
element that defines the root element, a namespace declaration for XSLT, a version attribute, and anxsl:template
element that contains the transformation logic. - An XSLT stylesheet can be used to generate HTML, XML, or text-based output.
- XSLT can be used in a variety of scenarios, such as data extraction, filtering, and restructuring.
Summary
In this tutorial, we have introduced XSLT, a language used for transforming structured data into other formats, such as HTML, XML, and text. We have covered the basic syntax for XSLT stylesheets, an example transformation of XML data into an HTML table, the output generated by the stylesheet, its explanation, use cases, important points, and summary.