0

How can I add an incremented number in XML array using datawave 2.0 in mule4?

Example:

<Employees>    
    <Employee>    
        <Attribute Name="IncrementValue"></Attribute>    
    </Employee>    
    <Employee>    
        <Attribute Name="IncrementValue"></Attribute>    
    </Employee>    
<Employees>

Above example update IncrementValue with a sequence number. Can anyone help me out?

aled
  • 21,330
  • 3
  • 27
  • 34
user2231233
  • 21
  • 3
  • 7

1 Answers1

0

DataWeave is a functional language. It doesn't have a counter but you may use the index of the mapObject() function for this ejemplo:

%dw 2.0
output application/xml
---
Employees: payload.Employees mapObject((value1, key1, index1) ->
    Employee: value1 mapObject ((value2, key2, index2) -> (key2): index1) 
)

Output for the input in the description:

<?xml version='1.0' encoding='UTF-8'?>
<Employees>
  <Employee>
    <Attribute Name="IncrementValue">0</Attribute>
  </Employee>
  <Employee>
    <Attribute Name="IncrementValue">1</Attribute>
  </Employee>
</Employees>

Index starts from 0. If you want to start from 1 you could just add 1: index + 1

aled
  • 21,330
  • 3
  • 27
  • 34
  • How can i use variable to increment value? – user2231233 Dec 02 '19 at 19:00
  • Please clarify why using the index does not answer the original question. I see that you created a new question for not using the index: https://stackoverflow.com/questions/59145117/how-can-i-increment-variable-value-inside-function-mapobject-datawave-2-0 – aled Dec 03 '19 at 12:14