Search code examples
muledataweavemulesoftmule4

Attribute values are missing while adding new node to xml payload using mule 4


I'm trying to add the new child node(ValidationError) to the existing xml payload using the below dataweave code:

%dw 2.0
var errormessage = "Rating Service Exception Error. Please contact support."
output application/xml writeDeclaration=false
---
DTOApplication : payload.DTOApplication ++ { ValidationError @(TypeCd: "Validation", Name: "Rate Service Exception Error", Msg: errormessage, SubTypeCd: "ERROR") :null}

The ValidationError child node appends perfectly, but the attribute values of the root node DTOApplication are missing.

Input payload: https://github.com/Manikandan99/Map_request/blob/main/request_payload.xml

Expected response: https://github.com/Manikandan99/Map_request/blob/main/Expected_response.xml

The new child node is ValidationError :

<ValidationError TypeCd="Validation" Name="Rate Service Exception Error" Msg="Rating Service Exception Error. Please contact support." SubTypeCd="ERROR"/>

Any ideas please on how to append ValidationError child node to existing payload without missing the attribute values of DTOApplication using dataweave 2.0?


Solution

  • Could you Please try below code, its giving expected output

    %dw 2.0
    var errormessage = "Rating Service Exception Error. Please contact support."
    output application/xml  writeDeclaration=true
    ---
    payload mapObject ((value, key, index) -> 
      (key): value ++ {
        ValidationError @(TypeCd: "Validation", Name: "Rate Service Exception Error", Msg: errormessage, SubTypeCd: "ERROR"): null
      }
    )