YOUR FEEDBACK
Robert Z. Cashman wrote: I'll be the first one to cry foul once someone does something wrong with the pat...
Cloud Computing Conference
March 22-24, 2009, New York
Register Today and SAVE !..


2008 East
DIAMOND SPONSOR:
Data Direct
Frontiers in Data Access: The Coming Wave in Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
Intel
Virtualization – Path to Predictive Enterprise
Green Hills
IT Security in a Hostile World
JBoss / freedom oss
Practical SOA Approach
GOLD SPONSORS:
Software AG
The Art & Science of SOA: How Governance Enables Adoption
PlateSpin
Effective Planning for Virtual Infrastructure Growth
Fujitsu
Automated Business Process Discovery & Virtualization Service
Ceedo
Workspace Virtualization
Click For 2007 West
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
TODAY'S TOP SOA & WEBSERVICES LINKS


Incorporating Standards
Incorporating Standards

XML is just a tool, not a solution. Solutions come only when two or more entities agree to use the same format to share data.

The advent of XML has given birth to a number of groups that are developing these standards. But like any committee-based decision, it takes time to agree on what exactly those standards should be. You can't always wait for the standard to be completed. You need to exchange data with your clients/suppliers/partners today, but you don't want to do the work all over again when the standard becomes available. In some cases you've had a method for sharing data that's worked for quite a while and it's not feasible or reasonable to abandon that work. XSL Translation makes it possible to leverage your early work against any related XML document.

Let's assume you're already transferring data between your system and another using a format you designed (see Listing 1). As long as you get data in this format, your system is ready to import and process it. Along comes the long-awaited standard (see Listing 2) and, of course, even if your format is XML-based, it's entirely different and your partners are anxious to start using it. Or you're the one implementing the standard and your partners still need the data in the old format.

There's a way around rewriting your present methods of importing and/or exporting data. XSLT (eXtensible Stylesheet Language Translation) uses the power and even the format of XML to convert documents from any XML format to another structured format, XML or not. XSLT consists of an XML namespace that defines a set of elements, attributes, and other functions, including conditional and looping controls that make it a functional, if somewhat limited, language. XSLT engines process the XSLT document using an XML document as the input and deliver the data from the original document in the desired format. Most current XSLT is used to convert XML documents to HTML for Web site displays or to other XML documents; however, it's just as useful for generating delimited, fixed-length, or any structured format. Figure 1 shows how the XML and XSLT are processed to generate the target document.

Getting to the Data
The first task in creating a translation document is referencing specific elements and attributes of the source document. This is accomplished using yet another W3C Recommendation: XPath. XPath defines a syntax that returns elements, attributes, or text that meet specific location, value, or other criteria. A basic knowledge of XPath is required before creating any XSLT document, so here are the essentials.

If you're familiar with directory and file paths, XPath will be simple. Like most file systems, XPath references can be absolute (starting at the top of the document with a /) or relative (starting at the current point in the document). In addition, like most multiple file systems, a period or dot ( . ) refers to the current element and two periods or dots (..) refer to the parent of the current element.
In Listing 2:

/orders/order/customer/city
refers to the <city> element and if the <customer> element is current, then
./city
refers to the same <city> element and
../deliverymethod
refers to the <deliverymethod> element. Wild cards are also allowed, so that
/orders/order/customer/*
refers to the <name>, <address>, <city>, <state>, and <zip> elements. Collections or arrays can be referred to using an index. Whether the indexing is 0- or 1-based is dependant upon the XSLT engine, make sure you check with the vendor of the one you're using.

/orders/order/orderline[2]/price
refers to the <price> element in the second orderline with the $19.99 value. Attributes are referenced using an at sign (@).
/orders/order/orderline[1]/_ item@itemid
refers to the itemid attribute on the <item> element of the first <orderline> element with a value of "43-3398".

If your translation engine supports them, there are several functions that can be used in XPath to further refine the selection or output. For example:

/orders/order/orderline[last()]
refers to the last, in this case the second, order line.
starts-with(
orders/order/orderline/_
item@itemid,
'53'-)
refers to all items where the itemid begins 53-, in this case, the item in the second orderline.
substring-before(
orders/order/customer/name,
'')
refers to the text of the customer name before the first space, in this case, "Common."

Too Much, Too Little, Too Late
It's extremely unlikely that the order and content of the elements and attri- butes in the source document will match those of the target format. If your target format is XML, it's even more unlikely that the element names will match. If you're lucky enough that they do and your target format is XML, you'll make heavy use of xsl:copy, which duplicates an element, its attributes, and children. However, in most cases you'll use xsl:value-of to output the data (and use xsl:element and xsl:attribute if your target document is XML). You may also use xsl:text to generate nondata portions of the target document.

You can change the order within an instance of the data by simply placing the xsl:value elements in the order you want. Lines 66-70 in Listing 3 produce Mytown|KS|55593. For any element in the target document the source document will have the right amount of data, too much, or too little. XSLT provides constructs to handle nearly any conversion. The right amount of data is simple to convert. A simple xsl:value-of will handle it. Line 8 translates to Common Corporation.

Too much data can usually be handled by consolidating two or more elements from the source document using multiple xsl:value-of elements. This may require xsl:text to insert spaces or other separators. Lines 31-34 return [43-3398] 3/4 Inch Widget for the first orderline element in Listing 2.

Too little data has to be handled by using default values or determining the target values based on some portion or combination of the source document's values. This will probably create heavy use of the conditional and branching elements: xsl:if, xls:choose, xsl:when, and xsl:otherwise. The absence of a referenced element or attribute when using xsl:value-of doesn't cause an error. It may, however, cause other problems in the output. You can check for the existence of elements and attributes and handle them accordingly using xsl:if. Lines 38-43 place an N/A in the color spot when no color attribute is available. And Lines 14-25 determine the method of shipping (e.g., USMail or FedEx). Unlike a case statement in most languages, the test for each xsl:when in an xsl:choose can check completely different conditions on completely different items.

Since most data documents contain multiple instances of data or records, most translations require some kind of looping structure. XSL uses xsl:for-each to handle this. Looping can be further enhanced to include ordering using xsl:sort. This allows for further rearranging of the source document. Listing 3, lines 27-28, output the orderlines in order of the itemid.

If there are repetitive data structures (order, delivery, and billing addresses) in your documents, you'll be able to make use of the xsl:template functionality. It allows you to define the translation once and use it throughout the rest of your stylesheet by referencing it using xsl:apply-templates. Lines 54-72 show a template that formats the address of a customer into two address strings by concatenating everything past the first address element, separated by commas. It then adds a city, a state, and a zip code string.

Listing 3 is the complete XSLT document that converts the sample source document (see Listing 2) to the sample target document (see Listing 1). Since an XSL stylesheet is an XML document, run your XSLT documents through a parser to check that they're well formed and, if possible, for validity (a DTD fragment for XSLT is available from the W3C Web site). The details of applying a stylesheet to a specific XML document are particular to the translation engine you're using. Usually they consist of loading both the XSLT and the source documents and using a transform method that results in either a text string or a DOM object. As with any coding project, you'll need to test your stylesheet against all the possible data patterns to ensure that no errors are caused in processing or output.

New Version, Same Process
Once you have the XML standard document converted to your format, it can be processed as normal. Of course, then a new version of the standard will be released. It may be advantageous to continue to support the original standard until such time as the new version is prevalent. You can modify your XSLT stylesheet to fit the new standard or create an XSLT stylesheet to convert one standard document to the other. There are XSLT elements that are used to create well-formed XML output; most of them are self-explanatory. Listing 4 uses these functions to create the following:

<ordln> <itm itmid="43-3398" qty="1" prc="$3.59">3/4 Inch Widget</itm> </ordln>
These elements can obviously be used if your original data format is XMLbased as well.

Issues to Watch
As with other aspects of XML, whitespace is not handled consistently between, and sometimes within, translation engines. For the most part, if you don't want whitespace in your output, don't put it in your XSLT stylesheet.

The implementation record of HTML standards for browsers has been continued with translation engines as well. Some early versions implement few, if any, of the XPath functions and just the bare essentials of the XSLT elements and attributes. And the error messages generated when you use a feature not in your release are, as usual, less than explicit. Your best bet is to make sure you have the most up-to-date version and that the namespace you reference in your XSLT document corresponds to the requirements of the engine you're using. An incorrect namespace can cause incorrect output or, in some cases, no output at all.

When the W3C approves the schema specification later this year, standard formats for common data transfers should become more frequent. Using XSLT, you can leverage your existing integration solutions while working on your own XML-based methods. As those standards evolve, XSLT can help you manage the support of older versions using a single import method.

XML JOURNAL LATEST STORIES . . .
A round-up of the many themes and topics of interest to infrastructure architects, developers and IT managers featuring at SYS-CON's Cloud Computing Expo being held November 19-21, 2008 at The Fairmont Hotel in San Jose, California. The conference is expecting a record turnout of senio...
SYS-CON Events announced today that the leading global SOA, Virtualization, Cloud Computing and Open Source technology provider FreedomOSS named "Gold Sponsor" of SYS-CON's SOA World Conference & Expo which will take place November 19-21, 2008, at the Fairmont Hotel in the heart of Sil...
Cloud Computing offers significant benefits over traditional solutions for deploying production systems as well as for conducting development and testing activities. This session will distill the unique characteristics of clouds and describe how to best think about deployments in the c...
Intel has just released Intel XML Software Suite 1.2. This latest release helps maximize XML performance, while minimizing the effort for any Enterprise, SOA, SaaS, and Web 2.0 based applications. Intel XML Software Suite 1.2 optimizes XML application performance, takes full advantage ...
SYS-CON Events announced today that the leading global SOA, Virtualization, Cloud Computing and Open Source technology provider Intel named "Gold Sponsor" of SYS-CON's SOA World Conference & Expo which will take place November 19-21, 2008, at the Fairmont Hotel in the heart of Silicon ...
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021


SYS-CON FEATURED WHITEPAPERS


ADS BY GOOGLE