Some 'more, in-depth' information on Oracle BPEL PM, ESB and other SOA, day2day things

Sunday, March 26, 2006

BPEL: looping over arrays (collections) by hand

Over the last days I have seen some questions on hand-looping over arrays.
A (long) while ago, Marc, a colleaque from Europe sent me a nice example. So I thought to publish it here. I hope I comply with copyrights here :-0

Imagine you get a collection of emps back from a service, as defined by the following schema (from DB adapter in this case)

<xs:element name="EmpCollection"
<xs:element name="Emp" type="Emp"/>
<xs:complexType name="EmpCollection">
<xs:element name="Emp" type="Emp"
<xs:complexType name="Emp">
<xs:element name="comm" type="xs:decimal"/>
<xs:element name="deptno" type="xs:decimal"/>
<xs:element name="empno" type="xs:decimal"/>
<xs:element name="ename" type="xs:string"/>
<xs:element name="hiredate" type="xs:dateTime"/>
<xs:element name="job" type="xs:string"/>
<xs:element name="mgr" type="xs:decimal"/>
<xs:element name="sal" type="xs:decimal"/>

and accordingly a message type

<message name="EmpCollection_msg">
<part name="EmpCollection"

The first step is to create a variable, based on the msg type that contains this collection, and 2 counters, one (i) for the running index, and one (n) for the length of the collection. All as shown below ..

<variable name="EmpQuerySelect_p_deptno_OutputVariable"
<variable name="i" type="ns3:integer"/>
<variable name="n" type="ns3:integer"/>

Step 2, consists of getting the count of nodes and assigning 1 to the counter

<assign name="prepare_loop">
<from expression="number(1)"/>
<to variable="i"/>
<from expression="ora:countNodes(
<to variable="n"/>

Step 3, is the while loop and to get the information from a selected node, by applying the concepts further

<while name="While_1"
condition="bpws:getVariableData('i') <=
<scope name="Scope_1">
<variable name="selector" type="ns3:string"/>
<variable name="element" element="ns2:Emp"/>
<sequence name="Sequence_1">
<assign name="Get_Element">
<from expression="concat(
<to variable="selector"/>
<from expression="bpws:getVariableData
<to variable="element" query="/ns2:Emp"/>
<empty name="Do_stuff_with_element_Var"/>
<assign name="Increment_index">
<from expression="bpws:getVariableData('i')+1"/>
<to variable="i"/>

and voila - a loop over your collection is done :-)

Having questions? send your feedback on collections here


Post a Comment

<< Home