You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Mule 4中使用DataWeave 2.0移除XML中的空值与空属性?

DataWeave 2.0 移除XML空属性解决方案

skipNullOn="everywhere"配置仅对XML元素生效,无法过滤空字符串、null类型的属性,可通过自定义递归函数实现属性过滤需求,具体实现如下:

核心实现代码

%dw 2.0
output application/xml indent=true
// 递归过滤节点空属性的通用函数
fun filterEmptyAttributes(node: Object) = node mapObject (value, key) -> {
    (key) @(
        // 过滤值为空字符串或null的属性
        (value.@ filterObject ($ != "" and $ != null))
    ): 
    // 递归处理子节点
    if(value is Object) filterEmptyAttributes(value) else value
}
---
filterEmptyAttributes(payload)

测试验证

输入示例

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

输出结果

<root att2="data"/>

扩展说明

  • 该函数支持处理任意嵌套层级的XML结构,所有节点的空属性都会被自动过滤
  • 若需要额外过滤仅含空格的属性,可将过滤条件修改为(trim($) != "" and $ != null)

内容的提问来源于stack exchange,提问作者Thiru

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 12:45:04