如何在Data Weaver中检查空条件:Mule [英] How to Check null condition in Data weaver : Mule

查看:98
本文介绍了如何在Data Weaver中检查空条件:Mule的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here is my xml, in that first I need to check 'RecordsEntries' should not be 'null', then RecordEntry shouldn't be null followed by mapping code

<?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条件检查,但缺少括号或格式正确的地方

Tried multiple scenario's also used when condition checking but somewhere missing parenthesis or correct format

    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条件进行了其他尝试,得到了诸如无法将数组强制转换为布尔值"之类的错误.请提出建议.谢谢.

getting error like missing ). Tried other way around by checking the null condition directly inside the first loop got error like "Cannot coerce array to an boolean". Please suggest. Thanks.

推荐答案

您可以代替

%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对于查询payload.Records.*RecordsEntries.*RecordEntry中的值是空安全的",但是在尝试使用空值(例如null map {})进行操作时会抛出错误.

DataWeave is "null-safe" for querying values like in payload.Records.*RecordsEntries.*RecordEntry, but an error will be thrown when trying to operate with a null (e.g. null map {}).

default运算符将其左侧的值(如果是null)替换为右侧的值.

The default operator replaces the value to its left, if it's null, with the one on the right.

您还缺少*.每当您想要所有重复的元素都匹配时,就需要在xml中使用它.

Also you were missing an *. You need to use it in xml whenever you want all the repetitive elements that match.

这篇关于如何在Data Weaver中检查空条件:Mule的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆