Wednesday, August 15, 2007

Modifying XML records dynamically in BPEL

When you use an adapter to retrieve data from the database or reading records from a file, you will get a collection of XML records. Often you want to modify data within this list of XML records. This can be done like the the BPELX:InsertAfter/BPELX: and other functions. But there is another way to to this.

To dynamically change the value of element in a XML record you can manipulated the 'to:' element in the copy clause:

Normally you have:

<copy>
<from variable="InputVariable"
part="payload"
query="/ns1:HelloWorldRequest/ns1:input"/>
<to variable="OutputVariable"
part="payload"
query="/ns1:HelloWorldRequest/ns1:ListOfOrders
/ns1:Order:/ns1:Amount"/>
</copy>
This statement will fail if the the XML payload contains more than one row. Because the assigment is done on a single record. If we use a record counter that loops through this XML payload we are able to assign the specific record very easily. You can change copy in:

<copy>
<from variable="InputVariable"
part="payload"
query="/ns1:HelloWorldRequest/ns1:input"/>
<to variable="OutputVariable"
part="payload"
query="/ns1:HelloWorldRequest/ns1:ListOfOrders[$RecordCounter]
/ns1:Order:/ns1:Amount"/>
</copy>

The trick is to add the [$RecordCounter] directly in in the XPATH query of the copy activity. The BPEL PM will interprete this and translate this into the correct value.

Note: You must define the variable "RecordCounter" in your scope as an integer.

Post a Comment