cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
andi083
New Member

Extract data from XML using xPath

I would like to use xpath in flow to get the Position number of all OrderItems containing Article aid 9999 from below xml

 

tried to use 

 

xpath(xml(outputs('XML')), '//OrderItem[Article[@aid='9999']]/Position') but the Syntax seems to be wrong. What I expect is to get Position 1 and Position 3. 

 

 

        <Order>
            <Onr>106346</Onr>
            <OrderItems>
                <OrderItem>
                    <Position>1</Position>
                    <Article aid="9999">Red Pencil</Article>
                </OrderItem>
                <OrderItem>
                 <OrderItem>
                    <Position>2</Position>
                    <Article aid="8888">Blue Pencil</Article>
                </OrderItem>
                <OrderItem>
                 <OrderItem>
                    <Position>3</Position>
                    <Article aid="9999">Red Pencil</Article>
                </OrderItem>
            </OrderItems>
        </Order>
 

  

  

8 REPLIES 8
abm
Super User
Super User

Hi @andi083 

 

In your sample xml file you have <OrderItem> tags without closing </OrderItem> exists. First of all check your data and remove the unwanted tags.

 

Then try the below expression

 

xpath(xml(outputs('Compose_2')), '//OrderItems//OrderItem[Article[@aid=''9999'']]//Position')
 
Then add a foreach loop to extract the postion elements.
 
 


Did I answer your question? Mark my post as a solution!

If you liked my response, please consider giving it a thumbs up


Proud to be a Flownaut!

Learn more from my blog
Power Automate Video Tutorials
v-alzhan-msft
Community Support
Community Support

Hi @andi083 ,

 

Have you take a try with @abm 's suggestions?

 

Best regards,

Alice       

Community Support Team _ Alice Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

No, unfortunately it is not working. I will reply later in detail and provide code and screenshots. Thanks

I've been playing around with the code for a long time now and came across the actual problem. The XML file starts with a header like "<ServiceResponse xmlns="http://....//" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">" If I remove this Header manually, the query works, if the header stays - it doesn't work. I just wonder how I can ignore the header in the query? 

 

In real operation, the header always comes with, because it is an automated HTTP request, which then Returns the XML file.

 

I was testing the xPath with http://xpather.com/ and it works like expectet. However in the flow it doesnt work. Only when I remove <ServiceResponse> tag it works. 

 

so XML Looks like: 

 

<ServiceResponse xmlns="http://....//" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">

  <Order>
            <OrderItems>
                <OrderItem>
                    <Position>1</Position>
                    <Article aid="9999">Red Pencil</Article>
                </OrderItem>
           
                 <OrderItem>
                    <Position>2</Position>
                    <Article aid="8888">Blue Pencil</Article>
                </OrderItem>
          
                 <OrderItem>
                    <Position>3</Position>
                    <Article aid="9999">Red Pencil</Article>
                </OrderItem>
            </OrderItems>
        </Order>

</ServiceResponse>

Hi @andi083 

 

I did tried using xpather and it did worked fine.

 

Just another thought.

 

In your xml results use a Replace function expression xmlns="http://....//" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" to blank so that you end up having <ServiceResponse>

 

Finally you could use the xpath(xml(outputs('Compose_2')), '//ServiceResponse//Order//OrderItems//OrderItem[Article[@aid=''9999'']]//Position')

 

Thanks



Did I answer your question? Mark my post as a solution!

If you liked my response, please consider giving it a thumbs up


Proud to be a Flownaut!

Learn more from my blog
Power Automate Video Tutorials

Hi @andi083 

 

Did a bit more experiment with your xml file you have provided. Ihave added xmlns:j namespace. There was a namespace missing earlier. Now I can get the xpath working

 

xpath(xml(outputs('Compose_2')), '//ServiceResponse//Order//OrderItems//OrderItem[Article[@aid=''9999'']]//Position')

 

<ServiceResponse xmlns:j="http:site.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Order>
<Onr>106346</Onr>
<OrderItems>
<OrderItem>
<Position>1</Position>
<Article aid="9999">Red Pencil</Article>
</OrderItem>
<OrderItem>
<Position>2</Position>
<Article aid="8888">Blue Pencil</Article>
</OrderItem>
<OrderItem>
<Position>3</Position>
<Article aid="9999">Red Pencil</Article>
</OrderItem>
</OrderItems>
</Order>
</ServiceResponse>

 

Thanks



Did I answer your question? Mark my post as a solution!

If you liked my response, please consider giving it a thumbs up


Proud to be a Flownaut!

Learn more from my blog
Power Automate Video Tutorials

Thank you very much for your answer. The replace() function does not work in XML because you get the following error. So I would first have to convert the XML file to a string, then replace it and finally convert it back to XML. This doesn't seem to be the best option, nor is it very process-safe

 

InvalidTemplate. Unable to process template language expressions in action 'XML' inputs at line '1' and column '2649': 'The template language function 'replace' expects three string parameters: the string to replace characters inside of as the first parameter, the string that will be replaced by the new string as the second parameter, and the new string as the third parameter. The function was invoked with '1' parameter(s). Please see https://aka.ms/logicexpressions#replace for usage details.'.

Hi @andi083 

 

Like I mentioned in my earlier post if you can fix the namespace (See highlighted) you can resolve this issue.

 

<ServiceResponse xmlns:j="http:site.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">

 

If not you need to convert to string and replace with blank or add a namespace. That should work.

 

 



Did I answer your question? Mark my post as a solution!

If you liked my response, please consider giving it a thumbs up


Proud to be a Flownaut!

Learn more from my blog
Power Automate Video Tutorials

Helpful resources

Announcements
Power Automate News & Announcements

Power Automate News & Announcements

Keep up to date with current events and community announcements in the Power Automate community.

Power Automate Community Blog

Power Automate Community Blog

Check out the latest Community Blog from the community!

Users online (2,525)