Skip to main content

Notifications

Community site session details
Power Automate - Building Flows
Answered

Getting XML attribute using XPATH

Like (0) ShareShare
ReportReport
Posted on 27 Oct 2023 17:39:17 by 578

 have an XML that looks like this:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<w:comments>
 <w:comment w:author="Smith, John" w:date="2023-10-06T12:26:00Z" >
 <w:p w:rsidR="00294E10" w:rsidP="007202C8" w:rsidRDefault="00294E10" w14:paraId="3DA74C72" w14:textId="77777777">
 <w:pPr>
 <w:pStyle w:val="CommentText" />
 <w:ind w:left="0" />
 </w:pPr>
 <w:r>
 <w:rPr>
 <w:rStyle w:val="CommentReference" />
 </w:rPr>
 <w:annotationRef />
 </w:r>
 <w:r>
 <w:t>Comment 1</w:t>
 </w:r>
 </w:p>
 </w:comment>
 <w:comment.....

 

I pull the text I need from "w:t" using:

xpath(outputs('Compose_XML'), '//*[name()=''w:t'']/text()')

//Response: 
[
 "Comment 1",
 "Comment 2",
 "Comment 3"
 ]

 

The issue is that I'm having a hard time getter the attribute "w:author" text. I'm using:

//Option 1:
xpath(outputs('Compose_XML'), '//*[name()=''w:comment'']/@*[name()=''w:author'']') 

//Option 2:
xpath(outputs('Compose_XML'), '//@*[name()="w:author"]')

//Response:
[
 "w:author=\"Smith, John\"",
 "w:author=\"Anderson, Thomas\"",
 "w:author=\"Moss, Carrie-Anne\""
]

 

Both options give me a text with extra quotes, backslashes and '"w:author=\". How can I just get the names (e.g. Smith, John)?

 

Thanks in advance.

  • grantjenkins Profile Picture
    11,059 Super User 2025 Season 1 on 28 Oct 2023 at 01:22:25
    Re: Getting XML attribute using XPATH

    A slightly better approach that doesn't use the replace expression is below.

     

    //From
    xpath(xml(outputs('Compose_XML')), '//*/w:comment[@author]')
    
    //Map
    xpath(item(), 'string(//@w:author)')

    grantjenkins_0-1698455857794.png

  • Verified answer
    grantjenkins Profile Picture
    11,059 Super User 2025 Season 1 on 28 Oct 2023 at 01:00:22
    Re: Getting XML attribute using XPATH

    Power Automate really needs to improve on its XPath implementation. What you have "should" only return the value of each attribute. I've tried numerous ways to try and get just the value but no luck.

     

    A workaround is to do the following. Use a Select to get the initial output from the XPath expression, then use replace to remove the prefix and double quotes.

     

    //From
    xpath(outputs('Compose_XML'), '//@*[name()="w:author"]')
    
    //Map
    replace(replace(item(), 'w:author=', ''), '"', '')

     

    grantjenkins_1-1698454704791.png

     

    This would give the following output:

     

    [
     "Smith, John",
     "Anderson, Thomas",
     "Moss, Carrie-Anne"
    ]

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Power Automate - Building Flows

#1
stampcoin Profile Picture

stampcoin 101

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 82 Super User 2025 Season 1

#3
David_MA Profile Picture

David_MA 48 Super User 2025 Season 1

Overall leaderboard