1

I am working in a logic app that retrieve values from an XML. I was working initially with this XML structure:

<Parent>
    <Child1>Hello</Child1>
    <Child2>World</Child2>
</Parent>

And I was able to retrieve the values by using this XPath expression (this is inside of a For_Each action):

"@{xpath(xml(items('For_each_node')), 'string(/*[local-name()=\"Parent\"]/*[local-name()=\"Child1\"])')}"

It works great and I get 'Hello' in this example, however I've been given now this XML:

<Parent>
  <Child N="1">Hello</Child>
  <Child N="2">World</Child>
</Parent>

And now the expression placed above retrieves nothing. I think I need to adjust the expression to do something like 'retrieve node value where child attribute equals 1' but I am not able to make it work, I've seen examples on how to do that but somehow the syntax used in Logic apps for XPath is kind of different as how 'standard' XPath is used.

I would appreciate your help, thanks!

1 Answer 1

1

The XPath to select elements that have an N attribute with the value of 1 and a local-name() of Child

/*[local-name() = "Parent"]/*[local-name() = "Child" and @N = "1"]

Applied to the code you had originally provided:

"@{xpath(xml(items('For_each_node')), 'string(/*[local-name()=\"Parent\"]/*[local-name()=\"Child\" and @N=\"1\"])')}"

If your XML isn't bound to a namespace, then you can simplify things further. Instead of matching generically on any element with * and then using a predicate to test the local-name(), just use:

/parent/child[@N="1"]
Sign up to request clarification or add additional context in comments.

4 Comments

This worked great, thanks! Just one question regarding the suggested short version; it just did not work to me. I tried like this: "@{xpath(xml(items('For_each_node')), 'string(/parent/child[@N="1")')}" but it throws an error regarding an invalid XPath expression. Am I missing something? Thanks!
It look like you were escaping double quotes in your XPath, did you escape the double quotes around the attribute value? Since the expression is inside quotes, may need to or use single quotes.
I actually tried with this (my apologies): "@{xpath(xml(items('For_each_node')), 'string(/parent/child[@N=\"1\")')}", is this what you were referring to? Thanks!
Yes, that is what I was referring to. That is a valid XPath expression. I'm not familiar with Azure Logic. Examples that I have seen of people using XPath also happen to match genrically on element with * and then use predicates to match the local-name() or namespace-uri(), so maybe there is some reason. It's unfortunate, because that's a less readable convention. I guess, for now, stick with what works. Though, might be a candidate for a separate stackoverflow question asking how to use the more simple form of an XPath in Azure Logic Apps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.