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
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
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
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
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
Figure 5. The second input filter
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.
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.
|Sample Integration Developer project||LoanMediationPI.zip||15KB|
- XML mapping in WebSphere Integration Developer V7, Part 2: Working with complex XML structures
- WebSphere Integration Developer V7 Information Center
- developerWorks WebSphere Process Server and WebSphere Integration Developer resource page
- IBM Business Process Management zone
Dig deeper into Business process management on developerWorks
Get samples, articles, product docs, and community resources to help build, deploy, and manage your cloud apps.
Experiment with new directions in software development.
Software development in the cloud. Register today to create a project.
Evaluate IBM software and solutions, and transform challenges into opportunities.