“Is it possible to extract a specific value from an XML using Power Automate flow? What’s the best approach to do that?”
When you build flows in Power Automate, you’re working with JSON. That’s the format on the background, it’s easy to work with, and you can extract any value using an expression. But there’s another format that you can encounter when connecting to other systems, and that’s an XML. The formats are quite similar, you can easily convert JSON into XML, but not the other way around. And as such it’s a bit harder to extract the values. How do you then extract a specific value from an XML?
Use the xpath(…) expression
If you’ve got an XML, you can use the xpath(…) expression to find a specific value. It’s the same expression as used to extract values from SP multiple people picker column, but this time it’s a bit simpler.
xpath(<xml>, <path>)
Note: <…> are placeholders, replace them including < and >.
You already have an XML so you can apply the expression directly on the data. Let’s take the example XML below:
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
…and the value to extract is in the <body>. You could build the <path> manually, but there’re many tools that can do it for you, e.g. the xpather. Copy/paste your XML and while holding ctrl/cmd hover on the desired value. It’ll return the path leading to the value which can be used in the expression.
Note: since it’s a public website you should replace all potentially sensitive data in your XML with some placeholders before you paste it!
Take the path, put it in single quotes, and use it as the second parametr in the xpath(…) expression, e.g.
xpath(xml(outputs('Compose_-_XML')),'//body[1]/text()[1]')
If you encounter the error message “The template language function ‘xpath’ expects its first parameter to be an XML object”, add also the xml(…) expression as shown above.
The expression will return all elements at the defined path in the xml as an array. The last step could be to turn them into a string with the join(…) expression, e.g. separated by comma (if there’re move values).
join(outputs('Compose_-_value_from_xml'),', ')
Summary
When you work with an XML in Power Automate, it’s much easier to use the xpath(…) expression to extract a specific value than trying to somehow parse it. It might look complicated on the first look, but with the available services like the xpather it’s easy to build the path and use it in the expression.
am using two separated compose actions in order to use this XPATH expression:
compose 1: xpath(xml(outputs(‘Get_file_content_compose’)),’//invoicenumber[1]/text()’)
compose 2: xpath(xml(outputs(‘Get_file_content_compose’)),’//invoiceamount[1]/text()’)
Since i am doing this in two separated compose actions i am not able to add this in one row, at the moment it is reflecting as two excel rows.
Can anyone please advise how to make one xpath expression to extract ‘invoicenumber’ and ‘invoiceamount’ ?
and if i use 1 xpath expression instead of 2, will i be able to update and view in one excel row instead of 2?
Hello powerautomate123,
you can use both of the expression in a single ‘Compose’ action and store the data as an object:
{
“InvoiceNumber”: “xpath(xml(outputs(‘Get_file_content_compose’)),’//invoicenumber[1]/text()’)”,
“InvoiceAmount”: “xpath(xml(outputs(‘Get_file_content_compose’)),’//invoiceamount[1]/text()’)”
}