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

标签 xml mule dataweave mulesoft

当我使用 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>

当我运行此 dataweave 代码时:

%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"命名空间?

当然,我可以重写该消息,但这不是我的意图。 也许 dataweave 中有某种类似这样的标签?

输出 application/xml removeAllNamespaces

谢谢

最佳答案

您可以尝试下面的代码,它符合您的期望。 XML 没有直接的 writer 属性来删除所有 namespace 。

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

关于xml - 如何在 Mulesoft Dataweave 中删除 XML 命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65371374/

相关文章:

xml - 如何使用 xmlstarlet 选择多个同名元素?

c++ - 克隆rapidxml::xml_document

java - 在 Mule 中测试子流

mule - 如何设置 Mule 消息的出站属性

Mule 4 attribute.maskedRequestPath 预期行为

mule - 访问多部分请求中的多个文件

xml - 使用 WRITE-XML 语句通过 Progress 12 创建 XML 文件

c# - 转义 xml 中的特殊字符

mule - 如何检查在 mule 的请求配置中传递了什么 header

anypoint-studio - 如何使用 Dataweave 表示此响应?