Translating Web Service applications with XSL

Source: Internet
Author: User
Tags format query version xsl
Web|web Service | program

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>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.