【问题标题】:XML Tag exist or not in DataweaveDataweave 中是否存在 XML 标签
【发布时间】:2021-03-10 05:40:31
【问题描述】:

我想在 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>

【问题讨论】:

  • 您是要检查任何产品线中是否存在价格调整,还是需要检查每个产品线中是否存在上述字段?
  • 针对每个产品系列

标签: dataweave mulesoft mule4


【解决方案1】:

你可以试试这个:

%dw 2.0
output application/json
---
payload..*"product-lineitem" map {
            "calcResult($$)": if($.."price-adjustment"?) "some calc" else "some other calc"
        }

【讨论】:

    【解决方案2】:
    %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)
    

    您可以使用后代选择器和 Key present 选择器来搜索键是否存在于整个 XML 中。

    https://docs.mulesoft.com/mule-runtime/4.3/dataweave-selectors

    编辑:在您的代码中,有一些语法错误,例如:

    • 使用|| 代替or
    • 不使用 key:value 语法从地图返回对象(只需删除地图中的 `{})。

    上面的脚本不适用于您提供的输入,因为未设置“税”、“数量”和“基本价格”,但它不包含语法错误,因此您可以继续工作.

    【讨论】:

    • if-else 条件下如何工作?
    • 我用我正在尝试的内容更新了我的问题。
    猜你喜欢
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多