Thiru
Thiru

Reputation: 424

How to remove null/empty attributes from XML using dataweave2.0 in mule 4?

I need to remove empty attributes from an XML using dataweave 2.0,

can anybody help here ? skipNullOn="everywhere" is working only on elements not on attributes.

<root att1="" att2="data" att3=Null/> should be transformed into <root att2="data"/>

Upvotes: 0

Views: 1096

Answers (1)

aled
aled

Reputation: 25699

For some neither reason skipOnNull="everywhere" nor skipOnNull="attribute" seem to be filtering empty attributes. I created a recursive function to remove the empty attributes. I don't know what are you calling null attributes. That doesn't seem to be valid in XML.

%dw 2.0
output application/xml

fun filterEmptyAttributes(l) =
    l filterObject !isEmpty($)

fun removeEmptyAttributes(x) = 
    x match {
        case is Object -> 
            x 
            mapObject ((value, key, index) ->  (key) @( (filterEmptyAttributes(key.@) ) ): value ) 
        case is Array -> x map removeEmptyAttributes($) 
        else -> x
  }
---
removeEmptyAttributes(payload)

Input:

<root att1="" att2="data" att3=""/> 

Output:

<?xml version='1.0' encoding='UTF-8'?>
<root att2="data"/>

Upvotes: 1

Related Questions