You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mule 4中XML转换映射ID参数的DataWeave过滤逻辑求助

Mule 4 DataWeave XML转换:ID参数映射过滤逻辑修正

需要为account_work匹配type="Revenue_category_id"的ID值,为ledger_work匹配type="Ledger_category_id"的ID值,修正后的DataWeave代码及相关说明如下:

原DataWeave代码

%dw 2.0
output application/json
---
payload.Get_Mapping_Tables_Response.Response_Data.Mapping_Table.Mapping_Table_Data.*Rule_Table_Data map {
  "account_work": $.Accounting_working.ID, // need to add filer logic to get Revenue_category_id
  "ledger_work": $.Ledger_working.ID // need to add filer logic to get Ledger_category_id
}

请求XML

<wd:Get_Mapping_Tables_Response
xmlns:wd="urn:com.workday/bsve" wd:version="v40.0">
<wd:Response_Data>
    <wd:Mapping_Table>
        <wd:Mapping_Table_Data>
            <wd:ID>AC_Account_Map</wd:ID>
            <wd:Rule_Table_Data>
                <wd:Accounting_working>
                    <wd:ID wd:type="WID">1234</wd:ID>
                    <wd:ID wd:type="Revenue_category_id">RC_1</wd:ID>
                </wd:Accounting_working>
                <wd:Ledger_working>
                    <wd:ID wd:type="WID">12345</wd:ID>
                    <wd:ID wd:type="Ledger_category_id">LC_1</wd:ID>
                </wd:Ledger_working>
            </wd:Rule_Table_Data>
            <wd:Rule_Table_Data>
                <wd:Accounting_working>
                    <wd:ID wd:type="WID">12346</wd:ID>
                    <wd:ID wd:type="Revenue_category_id">RC_2</wd:ID>
                </wd:Accounting_working>
                <wd:Ledger_working>
                    <wd:ID wd:type="WID">12347</wd:ID>
                    <wd:ID wd:type="Ledger_category_id1">LC_2</wd:ID>
                </wd:Ledger_working>
            </wd:Rule_Table_Data>
        </wd:Mapping_Table_Data>
    </wd:Mapping_Table>
</wd:Response_Data>
</wd:Get_Mapping_Tables_Response>

预期响应

[
    {
        "account_work": "RC_1",
        "ledger_work": "LC_1"
    },
    {
        "account_work": "RC_2",
        "ledger_work": ""
    }
]

修正后的DataWeave代码

%dw 2.0
output application/json
ns wd urn:com.workday/bsve
---
payload.Get_Mapping_Tables_Response.Response_Data.Mapping_Table.Mapping_Table_Data.*Rule_Table_Data map {
    "account_work": ($.Accounting_working.ID filter ($.@wd:type == "Revenue_category_id"))[0] default "",
    "ledger_work": ($.Ledger_working.ID filter ($.@wd:type == "Ledger_category_id"))[0] default ""
}

逻辑说明

  1. 命名空间声明:添加ns wd urn:com.workday/bsve,确保能正确识别XML中带wd前缀的命名空间属性。
  2. 属性过滤:通过filter方法筛选出@wd:type属性与目标值匹配的ID节点,精准获取所需ID。
  3. 空值处理:使用[0] default ""取筛选后的第一个结果,若没有匹配项则返回空字符串,避免输出null值。

内容的提问来源于stack exchange,提问作者Devendra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 00:20:18