Imagine you get a collection of books back from a service, as defined by the following schema.
<xs:element name="BooksCollection" type="BooksCollection"/>
<xs:element name="Book" type="Book"/>
<xs:complexType name="BooksCollection">
<xs:sequence>
<xs:element name="Book" type="Book" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Book">
<xs:sequence>
<xs:element name="Name" type="xs:string"/>
<xs:element name="Author" type="xs:string"/>
<xs:element name="Cost" type="xs:decimal"/>
<xs:element name="Publisher" type="xs:string"/>
</xs:sequence>
</xs:complexType>
And accordingly a message type
<message name="BooksCollection_msg">
<part name="BooksCollection"
element="top:BooksCollection"/>
</message>
Step 1
The first step is to create a variable based on the above message type that contains this collection <Which may already be created in the code>
<variable name="BooksCollection_OutputVariable" messageType="ns1:BooksCollection_msg"/>
And 2 counters, one (i) for the running index, and one (n) for the length of the collection.
<variable name="i" type="ns3:integer"/>
<variable name="n" type="ns3:integer"/>
Step 2
Get the count of nodes and initialize the counter <i> with value 1.
<assign name="prepare_loop">
<copy>
<from expression="number(1)"/>
<to variable="i"/>
</copy>
<copy>
<from expression="ora:countNodes(
'BooksCollection_OutputVariable',
'BooksCollection','/ns2:BooksCollection/Book')"
/>
<to variable="n"/>
</copy>
</assign>
Step 3
Comes the while loop to loop over Collection and to get the information from a selected node.
<while name="Loop_Over_Collection"
condition="bpws:getVariableData('i') <= bpws:getVariableData('n')">
<scope name="Scope_1">
<variables>
<variable name="selectorVar" type="ns3:string"/>
<variable name="Book_Element" element="ns2:Book"/>
</variables>
<sequence name="Sequence_1">
<assign name="Get_Book_Element">
<copy>
<from expression="concat(
"/ns2:BooksCollection/Book[",
string(bpws:getVariableData('i')),
"]")"/>
<to variable="selectorVar"/>
</copy>
<copy>
<from expression="bpws:getVariableData
('BooksCollection_OutputVariable',
'BooksCollection',
bpws:getVariableData('selectorVar'))"
/>
<to variable="Book_Element" query="/ns2:Book"/>
</copy>
</assign>
<empty name="Logic_For_Book_Element_Comes_Here"/>
<assign name="Increment_index">
<copy>
<from expression="bpws:getVariableData('i')+1"/>
<to variable="i"/>
</copy>
</assign>
</sequence>
</scope>
</while>
This is the way to loop over a collection message.
0 comments:
Post a Comment