Blog Post

Azure Integration Services Blog
2 MIN READ

XPath to select array of nodes satisfying a child property with Logic Apps

David_Burg's avatar
David_Burg
Icon for Microsoft rankMicrosoft
Jun 18, 2020

The final query expression we will be decomposing today is this:

 

xpath(xml(variables('count')), '/*[local-name()="lists"]/*[local-name()="list"]/*[local-name()="name" and text()="*Billable"]/..')

 

 

This is the XPath function (https://docs.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#xpath).

We assume there is a count string typed variable containing the XML payload to query into. The variables function looks-up that variable (https://docs.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#variables).

The XML function (https://docs.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#xml) parses that string input to an XML document which the XPath function expects as first input.

 

Then comes the XPath itself

 

/*[local-name()="lists"]/*[local-name()="list"]/*[local-name()="name" and text()="*Billable"]/..

 

 

First 

 

/*[local-name()="lists"]

 

 

allows to select all 'lists' named elements at root level without consideration for namespace. Beware many online XPath test tools will ignore the input XML document namespace, while .NET, and Logic Apps XPath function built with .NET, honor the namespace. So for a node to match with just 

 

/lists

 

it needs to be 

 

<lists>

 

with no namespace assigned. Beware children nodes inherit default namespaces assign by their parents (

 

xmlns="http://..."

 

).

 

Second 

 

/*[local-name()="name" and text()="*Billable"]

 

lets you select all the name nodes where the name value is '*Billable'. It is typical to want to select elements from an XML array which have specific value for a child node or attribute. This demonstrate selecting the name nodes equal to *Billable.

 

Third

 

/..

 

lets you go back to the parent node of the name node, so you are able to list all the 'list' elements which satisfied the condition on their leave nodes (has a name element of value *Billable).

Updated Jun 18, 2020
Version 1.0
No CommentsBe the first to comment