如何在Data weaver中检查null条件:Mule

时间:2015-09-08 00:45:59

标签: mule mule-studio mule-component mule-el

这是我的xml,首先我需要检查'RecordsEntries'不应该是'null',然后RecordEntry不应该是null后跟映射代码

<?xml version="1.0" encoding="UTF-8"?>
<Records>
  <storenumber />
  <calculated>false</calculated>
  <subTotal>12</subTotal>
 <RecordsEntries>
   <RecordEntry>
     <deliverycharge>30.0</deliverycharge>
     <entryNumber>8</entryNumber>
     <Value>true</Value>
  </RecordEntry>
  <RecordEntry>
    <deliverycharge>20.0</deliverycharge>
    <entryNumber>7</entryNumber>
    <Value>false</Value>
  </RecordEntry>
</RecordsEntries>
 <RecordsEntries>
    <RecordEntry>
      <deliverycharge>30.0</deliverycharge>
      <entryNumber>8</entryNumber>
      <Value>false</Value>
    </RecordEntry>
 </RecordsEntries>
</Records>

尝试多个场景也使用when条件检查,但某处缺少括号或格式正确

    orders: {
    order: {
  StoreID: payload.Records.storenumber,
  Total: payload.Records.calculated,
 (( payload.Records.RecordsEntries.*RecordEntry ) map {
    IndividualEntry: {
    Number:$.entryNumber,
    DeliverCharge:$.deliverycharge
    }
  } when ( payload.Records.RecordsEntries != null and payload.Records.RecordsEntries.*RecordEntry !=null))}
}

收到错误消失)。通过在第一个循环内直接检查null条件来尝试其他方式得到了错误,例如“无法将数组强制转换为布尔值”。请建议。感谢。

2 个答案:

答案 0 :(得分:6)

你可以改为

%dw 1.0
%output application/xml
---
orders: {
  order: {
  StoreID: payload.Records.storenumber,
  Total: payload.calculated,
 ((payload.Records.*RecordsEntries.*RecordEntry default []) map {
      IndividualEntry: {
        Number:$.entryNumber,
        DeliverCharge:$.deliverycharge
      }
    })
  }
}

DataWeave是&#34; null-safe&#34;查询payload.Records.*RecordsEntries.*RecordEntry中的值,但尝试使用null(例如null map {})时会抛出错误。

default运算符将值替换为左侧的值,如果它是null,则将值替换为右侧的值。

你也错过了*。只要你想要所有匹配的重复元素,就需要在xml中使用它。

答案 1 :(得分:0)

如果要基于null或空白跳过dataweave中的特定字段分配,可以使用下面提到的脚本。

有效负载映射((payload01,indexOfPayload01) - &gt; {名称:payload01.Name,(LastName:payload01.LastName)当payload01.LastName!= null和payload01.LastName!=''})

如果名称字段在传入的有效负载中显示为空或空白,则会跳过Name的字段映射。