简体   繁体   中英

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 ie . 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

Solving the errors that I mentioned in my comment, adding a root element works. Remember that XML unlike JSON requires a root element.

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

input:

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

output:

<?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM