Iterating over XML in DataWeave is similar to iterating over arrays or lists. You can use the map or flatMap functions to iterate over nodes in the XML and transform them as needed.
<employees>
<employee>
<id>101</id>
<name>John Doe</name>
<department>IT</department>
</employee>
<employee>
<id>102</id>
<name>Jane Smith</name>
<department>HR</department>
</employee>
<employee>
<id>103</id>
<name>Bob Johnson</name>
<department>Finance</department>
</employee>
</employees>
%dw 2.0 output application/json --- payload.employees.*employee map { id: $.id, name: $.name, department: $.department }
[
{
"id": "101",
"name": "John Doe",
"department": "IT"
},
{
"id": "102",
"name": "Jane Smith",
"department": "HR"
},
{
"id": "103",
"name": "Bob Johnson",
"department": "Finance"
}
]
We can also use the flatMap function to transform the XML in a different way. Suppose we want to transform the XML to a CSV string where each row represents an employee and contains the id, name, and department fields separated by commas. We can use the following DataWeave code to achieve this:
%dw 2.0
output text/csv
header=true
---
payload.employees.*employee flatMap ((employee, index) -> [
(index == 0) ? ["id", "name", "department"] : null,
[employee.id, employee.name, employee.department]
])
In this code, we use the flatMap function to iterate over each employee node in the XML. We use the $ syntax to reference the child nodes of the employee node. Inside the flatMap function, we create an array for each row of the CSV, with the first row containing the header and the subsequent rows containing the employee data. We use the ternary operator (index == 0) ? […] : null to conditionally include the header row.
The output of this code is a string in CSV format:
id,name,department
101,John Doe,IT
102,Jane Smith,HR
103,Bob Johnson,Finance