The template language function 'xpath' expects its first parameter to be an XML object


Issue


While working on a POC about Debatching in Logic Apps using For Each,  I was encountered with an below error when testing it

The template language function 'xpath' expects its first parameter to be an XML object



"InvalidTemplate. Unable to process template language expressions for action
'For_each' at line '1' and column '1658': 'The template language function
'xpath' expects its first parameter to be an XML object.
The provided value is of type 'String'.
Please see https://aka.ms/logicexpressions#xpath for usage details.'."


Why it happened


As I had to debatch an xml message, following xpath expression was provided to ForEach action

xpath(triggerBody(),'//*[local-name()="PurchaseOrder" and namespace-uri()="http://www.adventure-works.com"]')

And when xml message was posted, the logic app was not able to apply xpath on the
triggerbody

It happened because triggerBody() is an Azure Workflow built-in FUNCTION, which is used to access
the output of trigger(shorthand for trigger().outputs.body) and it’s return type
is String.

What to do


Logic app is very well equipped to process XML message apart from JSON, however if any expressions(functions) are to be applied on it then it has to be explicitly handled by casting it to xml and then use xml based functions.

So the correct expression is


xpath(xml(triggerBody()),'//*[local-name()="PurchaseOrder" and namespace-uri()
="http://www.adventure-works.com"]')

In the above expression first triggerBody() (which is string) is casted in XML using xml() function and then xpath function is applied.


That's it, all worked fine.

Post a Comment

If you have any suggestions or questions or want to share something then please drop a comment

Previous Post Next Post