Use XSL to translate Web service applications

Source: Internet
Author: User
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, eXtensibleStylesheetLanguage (XSL) provides a robust, standard, and XML-friendly solution. 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

 
   
  
   8100
    99213  
      
   
    2388
       
   
    Uber Widget
       
   
    15
       
   
    10.95
     
    
      
   
    6273
       
   
    Flangeoid
       
   
    10
       
   
    52.00
     
      
 

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

 
   
  
   99213
    
  
   8100
    
      
         
    
     2388
          
    
     10.95
          
    
     15
          
    
     164.25
        
       
         
    
     6273
          
    
     52.00
          
    
     10
          
    
     520
        
     
  
 


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

 
   
      
     
    
      
         
    
     
          
    
     
          
            
           
        
     
    
      
         
    
     
          
    
     
          
    
     
          
    
     
        
     
      
 

The preceding section describes how to use XSL to translate Web service applications. For more information, see PHP Chinese website (www.php1.cn )!

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.