Reputation: 249
I'm trying to replace the child node names "Layer01", "Layer02" "Layer03", and so on, with "Layer" in an XML document.
In Mule 3 I used:
payload.replaceAll('Layer\\d\\d','Layer')
The logic is to replace the child nodes that start with the string "Layer".
Example input:
<Layers>
<Layer01>
............
............
</Layer01>
<Layer30>
............
............
</Layer30>
....
....
</Layers>
Expected output:
<Layers>
<Layer>
............
............
</Layer>
<Layer>
............
............
</Layer>
....
....
</Layers>
How can I implement this transformation in Mule 4?
Full Input payload : https://github.com/Manikandan99/Map_request/blob/main/input_rating.xml
Full Expected output : https://github.com/Manikandan99/Map_request/blob/main/output_rating.xml
Upvotes: 1
Views: 358
Reputation: 25699
You can use a recursive function to achieve the same result. Test each key to see if it matches the pattern, then replace it, otherwise let is as it was. Apply the function recursively to each value in case the child elements need transformation.
%dw 2.0
output application/xml
fun renameLayers(x) =
x match {
case is Object ->
x mapObject ((value, key, index) -> (key as String replace /^Layer\d\d/ with "Layer"): renameLayers(value))
else -> x
}
---
renameLayers(payload)
Input:
<Layers>
<Layer01>
<Layer02>
</Layer02>
</Layer01>
<Layer30>
<Other>
</Other>
</Layer30>
</Layers>
Output:
<?xml version='1.0' encoding='UTF-8'?>
<Layer>
<Layer>
<Layer/>
</Layer>
<Layer>
<Other/>
</Layer>
</Layer>
Upvotes: 2