Sufi
Sufi

Reputation: 196

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

Upvotes: 1

Views: 928

Answers (1)

aled
aled

Reputation: 25664

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>

Upvotes: 1

Related Questions