如何在 Mulesoft Dataweave 中删除 XML 命名空间

How to remove XML namespace in Mulesoft Dataweave

当我使用 Mulesoft dataweave 转换消息时,我想摆脱所有 xmlns 命名空间。

这是我的留言:

<?xml version='1.0' encoding='UTF-8'?>
<Entries xmlns="http://www.example.nl/Entries" type="Catalog" name="xxx Catalog" exportDate="2018-01-18T10:08:27.609Z">
  <Entry id="264063" deleted="0" creationDate="2017-05-26T14:26:09.511Z" lastModifiedDate="2017-10-13T22:46:39.000Z">
    <Attributes>
      <Attribute>
        <MetadataPath>Just an example 1</MetadataPath>
        <Locale/>
      </Attribute>
      <Attribute>
        <MetadataPath>Just an example 2</MetadataPath>
        <Locale>en_GB</Locale>
      </Attribute>
    </Attributes>
    <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>
</Entries>

当我运行这个数据编织代码时:

%dw 2.0
var x = payload.Entries
output application/xml encoding="utf-8"
---

{
   Entry @('type': x.@'type', name: x.@name, exportDate: x.@exportDate, id: x.Entry.@id, 
  deleted: x.Entry.@deleted, creationDate: x.Entry.@creationDate, lastModifiedDate:    x.Entry.@lastModifiedDate )
    : {(x.Entry.Attributes ), 
    (
       Categories: (x.Entry.Categories)
    ),
    (
      Specs:  (x.Entry.Specs)
    )
    }
    
    
}

那么就是这个结果

<?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 xmlns="http://www.example.nl/Entries">
    <MetadataPath>Just an example 1</MetadataPath>
    <Locale/>
  </Attribute>
  <Attribute xmlns="http://www.example.nl/Entries">
    <MetadataPath>Just an example 2</MetadataPath>
    <Locale>en_GB</Locale>
  </Attribute>
  <Categories>
    <Category xmlns="http://www.example.nl/Entries">
      <Hierarchy>GPC_Hierarchy</Hierarchy>
      <Id>999999</Id>
    </Category>
    <Category xmlns="http://www.example.nl/Entries">
      <Hierarchy>GPC_xx_Hierarchy</Hierarchy>
      <Id>999998</Id>
    </Category>
  </Categories>
  <Specs>
    <Spec xmlns="http://www.example.nl/Entries">Validatie Spec</Spec>
    <Spec xmlns="http://www.example.nl/Entries">Item Spec</Spec>
  </Specs>
</Entry>

如何摆脱所有这些 xmlns="http://www.example.nl/Entries" 命名空间?

当然我可以重写消息,但这不是我的意图。 Mabye dataweave 中有这样的标签吗?

output application/xml removeAllNamespaces

谢谢

您可以尝试下面符合您期望的代码。 XML 没有直接作者 属性 来删除所有名称空间。

%dw 2.0
var x = payload.Entries

fun removeALLNameSpacesFromXML(in) =
  in mapObject {
    '$$' @(($$.@)): 
      if ($ is Object)
        removeALLNameSpacesFromXML($)
      else
        ($)
  }
  //This is your transormation code ,I have just assigned with some variable to pass as a Function argument of removeALLNameSpacesFromXML function. 
var yourPayloadWithXmlns = {
  Entry @('type': x.@'type', name: x.@name, exportDate: x.@exportDate, id: x.Entry.@id, deleted: x.Entry.@deleted, creationDate: x.Entry.@creationDate, lastModifiedDate: x.Entry.@lastModifiedDate): {
    (x.Entry.Attributes),
    (
      Categories: (x.Entry.Categories)
    ),
    (
      Specs: (x.Entry.Specs)
    )
  }
}
output application/xml  
---
removeALLNameSpacesFromXML(yourPayloadWithXmlns)

示例输出:

<?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>