Transforming Data with XSLT
by Thomas Erl

Within just about any application design, there will be a requirement for the format of data to be altered between the time it is first retrieved, to when it reaches its final destination. Because XML provides a clear separation of content, structure and presentation, the output format of an XML document can be completely "transformed".

Unlike with XML schemas, where you have a series of languages to choose from and combine, XML transformation relies on one core standard: XSLT. There aren't many alternatives, but there are some supplementary technologies that can be used to extend and optimize this part of an architecture.

XSLT performs the following two primary transformation tasks:

1. The conversion of one XML document type into another.

2. The formatting of an XML document into human-readable output, by conversion to a page-description vocabulary.

For instance, an XSLT style sheet can be used to transform an XML document into an XSL-FO rendition that will ultimately make it suitable for published output, such as a PDF document. Alternatively, XSLT can be used to convert one XML document into another based on a different XML language, such as WML (Wireless Markup Language).

Put simply, XSLT allows for the efficient conversion of XML documents into a number of different output formats. The XSLT feature-set facilitates the manipulation, ordering, and filtering of XML document data to provide alternative views and renditions of information for any number of document transformation scenarios.

To demonstrate the use of a simple XSLT template, we'll transform the XML document below into an HTML table.

<?xml version="1.0"?>
<inventory>
   <book category="Fiction">
     <title>Joy of Integration</title>
     <author>Joe Smith</author>
   </book>
   <book category="Non-Fiction">
     <title>Integration for Dummies</title>
     <author>John Doe</author>
   </book>
</inventory>

To the above document we need to add a reference to the XSLT template we'll be building.

<?xml-stylesheet type="text/xsl" href="book.xsl" ?>

Note that XML documents do not need to have this statement embedded - the linking of an XML document and an XSLT template can be accomplished dynamically at runtime.

The XSLT template begins with the following header statements that simply identifies it as an XML document conforming to the W3C XSLT specification.

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

The following construct establishes the base node of the XML document, to which the template applies:

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

The match="/" fragment identifies the root node, which tells the XSLT processor that this template applies to the entire XML document (as opposed to a subset of the document tree).

The construct below uses XPath expressions to locate and loop through each instance of the book element. Every iteration through the loop results in the insertion of a table row consisting of category, title and author column values.

<xsl:template match="inventory">
   <table border="1">
   <xsl:for-each select="book">
     <tr>
       <td><xsl:value-of select="@category"/></td>
       <td><xsl:value-of select="title"/></td>
       <td><xsl:value-of select="author"/></td>
     </tr>
   </xsl:for-each>
   </table>
</xsl:template>

The XSLT syntax is interspersed with HTML tags responsible for building the table. Note the use of the @ symbol, which identifies a value as being an attribute, as opposed to an element.

The XSLT template is completed with the following closing element:

</xsl:transform>

Here is the template in its entirety:

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

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

   <xsl:template match="inventory">
     <table border="1">
     <xsl:for-each select="book">
       <tr>
         <td><xsl:value-of select="@category"/></td>
         <td><xsl:value-of select="title"/></td>
         <td><xsl:value-of select="author"/></td>
       </tr>
     </xsl:for-each>
     </table>
   </xsl:template>

</xsl:transform>

Upon opening the XML document, the sample template will generate HTML formatted output that a browser should render as follows:

FictionJoy of IntegrationJoe Smith
Non-FictionIntegration for DummiesJohn Doe
SOA Design Patterns by Thomas Erl
Foreword by Grady Booch
With contributions from David Chappell, Jason Hogg, Anish Karmarkar, Mark Little, David Orchard, Satadru Roy, Thomas Rischbeck, Arnaud Simon, Clemens Utschig, Dennis Wisnosky, and others.
Web Service Contract Design & Versioning for SOA by Thomas Erl, Anish Karmarkar, Priscilla Walmsley, Hugo Haas, Umit Yalcinalp, Canyang Kevin Liu, David Orchard, Andre Tost, James Pasley
SOA Principles of Service Design by Thomas Erl
Service-Oriented Architecture: A Field Guide to Integrating XML and Web Services by Thomas Erl
Service-Oriented Infrastructure:On-Premise and in the Cloud by Raj Balasubramanian, Benjamin Carlyle, Thomas Erl, Cesare Pautasso
Next Generation SOA:A Real-World Guide to Modern Service-Oriented Computing by Pethuru Cheliah, Thomas Erl, Clive Gee, Robert Laird, Berthold Maier, Hajo Normann, Leo Shuster, Bernd Trops, Clemens Utschig, Torsten Winterberg
SOA with .NET & Windows Azure: Realizing Service-Orientation with the Microsoft Platform by David Chou, John deVadoss, Thomas Erl, Nitin Gandhi, Hanu Kommalapati, Brian Loesgen, Christoph Schittko, Herbjorn Wilhelmsen, Mickey Williams
SOA Governance:
Governing Shared Services On-Premise & in the Cloud
by Stephen Bennett, Thomas Erl, Clive Gee, Anne Thomas Manes, Robert Schneider, Leo Shuster, Andre Tost, Chris Venable
SOA with Java by Raj Balasubramanian, David Chou, Thomas Erl, Thomas Plunkett, Satadru Roy, Philip Thomas, Andre Tost
Modern SOA Methodology: Methods for Applying Service-Orientation On-Premise & in the Cloud by Raj Balasubramanian, David Chou, Thomas Erl, Thomas Plunkett, Satadru Roy, Philip Thomas, Andre Tost
Cloud Computing: Concepts, Technology & Architecture by Thomas Erl, Zaigham Mahmood, Ricardo Puttini
Cloud Computing Design Patterns by Thomas Erl, Amin Naserpour

For more information about these books, visit: www.servicetechbooks.com


Arcitura Education Inc.
Arcitura Education Inc. is a leading global provider of progressive, vendor-neutral training and certification programs, providing industry-recognized certification programs for a range of certifications.
For more information:
www.arcitura.com
SOA Certified Professional (SOACP)
The books in this series are part of the official curriculum for the SOA Certified Professional program.
For more information:
www.soaschool.com
Cloud Certified Professional (CCP)
The books in this series are part of the official curriculum for the Cloud Certified Professional program.
For more information:
www.cloudschool.com
Big Data Science Certified Professional (BDSCP)
The books in this series are part of the official curriculum for the Big Data Science Certified Professional program.
For more information:
www.bigdatascienceschool.com/