xml - 如何在 Mulesoft Dataweave 中删除 XML 命名空间
问题描述
当我使用 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 没有直接的 writer 属性来删除所有的命名空间。
%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>
推荐阅读
- python - 为什么python首先打印出类方法?
- bamboo - 任何优先/更新竹构建队列的 REST 端点?
- c++ - 等待线程还是创建新线程?
- groovy - 在 Groovy 中未调用 Vert.x Future/Promise 处理程序
- android - 在公司防火墙后面接收 Firebase 通知(使用 NAT)
- sql-server - MyBatis 可调用 SQL 临时变量
- sql - SQL 中各行的总和
- c - 如何在事先不知道长度的情况下要求用户输入?在 C 中
- regex - 如何正确运行 find | 与 grep + 转义字符平行?
- node.js - 如何提取带有转义字符的 JSON 对象的参数 - '\'