XML (Extensible Markup Language) and XSLT (eXtensible Stylesheet Language Transformations) are technologies used for organizing and presenting data on the web. Here’s a simple explanation with an example:
XML (Extensible Markup Language): XML is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. It's commonly used to structure and store data in a hierarchical format.
Example of XML:
Example
<?xml version="1.0" encoding="UTF-8"?> <bookstore> <book category="fiction"> <title>Harry Potter</title> <author>J.K. Rowling</author> <year>2005</year> </book> <book category="non-fiction"> <title>Introduction to XML</title> <author>John Doe</author> <year>2010</year> </book> </bookstore>
In this example, <bookstore>
is the root element, <book>
elements are child elements under <bookstore>
, and <title>
, <author>
, and <year>
are nested within each <book>
.
XSLT (eXtensible Stylesheet Language Transformations): XSLT is a language for transforming XML documents into other formats, such as HTML, XHTML, or XML itself. It uses XSLT stylesheets to describe how the transformation should be done.
Example of XSLT: Let's say we want to transform the above XML into an HTML list (<ul>
):
Example
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>Book List</h2> <ul> <xsl:for-each select="bookstore/book"> <li> <xsl:value-of select="title"/> by <xsl:value-of select="author"/> (<xsl:value-of select="year"/>) </li> </xsl:for-each> </ul> </body> </html> </xsl:template> </xsl:stylesheet>
In this XSLT stylesheet:
<xsl:stylesheet>
defines that this is an XSLT stylesheet with a specified version and namespace.<xsl:template match="/">
specifies that the template applies to the root of the XML document.<html>
, <body>
, <h2>
, <ul>
, <li>
) along with XSLT instructions (<xsl:for-each>
, <xsl:value-of>
) to iterate through <book>
elements and output their <title>
, <author>
, and <year>
.Transformation Result: Applying this XSLT to the XML will produce an HTML document with a list of books:
Example
<html> <body> <h2>Book List</h2> <ul> <li>Harry Potter by J.K. Rowling (2005)</li> <li>Introduction to XML by John Doe (2010)</li> </ul> </body> </html>
This is a basic example of how XML and XSLT work together to transform structured data into a formatted output suitable for presentation on the web.