eXtensible Stylesheet Language/Transformation part (XSL/T)

XSL/T (eXtensible Stylesheet Language/Transformation part) is the single most useful standard alongside XML itself. It allows for transforming one format into another. Beware of confusion, for a lot of technologies have got the name stylesheet in them. This section deals with the transforming stylesheet XSL/T, while the next session deal with the two formatting stylesheets (named XSL/FO and CSS).


XSL/T was designed as a part of XSL, the eXtensible Stylesheet Language. XSL/T adds the Transformation of one XML document into another. XSL itself provides (besides XSL/T's transformation) formatting of XML documents for on-line reading, printing, speech programs, etc. (see the section called Stylesheet languages for visualisation At the moment, XSL/T has gained much support from XML users in contrast to the not yet widely used formatting part of XSL (named XSL/FO).

The difference between transformation and formatting might not be so clear at first. With formatting is meant the way it should look. A paragraph should be indented by 2cm, font size 11pt and a black colour. With transformation is meant the transformation of e.g. an entire document into a table of contents and just a list of all available section abstracts.

Working of XSL/T

A program capable of performing XSL/T processing takes as it's input both an XML file and the stylesheet document mentioned in the XML file by use of an <?xml-stylesheet href="sample.xslt" rel="stylesheet" type="text/xsl"?>-tag. The output can be any format, but normally a valid XML file or HTML is returned. Both the ingoing and the outgoing XML file are internally represented by a tree-like structure. The stylesheet transforms and re-arranges the incoming tree into the outgoing tree.

The stylesheet itself is a valid XML file, all instructions for the XSL/T processor are given by means of XML tags. The stylesheet contains multiple templates. Each template has a rule where it is matched against. The contents of the template are instructions on how to transform the part of the tree which was matched by the template rule. To transform a <emphasis>-tag in the incoming file to HTML's <i>-tag, the following template can be used:

Example A-2. XSL/T - basic example

	    <xsl:template match="emphasis">
	    <xsl:apply-templates />

This piece of code means that if the processor encounters an <emphasis>-tag in the incoming tree, it should follow the instructions within the template. That means putting the <i> opening and closing tag in the output tree and to continue processing within the <i>-tag by applying any additional templates that might be matched further down the tree.

Of course, tags like <xsl:if> and <xsl:while> are also available to make conditional processing possible. Also some support for variables and calculation is available, all geared towards being able to perform all needed document transformations, including constructing a table of contents from a well-structured document and so on.

Example usage: transforming database output

Much information is - and will be - contained in relational databases. In this example I will extract information out of a testdatabase containing information about a few Internet pages I maintain at a local testsite. The database structure is as follows:

Example A-3. XSL/T example - database table structure

	      CREATE TABLE pages (
	      url char(100) DEFAULT '' NOT NULL,
	      title char(200),
	      section char(10),
	      subject char(10),
	      date char(8),
	      printable char(100),
	      PRIMARY KEY (url)

This database is accessed using a JDBC connection. Every database vendor has some tool of sorts to output XML information from it's databases, but in this case I use an XML page processed by Cocoon (part of Apache.org's XML server project, see the section called Cocoon - Apache's xml effort).

Example A-4. XSL/T example - xml file before transformation

	    <connection name="page_connection">

	    <filelist>  <!-- the filelist tag is irrelevant to the example -->
	    <query connection="page_connection">
	      select * from pages order by section,subject,date

When the XML file containing this code fragment is processed by Cocoon's SQL processor it removes above tags and replaces the query tag with the following fragment:

Example A-5. XSL/T example - xml file after processing with cocoon's SQL processor

	    <filelist>  <!-- the filelist tag is irrelevant to the example -->
	    <ROW ID="0">
	    <title>Use of XML with project databases and Java</title>
	    <ROW ID="1">

Every row is contained within <ROW>-tags and the actual data is contained within tags named after the column name in the database. I wanted to convert this information into a normal HTML table. For that purpose I use the following stylesheet (also processed by Cocoon):

Example A-6. XSL/T example - the stylesheet

	    <xsl:template match="filelist">
	    <xsl:for-each select="ROWSET">
	    <xsl:for-each select="ROW">
	    <a href="{url}"><xsl:value-of select="title"/></a>
	    <td><xsl:value-of select="section"/></td>
	    <td><xsl:value-of select="subject"/></td>
	    <td><xsl:value-of select="date"/></td>
	    <xsl:if test="not(printable = '') ">
	    <a href="{printable}">Printable version</a>

Compiling stylesheets

A very recent development allows one to compile an XSL/T stylesheet into native code (using c++) or Java bytecode. The resulting program can receive an XML file as input. The output is the same as if you had processed the XML file with an XSL/T processor and the original stylesheet. Compiling a XSL/T stylesheet into a program has massive speed advantages, because a generic stylesheet processor needs to be able to do everything and the compiled stylesheet only has to be able to use it's own stylesheet.

Possible use for ceXML

There are a few possible roles I see for the usage of XSL/T:

Note should be taken that these are only possible roles, there are many more technologies that can be used. The advantage I see at the moment is that using XSL/T means using a native XML technology instead of using a purpose-written program. Using as much native XML technologies as feasible makes for a nice overall solution which can be adapted and re-used to one's heart's content.