by Thomas Erl
 XML Core Technology
divider
 Transforming Data
 with XSLT
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.
 

return to homepage


SOA: Principles
of Service Design

by Thomas Erl

An in-depth guide dedicated to service engineering with a thorough exploration of the design principles that comprise the service-orientation design paradigm (including a comparison with object-orientation).


Service-Oriented Architecture:
Concepts, Technology, and Design

by Thomas Erl

The first "how-to" guide to building SOA, providing coverage of WS-* specifications, .NET and J2EE platforms, and step-by-step processes for service-oriented analysis and design.
Service-Oriented Architecture:
A Field Guide
to Integrating
XML and Web Services

by Thomas Erl

The best-selling guide to service-oriented integration, providing hundreds of integration strategies and over sixty best practices.

For more information about either book, visit: www.soabooks.com

About SOA Systems

SOA Systems Inc. provides strategic SOA consulting services and offers a comprehensive SOA training program.

For more information see:

•  www.soasystems.com

•  www.soatraining.com



   
   
<?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


   
   
Home   Copyright © 1999-2007 SOA Systems Inc.