cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
MaxHeat
Regular Visitor

Passing variables to xpath

I am using Power Automate to call a restful api when an email alert comes into outlook. The api responds with an xml. I need to extract a value from the xml response based on a unique serial number that is scraped from triggering email and saved as a variable. I am trying to use xpath for this, but I can not figure out the syntax that will allow me to pass a variable into the xpath statement. To expedite testing, I have created and stored a standard xml in my flow:

 

xmlsnip.PNG

 

 

var1.PNG

 

I have read a ton of posts on all different boards and tried a ton of different things and I either get an empty data set or an "Invalid Expression" warning.

 

Here are some of the xpath statements I have tried, should return 2003:

 

xpath(xml(outputs('Compose_2')), '//book[author="+var1+"]/year')

 

xpath(xml(outputs('Compose_2')), '//book[author=string(variables('var1'))]/year')

 

xpath(xml(outputs('Compose_2')), '//book[local-name()=variables('var1')]/year')

 

xpath(xml(outputs('Compose_2')), '//book[author="string(variables.var1.value)"]/year')

 

This is just a sample of attempts, I have tried a bunch of different ways. I would appreciate any suggestions on this!!

1 ACCEPTED SOLUTION

Accepted Solutions
DeepakS
Super User
Super User

Hi @MaxHeat 

Here is an xpath example that you can use:

 

My Sample XML: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ms762271(v%3Dvs.85)

 

Following xpath will get the Title by book id:

xpath(xml(xmlstring),'string(/*[name()="catalog"]/*[name()="book" and @id="bk102"]/*[name()="title"])')
 
-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.
 

View solution in original post

7 REPLIES 7
DeepakS
Super User
Super User

Hi @MaxHeat 

Here is an xpath example that you can use:

 

My Sample XML: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ms762271(v%3Dvs.85)

 

Following xpath will get the Title by book id:

xpath(xml(xmlstring),'string(/*[name()="catalog"]/*[name()="book" and @id="bk102"]/*[name()="title"])')
 
-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.
 

View solution in original post

DeepaKS,

 

Thank you for your response. I am trying to pass a variable to the xpath because I need to be able to pull the "book" data based on a dynamic condition that is not known ahead of time. In other words, the required "book id" is saved as a variable and I need to pass that variable to the xpath to get the required "book" information.  Thanks

 

 

THIS DID NOT ANSWER MY QUESTION AND THAT IS WHY I DID NOT MARK IT SOLVED!!!!!

dchasanidis
Advocate II
Advocate II

Hello @MaxHeat ,

I was struggling myself too in order to find a solution for this. You should use concat() function to do the job for you.

Ironically, a string variable is not recognized by itself inside xpath segment, but can be used inside this function.

This is my example that worked:

 

Variable created name: Filter

 

Xpath expression: xpath(xml(body('Get_D365Configurator')),concat('//'variables('Filter')))

 
Please try it for yourself!

 

@dchasanidis 

 

You can also get rid of the concat function in your xpath expression by building the xpath in the variable.

 

For example if you have a string variable Path with a value of: 
//book[author="DynamicConent"]/year

 

Then your xpath expression in @MaxHeat problem would look like:
xpath(xml(outputs('Compose_2')),Path)

@csbarone16 

 

I am sorry for asking, but have you tested that? Because I tried too, but for some reason xpath couldn't recognize the variable correctly as a parameter. If it works plz make a reply in order for me to test it again.

 

Thanks!

@dchasanidisI did test it earlier today and it works.  Here is the sample I was running:

 

Flow1.png

The sample XML looks like the following:

Flow2.png

 

I made two variables in this sample, the first is just to show how I used a parameter in the variable that gets passed to the xpath function.  In my problem I was using a field from a SharePoint list, for this example I just made a string variable.  The Xpath variable named Path is what you send to the xpath function.

Flow3.png

The xpath expression looks like this:  

xpath(xml(outputs('Compose')),variables('Path'))

 

Here are the results

Flow4.png

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

M365 768x460.jpg

Microsoft 365 Collaboration Conference | December 7–9, 2021

Join us, in-person, December 7–9 in Las Vegas, for the largest gathering of the Microsoft community in the world.

Users online (1,861)