Unleashing the Power of XPath in Power Automate
Power Automate’s xpath()
function opens a world of possibilities for data manipulation and transformation within your workflows. While the official documentation doesn’t explicitly specify the XPath version, testing reveals that Power Automate supports XPath 1.0. For a complete reference on XPath 1.0 syntax and functions, consult the official Microsoft documentation.
Beyond the Bookstore Example
Many XPath examples focus on a simple bookstore scenario. Let’s explore a richer example with more detail:
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price currency="USD">29.99</price>
<info>
<chapters>
<chapter>
<name>The Boy Who Lived</name>
<page>1</page>
<description>Introduction to Harry Potter's early life.</description>
</chapter>
<chapter>
<name>The Vanishing Glass</name>
<page>15</page>
<description>Harry's encounter with a snake at the zoo.</description>
</chapter>
</chapters>
</info>
</book>
</book>
<title lang="de">Der Hobbit</title>
<author>J.R.R. Tolkien</author>
<year>1937</year>
<price currency="USD">19.99</price>
<info>
<chapters>
<chapter lcid="1031">
<name>Eine unerwartete Party</name>
<page>1</page>
<description>Bilbo Beutlin trifft die Zwerge.</description>
</chapter>
<chapter lcid="1031">
<name>Gebratenes Hammelfleisch</name>
<page>35</page>
<description>Die Zwerge begegnen Trollen.</description>
</chapter>
</chapters>
</info>
</book>
<book>
<title lang="en">1984</title>
<author>George Orwell</author>
<year>1949</year>
<price currency="USD">14.99</price>
<info>
<chapters>
<chapter>
<name>The Principles of Newspeak</name>
<page>1</page>
<description>Introduction to the language of Oceania.</description>
</chapter>
<chapter>
<name>War is Peace</name>
<page>50</page>
<description>Explanation of the Party's slogans.</description>
</chapter>
</chapters>
</info>
</book>
</bookstore>
Important: Always parse the XML string using the xml()
function before applying xpath()
.
Selecting Nodes with XPath
XPath Expression | Result |
---|---|
xpath(variables('oBookstoreXml'),'//book/*') |
All direct children of ‘book’ nodes |
xpath(variables('oBookstoreXml'),'//book/*[@*]') |
All direct children of ‘book’ nodes with at least one attribute |
xpath(variables('oBookstoreXml'),'//book[title[@lang="en"]]') |
All ‘book’ nodes where the title has lang="en" |
xpath(variables('oBookstoreXml'),'//book/*[self::title or self::author]') |
All title and author elements of all book nodes |
Note: The |
operator (union) isn’t supported in XPath 1.0. The example above demonstrates an alternative for selecting multiple paths.
XPath Functions in Power Automate
The following examples utilize the “Select” action. For a list of supported XPath functions, refer to the Microsoft documentation.
Example 1: Basic Node Information
This example retrieves node information using name()
, string()
, count()
, concat()
, and position()
. The “From” field contains the XPath expression xpath(variables('oBookstoreXml'),'//book/*')
.
The “Map” section defines the following:
Value | Expression |
---|---|
nodeName | xpath(item(), 'name(/*)') |
nodeValueAsString | xpath(item(), 'string(/*)') |
chapterCount | xpath(item(), 'count(//chapter)') |
chapterInformation | xpath(item(), 'concat("This node has ",count(//chapter), " chapter child nodes")') |
nodePosition | xpath(item(), 'position()') |
Because the XPath expressions in “Map” operate on the subset returned by “From,” position()
always returns 1. Some string functions like upper-case()
are not available in XPath 1.0.
Example 2: Working with Attributes
This example focuses on filtering and accessing attributes. The “From” field is xpath(variables('oBookstoreXml'),'//book/*[@*]')
.
“Map” is defined as follows:
Value | Expression |
---|---|
nodeName | xpath(item(), 'name(/*)') |
attributeLang | xpath(item(), '//@lang') |
attributeLangValue | xpath(item(), 'string(//@lang)') |
anyAttribute | xpath(item(), '//@*') |
Example 3: Complex Transformations
This example demonstrates more advanced transformations, including creating arrays and JSON objects. The “From” field remains xpath(variables('oBookstoreXml'),'//book/*[@*]')
.
“Map” includes these transformations:
Value | Expression |
---|---|
titleAsArray | xpath(item(), '//title/text()') |
chapterAsArray | xpath(item(), '//chapter/name/text()') |
titlesAsString | xpath(item(), 'string(//title)') |
chapterAsString | xpath(item(), 'string(//chapter/name)') |
chaptersAfterPage10AsArray | xpath(item(), '//chapter[page>10]/name/text()') |
chaptersAndPages | xpath(item(), '//chapter/*[self::name or self::page]/text()') |
titleAndChaptersAsJSON | json(concat('{"title":"',xpath(item(), string(//title)'),'","chapters":',xpath(item(), '//chapter/name/text()'),'}')) |
Note the use of Power Automate’s json()
and concat()
functions in conjunction with XPath expressions to create a JSON object. Also, string()
casts nodes to strings, while /text()
directly selects text node content.
XPath Beyond XML
XPath’s utility extends beyond XML. When the “Select” action isn’t sufficient, consider transforming JSON to XML and leveraging xpath()
for more complex operations. For instance:
xml(json(concat('{ "root": { "arr": ',variables('jsonResponse'), '}}')))
This transforms a JSON response into XML, making it suitable for XPath processing. Combining “Select” with xpath()
provides a powerful way to manipulate and extract data within your Power Automate workflows.