Chapter 5. Document Transformation

Table of Contents

5.1. XSL Transformations
5.2. Transformation in HTML
5.2.1. Table
5.2.2. Computing New Information
5.2.3. Bulleted Lists
5.3. Transformation into a Compact Textual Form
5.4. Transformation into PDF with XSL-FO
5.4.1. XSL-FO Input to the Renderer
5.4.2. From the Instance Document to the XSL-FO file
5.5. Transformation with a CSS
5.6. Associating an Instance File to a Stylesheet
5.7. Additional Information on XSL

Since XML is a tree-structured representation of information, it should be relatively simple to change its shape or select parts provided we have a way of to identify subtrees and to combine them in new trees. To achieve this, XML designers have defined the eXtensible Stylesheet Language (XSL) technology [28] which refers to two components:

XSLT
a transformation language for converting an XML document into either another XML document, into HTML, or into a plain text document (a very wide one-level tree!).
XSL-FO
a platform- and media-independent formatting language composed of a set of XML elements, called formatting objects, that describe parts of a printed page at a high level, e.g. <block>, <table>, etc. These elements are most often produced by XSLT transformations of an XML document.

XSLT is an XML based formalism for defining production rules (similar to OPS5 or Prolog without unification) that match nodes in a tree of an XML document and produce a new tree. These rules are defined in stylesheets (XML files named with the .xsl extension) that can be validated with a predefined XSLT Schema. This transformation mechanism is very general and can be used to produce any kind of tree, but most often it is used for presentation, one simple kind of tree being an HTML document. In fact, most web browsers can process XML documents linked with XSLT stylesheets to display the resulting transformation. When no stylesheet is associated with the XML file, most browsers now have a predefined stylesheet to explore them gradually by expanding and collapsing elements. XSLTdepends on XPath [17], explained in the previous chapter, a sublanguage designed to identify nodes in an XML document.

Unfortunately, browsers only implement XSLT 1.0 which is limited compared to the XSLT 2.0 recommmadation used in this document. Anyway experience has shown that it is not very reliable to rely on the XSLT processing performed by browser because each one has its peculiarities. Usually the transformation from XML to HTML will be performed on the server. In 2017, the XSLT 3.0 recommandation was approved featuring streaming and modularization, but as these features are not needed in this simple tutorial, we use only XSLT 2.0.

We will see in Section 5.2.1 how to create an HTML tabular presentation of our wine catalog. Section 5.2.2 illustrates features of stylesheets that allow to better select information and perform some simple calculations to produce information that was not present in the original XML file. In Section 5.2.3, we will show how to transform our cellar book instance document into an HTML page with indented bulleted lists. We will then show, in Section 5.3, how to transform our XML instance document into the compact text representation we presented in Figure 1.4. We will illustrate in Section 5.4 the use of Formatting Objects to produce a PDF output from an XML document. Finally, in Section 5.5 we compare the Cascading Style Sheets most often associated with HTML pages with XSLT.

The next chapter will describe how to achieve these transformations using XQuery.

The PDF version of this document and the corresponding set of HTML files on the companion web site were produced by XSLT stylesheets from a set of XML source files. This process is explained in Appendix C.