Processing an array using an XML map in WebSphere Integration Developer V7

Dividing array elements into smaller arrays

Learn a technique to sort loan applications into two lists and handle arrays of business objects using the XML map programming paradigm in WebSphere® Integration Developer.

Paul J. Gooderham (paulg@ca.ibm.com), Level 2 Support Engineer, IBM

Author photo of Paul GooderhamPaul Gooderham has been a member of the the Level 2 Support team for two years at IBM. Prior to that, he was part of the WebSphere Integration Developer development team.



25 May 2011

Also available in Chinese

Introduction

This short article examines the business need to sort loan applications into two lists, and thereby demonstrates a technique to handle arrays of business objects using XML maps. The example and the sample code accompanying this article will help you create business solutions using WebSphere Integration Developer (hereafter called Integration Developer).

Banking application scenario

This article examines a simplified banking application to demonstrate a technique that you can apply to your code. In this case, we are designing a web service that receives a list of customers who are applying for a loan. The service divides the applications into two piles and returns them to the caller so that they can be processed separately. You can use this technique when you need to separate an input array into two or more smaller arrays based on criteria in the input data.

Some applications have been approved automatically and some need further investigation. This determination has already been made and is reflected in the input data in a field called "preapproved", which can be true or false. You will test this field to decide which array the application belongs to.

The sample data has three customers and is organized as an array. The data is presented in Listing 1.

Listing 1. Input data
<tns:processBatch>
  <masterList>
    <list>
      <customerNumber>CN001</customerNumber>
      <customerName>Roger Smith</customerName>
      <customerAddress>123 Main Street, Streetsville, Maine</customerAddress>
      <preapproved>true</preapproved>
    </list>
    <list>
      <customerNumber>CN002</customerNumber>
      <customerName>Marylin Mason</customerName>
      <customerAddress>17 Stall Gate, Gaithersberg, PA</customerAddress>
      <preapproved>false</preapproved>
    </list>
    <list>
      <customerNumber>CN003</customerNumber>
      <customerName>Sean McGowan</customerName>
      <customerAddress>25 Hope Avenue, Glover, Oregon</customerAddress>
      <preapproved>true</preapproved>
    </list>
  </masterList>
</tns:processBatch>

In this web service, you will use the array processing features of XML maps to do the processing.


XML map overview

One of the programming paradigms offered by Integration Developer is called an XML map. This map allows you to convert one type of message into another type using an easy-to-read graphical interface, as shown in Figure 1. This allows a system using one type of message to communicate with a system using another type. You can also use it do the types of computation that are presented in this article.

Figure 1. XML map editor
XML map editor

On the left side of the screen is the incoming message, and on the right is the outgoing message. Operations called transforms typically take data from the input message and reformat it for use in the output message. Not all input fields need to be used and some output fields may be populated with data from another source.

Typically, data is copied from the source message into the appropriate field in the target message. However, the data can also be modified. For example, you can join two fields together and copy them into one output field. An output field can also receive data from a constant value or from a Java™ method.

XML maps can also handle arrays. When there is an array of fields in the input or output message, certain transforms are available to perform an action on each element of an array, or to merge several input arrays into one output.

In the example presented in this article, one input array will be split into several output arrays based on the criteria specified in the XML map. A "For each" transform will be used to process each input element.

Using the For each transform

Some messages contain a collection of objects organized into an array. For example, there is a list of customers to be processed. Most mapping transforms operate on regular scalar data and so arrays require a special transform.

"For each" is a type of mapping transform that operates on an array input. It is like a container that has two main features. As shown in Figure 2, the first is a nested map that contains the instructions to handle the data in each of the array elements. The second is an iteration process that applies the nested map to each of the elements of the input array. The For each transform also offers other features to control execution, one of which is described below.

The iteration is implied and cannot be manipulated by the programmer. The nested map, however, is blank until the programmer fills it in using any of the mapping transforms.

Figure 2. XML map editor
XML map editor

In this example, we are splitting the input into two arrays so the nested map in the For each transform will simply copy the data into the new array using a move transform. Press the Edit button on the For each transform (the yellow arrow) to display and edit the nested map (Figure 3). You can press the Edit button at the top of the nested map to move it back to the main map.

Figure 3. Nested map
The XML map nested in the for each displayed in the editor

To control which input elements are placed in the output array, we use an input filter. In the editor, this is found in the Filter Inputs tab of the Property page for the transform. The filter is expressed in the XPath language and if you press Ctrl+Space, then a content assist dialog appears to help you compose the filter. The filter used in the first For each transform is $list[ string(preapproved) = 'true' ], as shown in Figure 4.

Figure 4. The first input filter
The first input filter displayed in the properties

Since you are splitting the input array into two output arrays, you create a second For each transform with a different input filter. While the first transform selected preapproved applications, the second will select the others and place them in a second array. The filter used in the second For each transform is $list[ string(preapproved) = 'false' ], as shown in Figure 5. The map is implemented in an efficient language (XSLT) so that processing the list twice does not affect the overall application performance.

Figure 5. The second input filter
The second input filter displayed in the properties

The result of processing the data file presented above appears in Listing 2. The two customers whose applications are marked as "preapproved" appear in the first array called "approved". The other application appears in the list called "unapproved". The program that calls this web service can process these lists further. The approved list will be handled right away, and the unapproved list can be sent for further investigation.

Listing 2. Output data
<io:processBatchResponse>
  <categorizedLists>
    <approved>
      <list xmlns:bons0="http://LoanMediation" ... >
      <customerNumber>CN001</customerNumber>
      <customerName>Roger Smith</customerName>
      <customerAddress>123 Main Street, Streetsville, Maine</customerAddress>
      <preapproved>true</preapproved>
    </list>
      <list xmlns:bons0="http://LoanMediation" ... >
      <customerNumber>CN003</customerNumber>
      <customerName>Sean McGowan</customerName>
      <customerAddress>25 Hope Avenue, Glover, Oregon</customerAddress>
      <preapproved>true</preapproved>
    </list>
    </approved>
    <unapproved>
      <list xmlns:bons0="http://LoanMediation" ... >
      <customerNumber>CN002</customerNumber>
      <customerName>Marylin Mason</customerName>
      <customerAddress>17 Stall Gate, Gaithersberg, PA</customerAddress>
      <preapproved>false</preapproved>
    </list>
    </unapproved>
  </categorizedLists>
</io:processBatchResponse>

You can extend this technique by dividing the input into finer chunks and creating additional For each transforms and by modifying the input filter to select elements, depending on the fields in the data.


Conclusion

This article described a technique to separate an array of objects into several smaller output arrays based on each object's content. While the example is in the form of a standalone web service, the technique using XML maps can also be used in your own mediations and BPEL processes.


Download

DescriptionNameSize
Sample Integration Developer projectLoanMediationPI.zip15KB

Resources

Learn

Discuss

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into Business process management on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Business process management, WebSphere
ArticleID=659985
ArticleTitle=Processing an array using an XML map in WebSphere Integration Developer V7
publish-date=05252011