如何解决Dataweave 中是否存在 XML 标记
我想在 Mule 4 的 Dataweave 2.0 中检查下面的 XML 中是否存在 <price-adjustments>
标记。如何实现?
我正在尝试下面,但它给了我语法错误:
(payload.ns0#order.ns0#"product-lineitems".*ns0#"product-lineitem" map( e,lineindex ) -> {
if(e.price-adjustments != null || e.price-adjustments != "") e."price-adjustments"."base-price" - (e."price-adjustments"."tax"/e."price-adjustments".quantity") else e.ns0#"base-price" - (e.ns0#"tax"/e.ns0#"quantity")
}
XML
<?xml version="1.0" encoding="UTF-8"?>
<order xmlns="http://www.demandware.com/xml/impex/order/2006-10-31" version="20.8" order-no="00002404">
<order-date>2021-03-08T15:20:32.084Z</order-date>
<product-lineitems>
<product-lineitem>
<net-price>487.60</net-price>
</product-lineitem>
<product-lineitem>
<net-price>53.72</net-price>
<price-adjustments>
<price-adjustment>
<net-price>-53.72</net-price>
</price-adjustment>
</price-adjustments>
</product-lineitem>
</product-lineitems>
</order>
解决方法
你可以试试这个:
echo
,
%dw 2.0
output application/json
---
payload.order."product-lineitems".*"product-lineitem"
map( e,lineindex ) ->
if (e.."price-adjustments"? or e.."price-adjustments" != "")
e."price-adjustments"."base-price" - (e."price-adjustments".tax/e."price-adjustments".quantity)
else
e."base-price" - (e.tax/e.quantity)
您可以使用后代选择器和键存在选择器来搜索键是否存在于整个 XML 中。
https://docs.mulesoft.com/mule-runtime/4.3/dataweave-selectors
编辑:在您的代码中,有一些语法错误,例如:
- 使用
||
而不是or
- 在没有
key:value
语法的情况下从地图返回对象(只需删除地图中的 `{})。
上面的脚本不适用于您提供的输入,因为未设置“税费、数量”和“基本价格”,但它不包含语法错误,因此您可以继续工作.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。