Accessibility

Dreamweaver Article

 

XSL Overview


Table of Contents

What is XSL and Where Should You Use It?

XSL is for XML what CSS is for HTML. It stands for EXtensible Stylesheet Language. It is a language designed to present XML data in a readable format. XSL actually consists of two parts:

  • XSLT – a language for transforming XML documents
  • XPath – a language for navigating in XML documents

XSLT stands for XSL Transformations and is the most important part of XSL.

XSLT transforms an XML document into another XML document, into XHTML output, or into simple text. This is usually done by transforming each XML element into an HTML element. XSL is required because XML tags are user-defined, therefore browsers do not know how to interpret or render each tag. Their meaning is designed to be understood by humans, not machines.

XSLT can also perform the following operations on an XML tree:

  • add and remove elements
  • add and remove attributes
  • rearrange and sort elements
  • hide or display certain elements
  • find or select specific elements

If you need to review the XML syntax, read the XML Syntax section of my previous article on XML.

XSL Syntax

As you might recall from XML Overview article, all XML documents start with an XML declaration. The same is true for XSL style sheets. The first line of any XSL document is actually an XML declaration:

<?xml version="1.0" encoding="ISO-8859-1"?>

Is XSL the Same as XML?

Yes and no. Yes, because they observe the same syntax rules (with a few differences, which I will talk about next). No, because they have different goals: XML carries data, XSL formats it.

After the XML declaration, there is an XSL declaration, such as:

<xsl:stylesheet>

or

<xsl:transform>

However, in most real-world cases, the XSL declaration looks a bit more complex:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

This is because it also includes the namespace and the version of the XSL specification, according to the recommendations of the W3 Consortium.

Unlike the XML declaration, which is written on a single line and has no closing tag, the XSL declaration must have a closing tag, which marks the end of the XSL stylesheet:

</xsl:stylesheet>

Notice how this does not violate the XML syntax rules: an XSL stylesheet is a perfectly valid XML document, since it has a single root element, specified by the <xsl:stylesheet> tag.

Where Should You Use XSL?

XSL was designed with a few goals in mind, which make it the perfect solution for some development cases, while rendering it totally useless for others.

  • XSL was designed to process XML documents and to observe the XML syntax. Therefore,you should only use it in conjunction with XML in XML-aware applications. The perfect places to use XML and XSL are as follows: web portals, news aggregators, community websites, or any other web application that needs to deliver information to a variety of devices and a large number of clients.
  • XSLT is a language based on pattern matching. It looks for nodes that match a particular condition, then it applies the corresponding rules. As a consequence, it lacks the computing power of most programming languages. For instance, XSL cannot change the value of variables at runtime. It shouldn't be used to compute values from dynamic data sources using complicated formulas (such as in an online store). Web programming languages are better for this goal.
  • XSL was not designed to replace or to complement CSS. It shouldn't (and cannot) be used to style HTML. You can, however, use it for websites that need frequent visual redesigns, that change their layout often, and need to manipulate data in a flexible format.
  • XSL is not a content management tool. It shouldn't (and cannot) be used to change the content of XML documents or to edit information. You can, however, use XML and XSL for a content management system that needs to handle documents in several different formats.