【问题标题】:Dataweave to select exact tag from XML MulesoftDataweave 从 XML Mulesoft 中选择准确的标签
【发布时间】:2020-07-10 23:09:06
【问题描述】:

从下面的 XML 中获取“0011x000014VegoAAC”的正确 Dataweave 表达式是什么?

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<order order-no="00000907" xmlns="http://www.demandware.com/xml/impex/order/2006-10-31">
    <order-date>2020-07-10T08:57:05.076Z</order-date>
    <current-order-no>00000907</current-order-no>
    <product-lineitems>
        <product-lineitem>
            <net-price>54.17</net-price>
        </product-lineitem>
    </product-lineitems>
    <custom-attributes>
        <custom-attribute attribute-id="Adyen_pspReference">852594371442812G</custom-attribute>
        <custom-attribute attribute-id="Adyen_value">7099</custom-attribute>
        <custom-attribute attribute-id="sscAccountid">0011x000014VegoAAC</custom-attribute>
    </custom-attributes>
</order>

【问题讨论】:

    标签: dataweave mulesoft


    【解决方案1】:

    自定义属性看起来像标准标签。将其包装到引号中,它将正常工作。 custom-attribute 是一个数组,在 DW 转换中用星号表示。从这个数组(第 19 行创建它)中,我们过滤所有属性(由 @ 字符表示)等于 sscAccountId 的项目。

    结果是一个元素的数组。

    %dw 2.0
    var payload =read('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <order order-no="00000907" xmlns="http://www.demandware.com/xml/impex/order/2006-10-31">
        <order-date>2020-07-10T08:57:05.076Z</order-date>
        <current-order-no>00000907</current-order-no>
        <product-lineitems>
            <product-lineitem>
                <net-price>54.17</net-price>
            </product-lineitem>
        </product-lineitems>
        <custom-attributes>
            <custom-attribute attribute-id="Adyen_pspReference">852594371442812G</custom-attribute>
            <custom-attribute attribute-id="Adyen_value">7099</custom-attribute>
            <custom-attribute attribute-id="sscAccountid">0011x000014VegoAAC</custom-attribute>
        </custom-attributes>
    </order>','application/xml')
    output application/json
    ---
    (payload.order."custom-attributes".*"custom-attribute") 
     filter (item) -> (item.@"attribute-id" ~= "sscAccountid")
    

    【讨论】:

    • 谢谢@Alex,显然我一步一步接近你的答案,但出现错误:无效状态代码:400,响应正文:[{“message”:“无法反序列化来自 START_ARRAY 值的引用实例 [ line:1, column:423]","errorCode":"JSON_PARSER_ERROR"}] 一定很蠢,我的代码:%dw 2.0 output application/java ns ns0 demandware.com/xml/impex/order/2006-10-31 --- [{ PBSI__Customer__c: (payload.ns0 #order.ns0#"custom-attributes".*ns0#"custom-attribute") 过滤器 (item) -> (item.@"attribute-id" == "sscAccountid") }]
    • 这不是 DataWeave 错误。这可能是其他一些问题。你应该为它打开一个新问题。
    • @Alex - 如何从我想要 0011x000014VegoAAC 而不是 [0011x000014VegoAAC] 的响应中删除那些方括号
    • 这是数组。如果您只想要第一项 - 使用索引 0。喜欢 ((payload.order."custom-attributes".*"custom-attribute") filter (item) -> (item.@"attribute-id" ~= "sscAccountid "))[0]
    猜你喜欢
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多