Saturday, July 2, 2011

BPEL XPath function to retrieve values from a list

In BPEL, if you would like to look through a list of XML elements and check certain condition in each element then follow the steps below:

Sample XML:
<employees>
<employee>
<name>NAME1</name>
<age>25</age>
</employee>
<employee>
<name>NAME2</name>
<age>30</age>
</employee>
<employee>
<name>NAME3</name>
<age>35</age>
</employee>
</employees>

We can use "While" Activity for looping through multiple elements but the real task is to access the data within each dynamically:

1. Initialize a simple int "counter" variable and set the value = 0

2. Count the number of employee nodes in employees root node using,
ora:countNodes("employees/employee")
[Note: Use BPEL Expression builder to get the XPath Expression but remember to delete bpws:getVariableData from the expression because we are not looking for data but only number of nodes]

3. Loop through each of the employee node and check if the <age> is greater than 26 as:

bpws:getVariableData('employees','part',"employee[position()=bpws:getVariableData('counter')]")

4. Increment the counter and you will be able to a access all the employee data dynamically.

Things to remember:

1. In BPEL, the index starts with 1 and not 0 so make sure the "counter" value starts from 1 and not 0.
2. Whereever in the XPath you are accessing a variable inside a variable such as, { "employee[position()=bpws:getVariableData('counter')]" } use double quotes for outside variable and single quotes for inside variable
3. Use position() function as shown above

5 comments:

Sam Galip said...

I am trying to implement this in my project. instead of looping through entire list is there direct way of pulling the desired node. like bpws:getVariableData('employees','part',"employee[name=bpws:getVariableData('var_name')]")

Enceladus said...
This comment has been removed by the author.
Enceladus said...
This comment has been removed by the author.
Enceladus said...

#### ONE Question from my side ####

Extension of above Problem.

case condition="ora:countNodes('RetrieveLogistics_retrieveLogistics_OutputVariable','parameters','/ns43:RetrieveLogisticsResponse/ns24:LogisticList/ns24:Logistic[bpws:getVariableData('LogisticCounter','/ns88:IntegerType')]/ns10:ProductId') > 0"

Now this problem can not be solved by replacing single quotes from double quotes or otherwise.

Please help.

Regards,
EN

Marxchante said...

How can I get the value of position() and set it into a variable?
I only saw examples of position inside "[]" but I need to put the position in a variable.

Search This Blog