Dataweave:如何在输入XML上应用条件过滤器?

时间:2017-05-10 08:22:54

标签: mule anypoint-studio dataweave

我想对输入有效负载应用过滤器。输入是XML,如:

<result>
    <person>
        <id>1</id>
        <zipcode>1111AB </zipcode>
        <housenr>1 </housenr>
    </person>
    <person>
        <id>1</id>
        <zipcode>1111AC </zipcode>
        <housenr>2</housenr>
    </person>
    <person>
        <id>1</id>
        <zipcode>1111AD </zipcode>
        <housenr>3</housenr>
    </person>
    <person>
        <id>1</id>
        <zipcode>1111AB </zipcode>
        <housenr>4</housenr>
    </person>
</result>

最终用户可以搜索邮政编码和可选的房屋。输出json就像:

{
    persons: [{
        id: "1"
        address: "1111AB 1"
    },
    { 
        id: "2",
        address: "1111AC 1"
    }
    ]
}

所以我有想法过滤这样的地图:

{
persons: payload.result.*person filter ($.zipcode == flowVars.zipcode) map{
id: $.id,
address: $.zipcode ++ $.housenr
}

我想知道是否可以使该过滤器有条件。例如,如果flowVars Zipcode和Housnr为null,则不应应用过滤器。或者如果Zipcode已填满,但housnr没有,则只应用zipcode过滤器。

欢迎帮助!

感谢

2 个答案:

答案 0 :(得分:0)

这是您的数据编织的样子:

%dw 1.0
%output application/json
---
{
    persons: payload.result.*person filter flowVars.zipcode == null or (trim $.zipcode) == flowVars.zipcode map {
        id: $.id,
        address: $.zipcode ++ " " ++ $.housenr
    }
}

注意:我将修剪添加到$ .zipcode,因为您的示例XML在zipcode中包含空格。

答案 1 :(得分:0)

  {
  persons: using (personFilter = (payload.result.*person filter $.zipcode == flowVars.zipcode when flowVars.zipcode != null 
  otherwise payload.result.*person) )
  personFilter filter $.housenr == flowVars.housenr when flowVars.housenr != null otherwise (payload.result.*person filter $.zipcode == flowVars.zipcode when flowVars.zipcode != null 
  otherwise payload.result.*person)
   map {
  id: $.id,
  address: $.zipcode ++ $.housenr
      }  
  }