Introduction
In last post Debatching(Splitting) XML Message in Logic Apps - ForEach and SplitOn and we understood about how For each can be used to split/debatch messages.
In above scenario we are using only one for each, whereas there can be situations where more than one For each would be required to solve the problem.
Scenario
Say below is the purchase order and we need to take some action based on productName (value to be fetched).
So we have two arrays to deal with
1. Products (having multiple product)
2. Product (having single product detail) within each products
Creating Logic App
Using xpath above - we are asking to traverse through the triggerBody and look out of products node and if found add entry in For each array.(All occurrence of products nodes are added).
xpath(xml(triggerbody()),'purchaseOrder/products')
The array will have all the products node , so if input has 10 Products node then the array formed using xpath will have 10 items in it, each representing a single products node.
Next, we need to loop through each product from products and for that we will use array which was created in first For each and for that syntax is - items('For_each') and using xpath - we are asking to traverse through the items('For_each') and look out of product node and if found add entry in For each array.(All occurrence of product nodes are added).
xpath(xml(items('For_each')),'products/product')
last step is to use Set Variable action where we fetch value of product name from each product node, and for that we will use array which was created in second For each and for that syntax is - items('For_each_2') and using xpath - we get the current element value.
xpath(xml(items('For_each_2')),'product/productName/text()')[0]