在 dataweave 2.0 中从 var 创建一个 XML 属性

creating an XML attribute from var in dataweave 2.0

我有一个 JSON 输入:

{
  abc: "",
  def: "hello"

}

我想让这个空白元素在 XML 中可以为空,即。我正在使用以下数据编织代码:

%dw 2,0
output application/xml skipNullOn="everywhere"
var makeNil= (in) ->
in match {
case is Array -> in map makeNil($)
case is Object -> in mapObject (
if ( ($) == "")
  ($$) @(xsi#'nil':true): {}
else ($$): makeNil($)
)
else -> in
}
---
makeNil(payload)

我无法使用 @(xsi#'nil':true) 为键 ($$) 创建属性。请帮助我

解决我在评论中提到的错误,添加根元素有效。请记住,与 JSON 不同,XML 需要一个根元素。

%dw 2.0
output application/xml skipNullOn="everywhere"
ns xsi http://www.w3.org/2001/XMLSchema-instance
var makeNil= (in) ->
    in match {
        case is Array -> in map makeNil($)
        case is Object -> in mapObject (
            if ( ($) == "")
                ($$) @(xsi#'nil':true): {}
            else ($$): makeNil($)
        )
        else -> in
    }
---
top: makeNil(payload)

输入:

{
  "abc": "",
  "def": "hello"
}

输出:

<?xml version='1.0' encoding='UTF-8'?>
<top>
  <abc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
  <def>hello</def>
</top>