首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在dataweave 2.0中,如何组合map和XML属性的过滤器?

在dataweave 2.0中,如何组合map和XML属性的过滤器?
EN

Stack Overflow用户
提问于 2019-05-23 21:06:56
回答 1查看 1K关注 0票数 0

我试图从一个更大的集合中选择某个XML,使用一个过滤器来只获得具有正确属性的那个。

如果没有过滤器,我会接收当前集合中的每一项,但就是想不出过滤器的正确语法……

我的XML-data:

代码语言:javascript
运行
复制
<customer>
  <profile>
    <custom-attributes>
      <custom-attribute attribute-id="customerCredit">0.0</custom-attribute>
      <custom-attribute attribute-id="customerIDS">12345</custom-attribute>
      <custom-attribute attribute-id="sscid">00001</custom-attribute>
    </custom-attributes>
  /<profile>
</customer>

我的数据编织过滤器:

代码语言:javascript
运行
复制
payload.ns0#customers.*ns0#customer map ( customer , indexOfCustomer ) -> {
    (customer.ns0#profile.*ns0#"custom-attributes" filter ($.ns0#"custom-attribute".@"attribute-id" == "customerIDS") map {
     "keys" : $
     }
     )      

对于上面的例子,我希望收到“key: 12345”,但由于过滤器的原因,它被简单地跳过了。

EN

回答 1

Stack Overflow用户

发布于 2019-05-23 23:18:14

您的输入与您的dw脚本不匹配,因此很难判断,没有名称空间,并且缺少"customers“元素。

但是基于你的输入和输出,你可以通过一个过滤器来实现它:

代码语言:javascript
运行
复制
%dw 2.0
output application/json
---
payload.*customer map {
    keys: $.profile."custom-attributes".*"custom-attribute" filter($.@"attribute-id"=="customerIDS")    
}

输出:

代码语言:javascript
运行
复制
[
  {
    "keys": [
      "12345"
    ]
  }
]

或者根据您的示例,也不需要使用map:

代码语言:javascript
运行
复制
%dw 2.0
output application/json
---
keys: payload.customer.profile."custom-attributes".*"custom-attribute" filter($.@"attribute-id"=="customerIDS")

如果您提供您期望的更详细的输入和输出,我们可以提供更多帮助。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56275951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档