|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV |
TODAY'S TOP SOA & WEBSERVICES LINKS XML Protocols Incorporating Standards
Incorporating Standards
By: Alan Sproat
Dec. 21, 2000 12:00 AM
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
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. /orders/order/customer/cityrefers to the <city> element and if the <customer> element is current, then ./cityrefers to the same <city> element and ../deliverymethodrefers 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]/pricerefers 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@itemidrefers 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(refers to all items where the itemid begins 53-, in this case, the item in the second orderline. substring-before(refers to the text of the customer name before the first space, in this case, "Common."
Too Much, Too Little, Too Late 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 <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 . . .
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING XML NEWS |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||