XSL stands for the Extensible Style Language. It is composed of two parts, XSLT (Extensible Style Language Transformations) and XSL-FO (XSL Formatting Objects), and uses rules from other XML Specifications such as XPath and XLink. It is used to manipulate XML data and transform it into formats that can be displayed or read to humans or other machines.

In order to better grasp XSL, it is best to show a simple XML document:


     <?xml version="1.0"?>
     <email>
          <message>
               <to>Anml4ixoye@e2.com</to>
               <from>you@e2.com</from>
               <body>E2 is sooo cool!</body>
          </message>
          <message>
               <to>Anml4ixoye@e2.com</to>
               <from>you@yahoo.com</from>
               <body>Are you going to Counting Crows tonight?</body>
          </message>
     </email>

So now we have an XML document. By itself it is pretty useless. Sure, you can open the document, and get a good grasp of what is going on. However, not everyone likes looking at XML. In addition, you may need for it to be in a specific format for your application. Currently if you wanted the above message to be displayed in Word format, postscript format, or a text file, you would have to manually enter it into those applications, then process them. However, using XSL, you can take the above XML document and transform it easily into all of the above formats and more.


XSLT

These transformations are handled by XSLT, also known as the XSL Transformation Language. In its most basic form, it is designed to facilitate the manipulation of data from one XML document into another XML-Compatible document. Most commonly that is an XHTML (well-formed HTML) document, however it is not limited to that, and can even be used to output the XML document as another XML document with a different structure or such things as spreadsheets or pdf files.

An XSLT Transformation can only occur in an XML processor. Usually the XSLT is contained within an XSL style sheet and is linked to the XML document using a style sheet declaration at the top of the XML.

A XSL style sheet utilizing XSL Transformations would need to begin with:

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

and have each element utilizing XSLT prefixed with "xsl:":

<xsl:template match="/"/>

(Technically speaking the xsl prefix is a namespace and can be whatever you declare the namespace prefix to be in the xmlns (XML Namespace) declaration)

Finally, in order to link the XML document to the style sheet, the following line would need to be added:


<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="myxsl.xsl"?>

When the processor encounters this declaration, it then processes the XML Document against the XSL stylesheet and displays the resulting output. Currently this happens in one of three ways:

  • Client-Side (Browser) - The web server sends both the XML file and the XSL file to the browser. The browser then processes the XSL and displays the output.
    • Advantages: Client-side - no load on the server besides the hits of sending the documents.
    • Disadvantages: Dependent on however the browser decides to process the XSL. Will not work in all browsers.
  • Server-Side - The web server processes the XML file against the XSL style sheet and returns the resulting output to the client
    • Advantages: Since the server is returning the output, the browser does not have to be XML aware. More control over how the XSL transformation happens.
    • Disadvantages: Server-side, so there is an additional processing load on the server.
  • XSL Conversion Tool - The XML is processed against the XSL style sheet and the resulting document is stored on the server. The server sends the output to the client.
    • Advantages: Web Server is not doing the processing, so minimal server load. Faster load times as the transformation is already complete.
    • Disadvantages: May not be dynamic, requires extra steps to get the documents on your server
Note: Before the introduction of XSL style sheets, documents were transformed using either XDR's (Microsoft - deprecated) or CSS. XSL replaces both solutions.

XSL Style Sheets are XML documents, consisting of template rules, used to create a mapping construct that describes the way elements in a document should be selected or manipulated.


XSL-FO

XSL-FO, also known as the XSL Formatting Objects, are a component of XSL used to describe how an XML document should be rendered when the client views it. XSL-FO utilizes XML syntax in order to define the formatting styles.

XSL-FO can be defined in the same style sheet is as used for XSL Transformations above. However the style sheet would need to have an additional XML Namespace declaration as follows:


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

Thereafter any element utilizing the XML-FO elements would need to be prefixed with "fo:"


<fo:list-item/>

Note: XSL-FO received a major boost in October 2002 when Adobe announced XSL-FO Support in Adobe document server


Putting it together

So far we have looked at XSL, XSLT, and XSL-FO. But what can we do with all of this? Well, we already have an existing XML document (the email messages at the top of this node). Let's utilize the power of XSL to transform it.

Step 1) Analysis - Like any good project, we need some analysis here. What do we want to *do* with this document? We'll make it simple and say that we just need to transform the XML above to HTML, but this is the most important step, so don't skimp on it!

Step 2) Setting up the transformation - We now need to do the actual transformations. An example is below with comments.

<!-- Declare XML and style sheet namespacing -->
<?xml version="1.0"?>
<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/TR/WD-xsl
  xmlns:fo="http://www.w3.org/1999/XSL/Format">

<!-- The following says "do this if you find -->
<!-- an XML root node of any type" -->
<xsl:template match="/">
<html>
  <head>
    <title>Your Messages</title>
  </head>
  <body>
    <!-- This applies templates defined below -->
    <xsl:apply-templates
         select="email">
  </body>
</html>
</xsl:template>

<!-- This happens every time an email element -->
<!-- is found under the element that called this -->
<!-- template -->
<xsl:template match="email">
   <h2>Your Messages</h2>
    <xsl:apply-templates
         select="messages">
</xsl:template>

<!-- This happens every time a messages element -->
<!-- is found under the element that called this -->
<!-- template -->
<xsl:template match="messages">
  <table width="200">
    <tr><td>To: <xsl:value-of select="to"></td></tr>
    <tr><td>From: <xsl:value-of select="from"></td></tr>
    <tr><td>Body: <xsl:value-of select="body"></td></tr>
  </table>
</xsl:template>

The above would produce the following output:
To: Anml4ixoye@e2.com
From: you@e2.com
Body: E2 is sooo cool!

To: Anml4ixoye@e2.com
From: you@yahoo.com
Body: Are you going to Counting Crows tonight?

Step 3) Apply the XML-FO Objects - To apply the XML-FO Objects, we would simply add them to our above style sheet. This would modify the messages template to look like:

<xsl:template match="messages">
  <table width="200">
    <tr>
      <td>
        <fo:block font-size="14pt" font-family="Arial">
        To: <xsl:value-of select="to">
        </fo:block>
      </td>
    </tr>
    <tr>
      <td>
        <fo:block font-size="14pt" font-family="Arial">
        From: <xsl:value-of select="from">
        </fo:block>
      </td>
    </tr>
    <tr>
      <td>
        <fo:block font-size="12pt" font-family="Arial">
        Body: <xsl:value-of select="body">
        </fo:block>
      </td>
    </tr>
  </table>
</xsl:template>

The above document barely scratches the surface for everything that can be done with XML, XSL, etc. Be sure to visit the W3C for more information and tutorials.


References:

  • the W3C - http://www.w3.org
  • the W3C Schools - http://www.w3schools.com
  • Propoint - http://www.propoint.org
  • Personal Experience