A common problem with Web service applications is that real back-end software does not support XML (or at least not the standardized XML that your Web service uses). To solve this problem, many architectures implement a translation phase that interprets the received XML and translates it into a more appropriate format that the backend system can identify. Although there are many ways to translate XML into different formats, the Extensible Stylesheet Language (Extensiblestylesheet language,xsl) provides a robust, standard, and XML-friendly solution.
Some of the common problems
There are many possible places to translate between an XML message and the application that will process it. However, there are a number of common problems, including:
Query (Lookup)
Mappings (Mapping)
Aggregation (Aggregation)
Split (splitting)
Formula (formulas)
Reordering (reordering)
The query processing accepts the received value and maps it to a different value for the target system. For example, your XML has a value of 309, but the value the application needs is "Uber Widget."
Mapping is essentially reassigning the value from one field to another. For example, in XML, you might have a accountnumber element that needs to be reassigned to a new element called Customeraccountnumber.
Aggregation processing is the synthesis of two or more project groups from XML to a single project for the backend system. A common example is to combine the last and first name fields into a single name (field). Segmentation is the reverse operation of aggregation, which divides one value of XML into two or more separate components.
Formula processing typically involves the calculation of one or more XML values to obtain a value for the new application. An example is the use of XML lining orders to calculate the total amount of orders.
Finally, reordering is the process of changing the sequence or structure of projects in the XML so that they conform to the desired sequence or structure of the target system.
Mapping examples
Now let's take a closer look at this process with an example. Let's assume that the XML you receive is the same as in Listing 1:
Listing 1:webserviceorder.xml
<?xml version= "1.0"?>
<Order>
<OrderNumber>8100</OrderNumber>
<AccountNumber>99213</AccountNumber>
<Item>
<SKU>2388</SKU>
<description>uber widget</description>
<Quantity>15</Quantity>
<PricePer>10.95</PricePer>
</Item>
<Item>
<SKU>6273</SKU>
<Description>Flangeoid</Description>
<Quantity>10</Quantity>
<PricePer>52.00</PricePer>
</Item>
</Order>
Now, our order system needs a slightly different format. All we need to do is convert the orders received from the Web service into this format in Listing 2.
Listing 2:applicationorder.xml
<?xml version= "1.0" encoding= "UTF-8"
<neworder>
<customeraccountnumber>99213 </customeraccountnumber>
<customerordernumber>8100</customerordernumber>
<orderitems>
<orderitem>
<SKU>2388< /sku>
<customerprice>10.95</customerprice>
<quantity>15</quantity>
<subtotal>164.25 </subtotal>
</orderitem>
<orderitem>
<sku>6273</sku>
<CustomerPrice> 52.00</customerprice>
<quantity>10</quantity>
<subtotal>520</subtotal>
</orderitem>
</orderItems>
</NewOrder>
Translation
Since this is a simplified example, we need only a few things to do with the XSL template when converting the format. The first thing we should be aware of is that we have to map some elements:
Map order to Neworder
Mapping the AccountNumber into Customeraccountnumber
Mapping the OrderNumber into Customerordernumber
Map Item to OrderItem
Mapping the Priceper into Customerprice
Then we need to rearrange the orderitem elements under a new element called OrderItems. Finally, we add a new element called subtotal, which is calculated based on the unit price and quantity of the (commodity).
Mapping translation is the simplest because you simply define the new element in the template and indicate that the new element has the value of the element from the received XML document. The reordering of items is implemented by putting item child template calls into a new element called OrderItems. Finally, a simple XPath expression is used to compute. Listing 3 shows the XSL document used to convert the received XML into an application-specific XML.
Listing 3:translate.xsl
<?xml version= "1.0"?>
<xsl:stylesheet version= "1.0" xmlns:xsl= "Http://www.w3.org/1999/XSL/Transform" >
<xsl:template match= "/" >
<xsl:apply-templates select= "Order"/>
</xsl:template>
<xsl:template match= "order"
<neworder>
<customeraccountnumber><xsl:value-of select= "AccountNumber"/></customeraccountnumber>
<customerordernumber><xsl:value-of select= "OrderNumber"/></ Customerordernumber>
<orderitems>
<xsl:apply-templates select= "Item"/>
</orderitems>
</neworder>
</xsl:template>
<xsl:template match= "Item" >
<OrderItem>
<sku><xsl:value-of select= "SKU"/></sku>
<customerprice><xsl:value-of select= "Priceper"/></customerprice>
<quantity><xsl:value-of select= "Quantity"/></quantity>
<subtotal><xsl:value-of select= "Priceper * Quantity"/></subtotal>
</OrderItem>
</xsl:template>
</xsl:stylesheet>
Brian Schaffner, the author of this article, is the Deputy director of Fujitsu consulting firm. He provides architectural, design and development support to Fujitsu's technical consulting companies.