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.
<title>Joy of Integration</title>
<title>Integration for Dummies</title>
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.
The following construct establishes the base node of the XML document, to which the template applies:
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.
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:
Here is the template in its entirety:
Upon opening the XML document, the sample template will generate HTML formatted output that a browser should render as follows:
|Fiction||Joy of Integration||Joe Smith|
|Non-Fiction||Integration for Dummies||John Doe|
- Inside XML Schemas
- SOAP in a Nutshell
- Transforming Data with XSLT
- Understanding DTDs
- Why SAX is Good for DOM
- What You Should Know about XPath
- An XHTML Primer
- XLink - Inside and Out
- Data Access with XQuery
- XSL versus CSS
- Another Introduction to XML
- Unifying Corporate Data & Documents
- Replacing HTML Documents with XML
- Meta-Enable Your Enterprise
- The XML Data Custodian
- Integrating XML into the Enterprise
- The Wireless Enterprise
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.
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
For more information about these books, visit: www.servicetechbooks.com