Next page Previous page Start of chapter End of chapter

Extensible Stylesheet Language Transformations

Extensible Stylesheet Language Transformations (XSLT) is an XML application to transform one XML document into a another document in some format (such as XML, HTML, plain text). Since one typical application of XSLT is to render the information contained in the XML document by mapping the XML document into an HTML one, XSLT documents is also called XSLT stylesheets.

An XSLT document is a well-formed XML document that contains template rules. Each template rule has a pattern and a template. An XSLT processor is a software that applies a given XSLT stylesheet to a given XML document and builds the output document. An XSLT processor works as follows. It visits the XML document tree using a preorder traversal and compares each encountered tree node to the patterns of the template rules in the stylesheet. If a match is found, the processor writes the template of the matched rule into the output document. When all nodes have been visited, the output document is returned.

Here is an example. Consider the following XML document containing an excerpt of the DBLP bibliography:

<?xml version="1.0"?>
<!DOCTYPE dblp SYSTEM "dblp.dtd">

<!--A short bibliography of Massimo Franceschet-->
<dblp>
   <inproceedings key="M4M05"> <!--A conference paper-->
      <author>M. Franceschet</author>
      <author>E. Zimuel</author>
      <title>Modal logic and navigational XPath: an experimental comparison</title>
      <booktitle>Proceedings of the Workshop Methods for Modalities</booktitle>
      <pages>156-172</pages>
      <year>2005</year>
      <url>http://www.sci.unich.it/~francesc/pubs/m4m05.pdf</url>
   </inproceedings>
   <article key="JLLI05"> <!--A journal paper-->
      <author>M. Franceschet</author>
      <author>B. ten Cate</author>
      <title>Guarded fragments with constants</title>
      <journal>Journal of Logic, Language and Information</journal>
      <volume>14</volume>
      <number>3</number>
      <pages>281-288</pages>
      <year>2005</year>
      <url>http://www.sci.unich.it/~francesc/pubs/jlli05.pdf</url>
   </article>
</dblp>

As a first example, we use the simplest XSLT document that you can write:

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

Besides the XML declaration, it contains a unique element called xsl:stylesheet. This is the root element of any XSLT document. It has a synonym, xsl:transform, that means exactly the same. The xsl:stylesheet element has an attribute version, whose value can be 1.0 or 2.0, and an attribute xmlns:xsl declaring the xsl prefix for the http://www.w3.org/1999/XSL/Transform namespace (the XSLT namespace). There are no template rules in the above stylesheet, hence, when applied to the above XML document, the XSLT processor falls back on built-in rules. These rules, when applied to our bibliography XML document, have the effect of producing the following output document:

<?xml version="1.0" encoding="UTF-8"?>

      M. Franceschet
      E. Zimuel
      Modal logic and navigational XPath: an experimental comparison
      Proceedings of the Workshop Methods for Modalities
      156-172
      2005
      http://www.sci.unich.it/~francesc/pubs/m4m05.pdf


      M. Franceschet
      B. ten Cate
      Guarded fragments with constants
      Journal of Logic, Language and Information
      14
      3
      281-288
      2005
      http://www.sci.unich.it/~francesc/pubs/jlli05.pdf

First of all, notice that the output document is not an XML document. It is some text taken from the input XML document prefixed by an XML declaration. The build-it rules specify the following:

The XSLT processor reads the XML document from top to bottom (or the XML tree in preorder) and applies the built-in rules or the rules specified by the user. It is worth noting that by default the XSLT processor never visits attribute nodes. Hence attribute values are never copied into the output unless there exists an explicit user-defined rule that forces the processor to visit attribute nodes. On the other hand, all the other nodes (root, element, comment, processing instruction, namespace, and text nodes) are visited, and the corresponding rules are applied.

With reference to the our example, root and element nodes are visited and their content is processed. Text nodes are visited and their values are printed (notice that strings of white spaces are preserved in the output since they correspond to text nodes). Moreover, comment nodes are also visited but their values are not printed. Finally, attribute nodes are not visited hence nothing is printed. Summing-up, the build-it default behavior has the effect to take the element content but not the markup of the input document.

Next page Previous page Start of chapter End of chapter
Caffè XML - Massimo Franceschet