SoFunction
Updated on 2025-04-08

Translate web service applications with XSL

A common problem with web service applications is that 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 implement a translation phase to interpret the received XML and convert it into a more appropriate format that the backend system can recognize. While 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 common questions

Between XML messages and the application that will handle it, there are many possible translations. However, there are many common problems, including:

Lookup
Mapping
Aggregation
Splitting
Formulas
Reordering
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 required by the application is "Uber Widget".

Mapping is essentially reassigning values ​​from one field to another. For example, in XML, you might have an AccountNumber element that needs to be reassigned to a new element called CustomerAccountNumber.

Aggregation is the combination of two or more projects from XML into a single project for a back-end system. A common example is combining the last and first fields into a single name (field). Splitting is an inverse operation of aggregates, which divides an XML value into two or more separate components.

Formula processing generally involves the calculation of one or more XML values ​​to obtain a value for a new application. An example is to use sub-orders in XML to calculate the total number of orders.

Finally, reordering is the process of changing the sequence or structure of the items in the XML so that they can meet the desired sequence or structure of the target system.

Mapping examples
Now let's take an example to take a closer look at the process. Let's assume the XML received is like in Listing 1:

Listing 1:

<?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 requires 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:

<?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>
 

translate

Since this is a simplified example, there are only a few things we need to do with XSL templates when converting formats. The first thing we need to pay attention to is: we have to map some elements:

Map the Order into NewOrder
Map AccountNumber into CustomerAccountNumber
Map OrderNumber into CustomerOrderNumber
Map Item into OrderItem
Map PricePer into CustomerPrice
Then we need to re-arrange the OrderItem element under a new element called OrderItems. Finally, we add a new element called Subtotal, which will be calculated based on the unit price and quantity of (commodity).

Mapping translation is the easiest thing, because you simply need to define a new element in the template and indicate that this new element has the value from the element in the received XML document. Reordering Items is achieved by putting the Item sub-template call into a new element called OrderItems. Finally, use a simple XPath expression to perform the calculation. Listing 3 shows the XSL document used to convert the received XML into XML for the application.

Listing 3:

<?xml version="1.0"  ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http:///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. He provides architecture, design and development support to Fujitsu's technology consulting firms.