dataweave - Mule-Dataweave 转换问题
问题描述
在为以下有效负载编写数据编织表达式时,我看到以下错误。如何解决这个问题(所有 XML 格式)?
XML 文件:
<Interactions
xmlns="urn:astrazeneca:na:Activity:domain:3"
xmlns:ns0="urn:astrazeneca:na:Activity:domain:3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
SchemaVersion="3.1">
<ns0:Interaction>
<InteractionDetails CreatedOnDate="2020-01-07T00:40:38"
RecordCompanyCode="AZN"
RestrictionGroup="NONE"
UpdatedOnDate="2020-01-07T00:40:39">
<StartDate>2020-01-07T00:40:18</StartDate>
<EndDate>2020-01-07T00:40:18</EndDate>
<Location xsi:type="LocationAddress">
<AddressLine LineNo="1">6089 N 1ST ST STE 102</AddressLine>
<CityName>FRESNO</CityName>
</Location>
<RelatedInteraction>
<RelationshipType>is_a_child_of</RelationshipType>
</RelatedInteraction>
</InteractionDetails>
</ns0:Interaction>
</Interactions>
表达:
%dw 2.0
output application/xml
---
"Interactions" : payload.Interactions mapObject {
"Interaction" : $ mapObject {
"InteractionDetails" : $ mapObject {
"Location" : $ - "AddressLine"
}
}
}
错误:
You called the function '-' with these arguments:
1: String ("2020-01-07T00:40:18")
2: String ("AddressLine")
解决方案
假设您要删除给定元素的所有出现,您可以将最终解决方案基于以下 DataWeave 表达式:
%dw 2.0
fun removeElements(element,elements) =
element mapObject (value, key) -> {
((key):
if (value is Object)
removeElements(
if (elements is Array) value -- elements //if elements is an array
else value -- (elements splitBy ','), elements) //if elements is a string with comma separated elements
else value)
}
output application/xml
---
// All three options will be accepted by the removeElements function
removeElements(payload, "AddressLine")
//removeElements(payload, ["AddressLine", "CityName"])
//removeElements(payload, "AddressLine,CityName")
以下输入:
<Interactions
xmlns="urn:astrazeneca:na:Activity:domain:3"
xmlns:ns0="urn:astrazeneca:na:Activity:domain:3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
SchemaVersion="3.1">
<ns0:Interaction>
<InteractionDetails CreatedOnDate="2020-01-07T00:40:38"
RecordCompanyCode="AZN"
RestrictionGroup="NONE"
UpdatedOnDate="2020-01-07T00:40:39">
<StartDate>2020-01-07T00:40:18</StartDate>
<EndDate>2020-01-07T00:40:18</EndDate>
<Location xsi:type="LocationAddress">
<AddressLine LineNo="1">6089 N 1ST ST STE 102</AddressLine>
<CityName>FRESNO</CityName>
</Location>
<RelatedInteraction>
<RelationshipType>is_a_child_of</RelationshipType>
</RelatedInteraction>
</InteractionDetails>
</ns0:Interaction>
</Interactions>
将产生以下输出:
<?xml version='1.0' encoding='UTF-8'?>
<Interactions xmlns="urn:astrazeneca:na:Activity:domain:3" SchemaVersion="3.1">
<ns0:Interaction xmlns:ns0="urn:astrazeneca:na:Activity:domain:3">
<ns0:InteractionDetails CreatedOnDate="2020-01-07T00:40:38" RecordCompanyCode="AZN" RestrictionGroup="NONE" UpdatedOnDate="2020-01-07T00:40:39">
<ns0:StartDate>2020-01-07T00:40:18</ns0:StartDate>
<ns0:EndDate>2020-01-07T00:40:18</ns0:EndDate>
<ns0:Location xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="LocationAddress">
<ns0:CityName>FRESNO</ns0:CityName>
</ns0:Location>
<ns0:RelatedInteraction>
<ns0:RelationshipType>is_a_child_of</ns0:RelationshipType>
</ns0:RelatedInteraction>
</ns0:InteractionDetails>
</ns0:Interaction>
</Interactions>
推荐阅读
- reactjs - Reactjs 电子邮件框架
- google-apps-script - 您可以在 gas 中创建电子表格并获取 ID 和 getActive 吗?
- sql - 用于连接子句的 TypeORM 多重条件
- python - Pip 无法使用 Python 3.8.5 安装 PyTorch
- c - 打印结构指针 c
- c# - Neo4jClient中yield后对Nodetypes进行排序
- swift - 输入后关闭键盘
- java - 如何使用 Streams 从 Java Selenium 的下拉列表中选择一个元素
- r - 使用 MatchIT 进行 R 倾向得分匹配
- c# - ASP.Net (2.2) WebApp 连接到 Azure B2C 失败