A common problem with Web service applications is that the real backend software does not support XML (or at least does not support the standardized XML used by your Web service ). To solve this problem, many architectures have implemented a translation phase to interpret the received XML and convert it into a more appropriate format that can be recognized by the backend system. Although there are many ways to translate XML into different formats, eXtensibleStylesheet Language (XSL) provides a robust, standard, and XML-friendly solution.
Some common problems
There are many possibilities for translation between an XML message and the application that will process it. However, there are also many common problems, including:
Lookup)
Mapping)
Aggregation)
Split)
Formula (Formulas)
Reordering)
The query process accepts the received value and maps it to a different value for the target system. For example, your XML value is 309, but the value required by the application is "Uber Widget ".
In essence, ing re-allocates values from one field to another. For example, in XML, you may have an AccountNumber element, which needs to be reassigned to a new element called mermeraccountnumber.
Aggregate Processing combines two or more project teams from XML into a single project for the backend system. A common example is to combine the surname and Name field groups into a single name (field ). Segmentation is an aggregate inverse operation, which divides a value of XML into two or more separate components.
Formula processing generally involves the calculation of one or more XML values to obtain a value for the new application. One example is to use the sub-orders in XML to calculate the total number of orders.
Finally, reordering is to change the processing of the Project sequence or structure in XML so that they can meet the sequence or structure expected by the target system.
Ing example
Now let's take a closer look at this process with an example. Let's assume that the received XML is the same as that in Listing 1:
Listing 1: webserviceorder. xml
<? Xml version = "1.0"?>
<Order>
<OrderNumber> 8100 </OrderNumber>
<AccountNumber> 99213 </AccountNumber>
<Item>
<SKUs> 2388 </SKU>
<Description> Uber Widget </Description>
<Quantity> 15 </Quantity>
<PricePer> 10.95 </PricePer>
</Item>
<Item>
<SKUs> 6273 </SKU>
<Description> Flangeoid </Description>
<Quantity> 10 </Quantity>
<PricePer> 52.00 </PricePer>
</Item>
</Order>
Now, our order system needs a slightly different format. What we need to do is to convert the order 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>
<SKUs> 2388 </SKU>
<CustomerPrice> 10.95 </CustomerPrice>
<Quantity> 15 </Quantity>
<Subtotal> 164.25 </Subtotal>
</OrderItem>
<OrderItem>
<SKUs> 6273 </SKU>
<CustomerPrice> 52.00 </CustomerPrice>
<Quantity> 10 </Quantity>
<Subtotal> 520 </Subtotal>
</OrderItem>
</OrderItems>
</NewOrder>
Translation
As this is a simplified example, we only need to do a few things on the XSL template when converting the format. The first thing we should note is that we must map some elements:
Map Order to NewOrder
Map AccountNumber to CustomerAccountNumber
Map OrderNumber to mermerordernumber
Map an Item to an OrderItem
Map PricePer to CustomerPrice
Then, we need to reschedule the OrderItem element to 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 product.
Ing translation is the simplest, because you only need to define a new element in the template and specify that the new element has the value of the element from the received XML document. The re-sorting of Items is implemented by calling the Item sub-template into a new element called OrderItems. Finally, a simple XPath expression is used for calculation. Listing 3 shows the XSL document used to convert the received XML into XML for the application.
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 is the deputy director of Fujitsu consulting. He provides architecture, design, and development support for Fujitsu's technical consulting companies.