Skip to main content

Notifications

Community site session details

Community site session details

Session Id :

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

VJR Profile Picture Posted by VJR 7,635

 

  • 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)

 

 

 

Categories:

Desktop flows

Comments