简体   繁体   中英

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"/>

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"/>

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