我正试图使用以下数据编织代码将新的子节点(ValidationError(添加到现有的xml负载中:
%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}
ValidationError子节点完美地追加,但是缺少根节点DTOApplication的属性值
输入有效载荷:https://github.com/Manikandan99/Map_request/blob/main/request_payload.xml
预期响应:https://github.com/Manikandan99/Map_request/blob/main/Expected_response.xml
新的子节点是ValidationError:
<ValidationError TypeCd="Validation" Name="Rate Service Exception Error" Msg="Rating Service Exception Error. Please contact support." SubTypeCd="ERROR"/>
关于如何使用dataweave 2.0在不丢失DTOApplication属性值的情况下将ValidationError子节点附加到现有负载,有什么想法吗?
你能试一下下面的代码吗,它给出了预期的输出
%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
}
)
脚本的问题是它使用节点DTOApplication的值,但忽略了它具有属性。我提供了一个简单的解决方案,但@Anurag Sharma解决方案似乎更好,因为它是通用的。
%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}
}