codey_08
codey_08

Reputation: 249

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?

Upvotes: 1

Views: 305

Answers (2)

Anurag Sharma
Anurag Sharma

Reputation: 932

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
  }
)

Upvotes: 3

aled
aled

Reputation: 25812

The problem your script has is that it uses the value of the node DTOApplication but ignores that it has attributes. I'm providing a simple fix but @Anurag Sharma solution seems better because it is generic.

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

Upvotes: 2

Related Questions