Meaning of XSLT

Filter Course


Meaning of XSLT

Published by: Sujan

Published date: 16 Jun 2021

Meaning of XSLT- Photo

XSLT (Extensible Stylesheet Language Transformation)

XSLT is a language for transforming XML documents. It is the most important part of XSL.

XPath is a language for navigating in XML documents.

XQuery is a language for querying XML documents.

XSL stands for EXtensible Stylesheet Language.
The World Wide Web Consortium (W3C) started to develop XSL because there was a need for an XML-based Stylesheet Language.

  • XSL-FO – a language for formatting XML documents (discontinued in

    2013)

  • XQuery – a language for querying XML documents

It is used to transform an XML document into another XML document, or another type of document that is recognized by a browser, like HTML and XHTML. Normally XSLT does this by transforming each XML element into an (X)HTML element.

With XSLT you can add/remove elements and attributes to or from the output file. You can also rearrange and sort elements, perform tests and make decisions about which elements to hide and display, and a lot more.

A common way to describe the transformation process is to say that XSLT transforms an XML source-tree into an XML result-tree.

How Does it Work?

  • XSLT Uses XPath
  • XSLT uses XPath to find information in an XML document. XPath is used to navigate through elements and attributes in XML documents.
  • In the transformation process, XSLT uses XPath to define parts of the source document that should match one or more predefined templates. When a match is found, XSLT will transform the matching part of the source document into the result document.

XSLT Browser Support

All major browsers support XSLT and XPath.

XSLT is a W3C Recommendation

XSLT became a W3C Recommendation 16. November 1999.

 

Correct Style Sheet Declaration

The root element that declares the document to be an XSL style sheet is <xsl:stylesheet> or <xsl:transform>.

Note: <xsl:stylesheet> and <xsl:transform> are completely synonymous and either can be used!

The correct way to declare an XSL style sheet according to the W3C XSLT.

Recommendation is:

<xsl:stylesheet version=”1.0″

xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”>

or:

<xsl:transform version=”1.0″

xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”>

To get access to the XSLT elements, attributes and features we must declare the XSLT namespace at the top of the document.

The xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” points to the official W3C XSLT namespace. If you use this namespace, you must also include the attribute version=”1.0″.

Start with a Raw XML Document
We want to transform the following XML document (“cdcatalog.xml”) into XHTML:

<?xml version=”1.0″ encoding=”UTF-8″?> <catalog>

<cd>
<title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year>

</cd> .

.

</catalog>

Viewing XML Files in IE, Chrome, Firefox, Safari, and Opera:

Open the XML file (click on the link below) – The XML document will be displayed with color-coded root and child elements (except in Safari). Often, there is a plus (+) or minus sign (-) to the left of the elements that can be clicked to expand or collapse the element structure.

Create an XSL Style Sheet

Then you create an XSL Style Sheet (“cdcatalog.xsl”) with a transformation template:

<?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>My CD Collection</h2>

<table border=”1″>

<tr bgcolor=”#9acd32″>

<th>Title</th>

<th>Artist</th>

</tr>
<xsl:for-each select=”catalog/cd”>

<tr>

<td><xsl:value-of select=”title”/></td>

<td><xsl:value-of select=”artist”/></td>

</tr>
</xsl:for-each>

</table>

</body>

</html>

</xsl:template>

</xsl:stylesheet>

Link the XSL Style Sheet to the XML Document

Add the XSL style sheet reference to your XML document (“cdcatalog.xml”):

<?xml version=”1.0″ encoding=”UTF-8″?>

<?xml-stylesheet type=”text/xsl” href=”cdcatalog.xsl”?>

<catalog>

<cd>
<title>Empire Burlesque</title>

<artist>Bob Dylan</artist>

<country>USA</country>

<company>Columbia</company>

<price>10.90</price>

<year>1985</year>

</cd>

.

.

</catalog>

If you have an XSLT compliant browser it will nicely transform your XML into XHTML.

An XSL style sheet consists of one or more sets of rules that are called templates.

A template contains rules to apply when a specified node is matched.

The <xsl:template> Element

The <xsl:template> element is used to build templates.

<xsl:template match=”/”>

<html>
<body>
<h2>My CD Collection</h2>

<table border=”1″>
<tr bgcolor=”#9acd32″>

 

<th>Title</th>

<th>Artist</th>

</tr>
<xsl:for-each select=”catalog/cd”>

<tr>

<td><xsl:value-of select=”title”/></td>

<td><xsl:value-of select=”artist”/></td>

</tr>

</xsl:for-each>

</table>

</body>

</html>

</xsl:template>

</xsl:stylesheet>

The match attribute is used to associate a template with an XML element. The match attribute can also be used to define a template for the entire XML document. The value of the match attribute is an XPath expression (i.e. match=”/” defines the whole document).

Since an XSL style sheet is an XML document, it always begins with the XML declaration: <?xml version=”1.0″ encoding=”UTF-8″?>.

The next element, <xsl:stylesheet>, defines that this document is an XSLT style sheet document (along with the version number and XSLT namespace attributes).

The <xsl:template> element defines a template. The match=”/” attribute associates the template with the root of the XML source document.

The content inside the <xsl:template> element defines some HTML to write to the output.

The last two lines define the end of the template and the end of the style sheet.

The result from this example was a little disappointing because no data was copied from the XML document to the output.