问题描述
当我使用 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>