user1905307
user1905307

Reputation: 47

Dataweave 2.0 - How to inject an additional XML element in to an existing XML structure

I'm trying to figure out how to do this in dataweave, but not having much luck.

Is there an elegant way to do this without having to rebuild the XML structures again?

Given the below source and target examples, how would you inject a new element into only the first UserArea/PropertyList?

New element:

 <Property>
    <NameValue name="new.attribute">new value</NameValue>
 </Property>

Source structure

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <UserArea>
      <PropertyList>
         <Property>
            <NameValue name="xxx.CreatedBy">Test 1</NameValue>
         </Property>
         <Property>
            <NameValue name="xxx.EnteredBy">Test 2</NameValue>
         </Property>
         <Property>
            <NameValue name="xxx.SafetyFlag">false</NameValue>
         </Property>
         <Property>
            <NameValue name="xxx.DependFlag">true</NameValue>
         </Property>
         <Property>
            <NameValue name="eam.UDFCHAR10">ABC</NameValue>
         </Property>
      </PropertyList>
   </UserArea>
   <UserArea>
      <PropertyList>
         <Property>
            <NameValue name="xxx.Exited">Test 3</NameValue>
         </Property>
         <Property>
            <NameValue name="xxx.Entered">Test 4</NameValue>
         </Property>
         <Property>
            <NameValue name="xxx.SafetyFlag">false</NameValue>
         </Property>
         <Property>
            <NameValue name="xxx.DependFlag">true</NameValue>
         </Property>
         <Property>
            <NameValue name="eam.UDFCHAR10">ABC</NameValue>
         </Property>
      </PropertyList>
   </UserArea>
</root>

Target Structure

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <UserArea>
      <PropertyList>
         <Property>
            <NameValue name="xxx.CreatedBy">Test 1</NameValue>
         </Property>
         <Property>
            <NameValue name="xxx.EnteredBy">Test 2</NameValue>
         </Property>
         <Property>
            <NameValue name="xxx.SafetyFlag">false</NameValue>
         </Property>
         <Property>
            <NameValue name="xxx.DependFlag">true</NameValue>
         </Property>
         <Property>
            <NameValue name="xxx.UDFCHAR10">ABC</NameValue>
         </Property>
         <Property>
            <NameValue name="new.attribute">new value</NameValue>
         </Property>
      </PropertyList>
   </UserArea>
   <UserArea>
      <PropertyList>
         <Property>
            <NameValue name="xxx.Exited">Test 3</NameValue>
         </Property>
         <Property>
            <NameValue name="xxx.Entered">Test 4</NameValue>
         </Property>
         <Property>
            <NameValue name="xxx.SafetyFlag">false</NameValue>
         </Property>
         <Property>
            <NameValue name="xxx.DependFlag">true</NameValue>
         </Property>
         <Property>
            <NameValue name="xxx.UDFCHAR10">ABC</NameValue>
         </Property>
      </PropertyList>
   </UserArea>
</root>

Upvotes: 0

Views: 559

Answers (1)

Salim Khan
Salim Khan

Reputation: 4303

Would you consider this as rebuilding the xml structure or did you mean handcrafting each element in DW script

Input As given in the question

Script

%dw 2.0
output application/xml
var newElementAdded = {"NameValue" @(name:"new.attribute"): "new value"}
---

root: (payload.root mapObject {
    UserArea: (if(($$$) == 0) "PropertyList": $.PropertyList ++ {"Property": newElementAdded} else $)
})

Output

<?xml version='1.0' encoding='UTF-8'?>
<root>
  <UserArea>
    <PropertyList>
      <Property>
        <NameValue name="xxx.CreatedBy">Test 1</NameValue>
      </Property>
      <Property>
        <NameValue name="xxx.EnteredBy">Test 2</NameValue>
      </Property>
      <Property>
        <NameValue name="xxx.SafetyFlag">false</NameValue>
      </Property>
      <Property>
        <NameValue name="xxx.DependFlag">true</NameValue>
      </Property>
      <Property>
        <NameValue name="eam.UDFCHAR10">ABC</NameValue>
      </Property>
      <Property>
        <NameValue name="new.attribute">new value</NameValue>
      </Property>
    </PropertyList>
  </UserArea>
  <UserArea>
    <PropertyList>
      <Property>
        <NameValue name="xxx.Exited">Test 3</NameValue>
      </Property>
      <Property>
        <NameValue name="xxx.Entered">Test 4</NameValue>
      </Property>
      <Property>
        <NameValue name="xxx.SafetyFlag">false</NameValue>
      </Property>
      <Property>
        <NameValue name="xxx.DependFlag">true</NameValue>
      </Property>
      <Property>
        <NameValue name="eam.UDFCHAR10">ABC</NameValue>
      </Property>
    </PropertyList>
  </UserArea>
</root>

Upvotes: 3

Related Questions