Skip to main content
Power Automate
    • Connectors
    • Templates
    • Take a guided tour
    • Digital process automation
    • Robotic process automation
    • Business process automation
    • Process advisor
    • AI Builder
  • Pricing
  • Partners
    • Blog
    • Documentation
    • Roadmap
    • Self-paced learning
    • Webinar
    • Business process and workflow automation topics
    • Overview
    • Issues
    • Give feedback
    • Overview
    • Forums
    • Galleries
    • Submit ideas
    • User groups
    • Register
    • ·
    • Sign in
    • ·
    • Help
    Go To
    • Microsoft Power Automate Community
    • Welcome to the Community!
    • News & Announcements
    • Get Help with Power Automate
    • General Power Automate Discussion
    • Using Connectors
    • Building Flows
    • Using Flows
    • Power Automate Desktop
    • Process Advisor
    • AI Builder
    • Power Automate Mobile App
    • Translation Quality Feedback
    • Connector Development
    • Power Platform Integration - Better Together!
    • Power Platform Integrations
    • Power Platform and Dynamics 365 Integrations
    • Galleries
    • Community Connections & How-To Videos
    • Webinars and Video Gallery
    • Power Automate Cookbook
    • Events
    • 2021 MSBizAppsSummit Gallery
    • 2020 MSBizAppsSummit Gallery
    • 2019 MSBizAppsSummit Gallery
    • Community Engagement
    • Community AMA
    • Community Blog
    • Power Automate Community Blog
    • Community Support
    • Community Accounts & Registration
    • Using the Community
    • Community Feedback
    cancel
    Turn on suggestions
    Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.
    Showing results for 
    Search instead for 
    Did you mean: 
    • Microsoft Power Automate Community
    • Galleries
    • Power Automate Cookbook
    • How to insert an XML element throughout an XML doc...

    How to insert an XML element throughout an XML document (and not just to one node)?

    10-13-2022 22:22 PM

    Multi Super User VJR
    Multi Super User
    417 Views
    LinkedIn LinkedIn Facebook Facebook Twitter Twitter
    VJR
    Multi Super User VJR
    Multi Super User
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    How to insert an XML element throughout an XML document (and not just to one node)?

    ‎10-13-2022 10:22 PM

     

    • Here is a sample Input XML Document

     

    VJR_0-1665719440674.png

     

    • What if you would like to add the <price> element to the entire XML document for every book node?

      Directly using the Insert XML element action inserts only to the first node mentioned in the XPath query. 

     

    VJR_2-1665720297609.png

     

     

    • For doing this, first find out the count of book nodes.

            Use an XPATH on the book node.

    VJR_3-1665720684190.png

     

    Since there are 4 books, the count property of the Output variable ie; %XPathResults.Count% will have a count of 4 in this example.

     

    Note: There is a XPATH function called as Count() that is normally used to find out the count of nodes.

    But this function did not seem to work in the above action. Perhaps, functions are not yet supported.

     

     

    • To insert an XML element on every single node, you need to start with a Counter and have it running to every single book node.

    Lets call it NodeCounter.

     

    VJR_4-1665721058642.png

     

     

    • Iterate this counter using a While loop until it reaches the last Book node.

    Remember to increment it.

     

    VJR_5-1665721217809.png

     

    • The key part is to use this counter within the Xpath so that it applies to every book node of the XML.

    Example: /bookstore/book[1] selects the first book element that is the child of the bookstore element

     

    VJR_0-1665724693347.png

     

     

    Since this is within a loop, the counter will be applied to all the book nodes of the XmlDocument

     

    • This is how the entire Flow looks like

    VJR_6-1665721346682.png

     

     

    • After running the process you will see the price element for every single book node.

    VJR_7-1665721640975.png

     

     

    • At the end, you will have your new Xml in the variable called XmlDocument (not yet as a xml file). 

    Finally, using Write XML to file action use this variable to generate your final Xml file. (Reference video for writing to file here)

     

     

     

    No attachments.zip
    Labels:
    • Labels:
    • Desktop flows
    Message 1 of 1
    417 Views
    0 Kudos
    Reply
    • All forum topics
    • Previous Topic
    • Next Topic

    Power Platform

    • Overview
    • Power BI
    • Power Apps
    • Power Pages
    • Power Automate
    • Power Virtual Agents

    • Sign up free
    • Sign in

    Browse

    • Templates
    • Connectors
    • Partners

    Downloads

    • Mobile
    • Gateway

    Learn

    • Documentation
    • Learn
    • Support
    • Community
    • Give feedback
    • Blog
    • Pricing

    • © 2023 Microsoft
    • Contact us
    • Trademarks
    • Privacy & cookies
    • Manage cookies
    • Terms of use
    • Terms & conditions
    California Consumer Privacy Act (CCPA) Opt-Out Icon Your California Privacy Choices