Welcome to CodeYourCraft's XSLT mode Attribute tutorial! Today, we're going to explore one of the powerful features of XSLT (Extensible Stylesheet Language Transformations) that helps transform XML documents into other formats like HTML, JSON, and more.
XSLT mode attribute is used to specify the output format of an XSLT transformation. It allows us to create stylesheets that can process XML documents in different ways, making it suitable for various use cases.
XSLT defines several modes to handle different parts of an XML document. Each mode serves a specific purpose and helps in transforming the XML data effectively.
xml: ModesThese modes are defined in the XML Namespace (xml:) and handle the default processing of an XML document. The most common xml: mode is the identity transform, which simply copies the input XML to the output without modifications.
You can also create user-defined modes to perform custom transformations on the XML data. These modes are defined using the xsl:mode element in the XSLT stylesheet.
The XSLT mode attribute is specified using the xsl:output element in the XSLT stylesheet. The syntax is as follows:
<xsl:output method="output-method" omit-xml-declaration="boolean" indent="boolean" encoding="encoding-type" media-type="media-type"/>method: Specifies the output format of the transformation. Common output methods include xml, html, text, and json.omit-xml-declaration: A boolean value that determines whether the XML declaration (XML version, encoding, and standalone) should be included in the output.indent: A boolean value that specifies whether the output should be indented for better readability.encoding: The character encoding of the output. Common encodings include UTF-8, ISO-8859-1, and Windows-1252.media-type: The MIME type of the output. For XML, the media-type is application/xml.Let's take an example to understand the XSLT mode attribute better. Suppose we have an XML document representing a list of books:
<books>
<book id="001">
<title>The Catcher in the Rye</title>
<author>J.D. Salinger</author>
</book>
<book id="002">
<title>To Kill a Mockingbird</title>
<author>Harper Lee</author>
</book>
</books>And an XSLT stylesheet for transforming the XML into HTML:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/1999/XSL/Transform http://www.w3.org/TR/xslt10/XSLT.xsd">
<xsl:output method="html" indent="yes" encoding="UTF-8" omit-xml-declaration="yes"/>
<xsl:template match="/">
<html>
<head>
<title>Book List</title>
</head>
<body>
<h1>Book List</h1>
<ul>
<xsl:apply-templates select="/books/book"/>
</ul>
</body>
</html>
</xsl:template>
<xsl:template match="book">
<li>
<h2><xsl:value-of select="title"/> by <xsl:value-of select="author"/></h2>
</li>
</xsl:template>
</xsl:stylesheet>When we apply the XSLT transformation, we get the following HTML output:
<html>
<head>
<title>Book List</title>
</head>
<body>
<h1>Book List</h1>
<ul>
<li>
<h2>The Catcher in the Rye by J.D. Salinger</h2>
</li>
<li>
<h2>To Kill a Mockingbird by Harper Lee</h2>
</li>
</ul>
</body>
</html>In this example, the XSLT mode attribute specifies the output format as HTML.
What does the `xsl:output` element do in an XSLT stylesheet?
That's it for today's tutorial on the XSLT mode attribute! In the next lesson, we'll dive deeper into user-defined modes and create custom transformations for our XML data. Happy coding! 💻📚