简体   繁体   English

如何使用 mule 4 中的 dataweave2.0 从 XML 中删除空/空属性?

[英]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,我需要使用 dataweave 2.0 从 XML 中删除空属性,

can anybody help here?有人可以帮忙吗? skipNullOn="everywhere" is working only on elements not on attributes. skipNullOn="everywhere" 仅适用于元素而不适用于属性。

<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.由于某些原因skipOnNull="everywhere"skipOnNull="attribute"似乎都没有过滤空属性。 I created a recursive function to remove the empty attributes.我创建了一个递归的 function 来删除空属性。 I don't know what are you calling null attributes.不知道你说的null属性是什么。 That doesn't seem to be valid in XML.这在 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: Output:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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