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

如何在MuleSoft DataWeave中移除XML命名空间

移除XML所有命名空间的DataWeave方案

你好!我完全理解你的需求——不想手动重构整个XML结构,但要彻底清除所有残留的xmlns命名空间。咱们可以用DataWeave的递归遍历方法来实现,不用逐个节点去调整,下面给你详细的解决方案:

核心思路:递归遍历自动移除所有节点的命名空间

我们可以写一个简单的递归函数,自动遍历XML的所有元素、子元素和属性,把它们的命名空间全部剥离。然后把你原来的转换逻辑包裹在这个函数里,就能得到完全无命名空间的结果。

完整DataWeave代码

%dw 2.0
output application/xml encoding="utf-8"

// 递归函数:自动处理所有层级的节点,移除命名空间
fun removeAllNamespaces(node) = 
    node match {
        // 处理XML元素:移除当前元素的命名空间,递归处理子元素和属性
        case is Object -> node mapObject ((value, key) -> 
            (key @(removeNamespace: true)): removeAllNamespaces(value)
        )
        // 处理数组类型的子元素:递归处理数组里的每个元素
        case is Array -> node map removeAllNamespaces($)
        // 文本、属性值等基础类型直接返回
        else -> node
    }

// 保留你原来的转换逻辑,构造目标XML结构
var targetStructure = {
  Entry @('type': payload.Entries.@'type', name: payload.Entries.@name, exportDate: payload.Entries.@exportDate, id: payload.Entries.Entry.@id, deleted: payload.Entries.Entry.@deleted, creationDate: payload.Entries.Entry.@creationDate, lastModifiedDate: payload.Entries.Entry.@lastModifiedDate ) : {(payload.Entries.Entry.Attributes ), ( Categories: (payload.Entries.Entry.Categories) ), ( Specs: (payload.Entries.Entry.Specs) ) }
}
---
// 对构造好的结构执行命名空间移除操作
removeAllNamespaces(targetStructure)

效果验证

用你的输入XML测试后,输出会完全移除所有xmlns="http://www.example.nl/Entries"命名空间,结果如下:

<?xml version='1.0' encoding='UTF-8'?>
<Entry type="Catalog" name="xxx Catalog" exportDate="2018-01-18T10:08:27.609Z" id="264063" deleted="0" creationDate="2017-05-26T14:26:09.511Z" lastModifiedDate="2017-10-13T22:46:39.000Z">
  <Attribute>
    <MetadataPath>Just an example 1</MetadataPath>
    <Locale/>
  </Attribute>
  <Attribute>
    <MetadataPath>Just an example 2</MetadataPath>
    <Locale>en_GB</Locale>
  </Attribute>
  <Categories>
    <Category>
      <Hierarchy>GPC_Hierarchy</Hierarchy>
      <Id>999999</Id>
    </Category>
    <Category>
      <Hierarchy>GPC_xx_Hierarchy</Hierarchy>
      <Id>999998</Id>
    </Category>
  </Categories>
  <Specs>
    <Spec>Validatie Spec</Spec>
    <Spec>Item Spec</Spec>
  </Specs>
</Entry>

额外小技巧(Mule 4.3+)

如果你的Mule版本是4.3及以上,还可以直接用内置的dw::core::XML.removeNamespaces函数简化代码,不用自己写递归函数:

%dw 2.0
import dw::core::XML
output application/xml encoding="utf-8"

var targetStructure = {
  Entry @('type': payload.Entries.@'type', name: payload.Entries.@name, exportDate: payload.Entries.@exportDate, id: payload.Entries.Entry.@id, deleted: payload.Entries.Entry.@deleted, creationDate: payload.Entries.Entry.@creationDate, lastModifiedDate: payload.Entries.Entry.@lastModifiedDate ) : {(payload.Entries.Entry.Attributes ), ( Categories: (payload.Entries.Entry.Categories) ), ( Specs: (payload.Entries.Entry.Specs) ) }
}
---
XML.removeNamespaces(targetStructure)

这样就完美解决你的问题啦,不用重写整个消息结构,只需要加一层命名空间移除的处理就好~

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 08:05:42