首页 > 解决方案 > creating an XML attribute from var in dataweave 2.0

问题描述

I have a JSON input:

{
  abc: "",
  def: "hello"

}

I want to make this blank element as nillable in XML i.e. . I am using the below dataweave code:

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

I am not able to create an attribute using @(xsi#'nil':true) for key($$). Please help me

标签: muledataweaveanypoint-studiomule4

解决方案


解决我在评论中提到的错误,添加根元素是可行的。请记住,与 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>

推荐阅读