November 22, 2019

Transforming XML data with XSL

 

Data that has been structured with XML can be transformed by applying programming instructions using XSL (eXtensible Stylesheet Language). XSL, a family of languages (XSLT, XPath, XSL FO), is itself written using XML syntax. An XML document has a tree-like structure for its data. This is referred to as a source tree. XSL transforms the source tree into another tree, a result tree, before applying formatting instructions for output. I have avoided discussing the use of templates and pattern matching, which XSL uses to implement the creation of a new result tree.

I have created some files for you to use. Remember to store them in the same folder.

You can get an XML file here. (Right click and Save Target As...). This is simply an extension of the markup that you saw on the previous page. It stores the data for a number of books.
You can get an XSL file here. This is the program that will be used to transform the XML data.

Reading these files is easy. Open Notepad, File/Open and select Files of type: All Files before navigating to the book.xml file. Repeat the process for the file book.xsl.

The XSL transformations are a series of instructions that operate on the XML data (source tree). By accessing particular elements of the source tree, a new tree is created that uses these elements in some predetermined manner. Once that new result tree is constructed, the elements can be displayed in a browser with HTML. A demonstration of this will provide another example of the transformation of data into information by use of a programming language.

Using IE6, which supports XSL, open book.xml (File/Open and Browse to select the file). As long as both files were saved in the same folder, you should see that the XSL file has transformed the XML data into this:

Browser output displaying titles and corresponding authors of all the books

 

The XSL instruction/element <xsl:for-each select="CATALOGUE/BOOK"> contains a for-each instruction, which represents a looping construct common to many programming languages. The select="CATALOGUE/BOOK" part of the instruction enables us to access and operate on each of the nested BOOK elements. This harks back to the point about structuring data or documents. The predictability of a data structure, in this case an XML data tree, enables us to write programs for operating on that data.

Once each BOOK element is accessed, each of the values of the four nested elements can be accessed in turn. However, the XSL file instructs the browser to select only two values, that of TITLE and AUTHOR, with the next two lines
<td><xsl:value-of select="TITLE"/></td>
<td><xsl:value-of select="AUTHOR"/></td>
Note that the <td></td> tags are used to construct the cells of a HTML table. The XSL is simply getting the XML data and placing it in HTML tags for output to the browser.

Next page » Filtering XML data

Previous page « Introduction to XML

 

 

 

 

 

 

 

 

 

 

 

 

Up to top of page