Logic Apps - extracting XML values with special characters using XPath

Jagath Ariyarathne 0 Reputation points
2025-01-10T16:37:51.97+00:00

I am retrieving an xml value from the input payload using XPath. The value contains an encoded '&' as '&' since XML does not allow '&' in the content.

Input XML message: "<test>12345&amp;</test>"

XPath expression I am using:

xpath(xml(triggerBody()),'//*[local-name()="test"]/text()')?[0]

Output : 12345&

The output contains decoded value of '&amp;' as '&'. But I want to get exact input text value "12345&amp;" to a variable without any decoding. How can I do that?

I could use "replace" function but it is difficult to do every field in a large xml to replace all special characters in XML.

Thank you

Azure Logic Apps
Azure Logic Apps
An Azure service that automates the access and use of data across clouds without writing code.
3,299 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Khadeer Ali 1,790 Reputation points Microsoft Vendor
    2025-01-10T17:30:37.63+00:00

    @Jagath Ariyarathne ,

    Welcome to Microsoft Q&A Platform!

    Thanks for reaching out your query on extracting XML values with special characters using XPath.

    Logic Apps isn't the best fit for this requirement because it automatically decodes special characters in XML (like turning &amp; into &), and there’s no built-in way to prevent this.

    To handle this properly, try using Azure Function. With a function, we can extract the exact XML value while keeping characters like &amp; intact. The Logic App can simply send the XML to the function, and it will return the correct value for further use.

    Hope this helps. Do let us know if you any further queries.


    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.