splitBy MuleSoft Dataweave 后如何区分

How to distinctBy after splitBy MuleSoft Dataweave

我有一个我的字段 splitBy,我想通过每次重复仅显示 1 条记录来区分它。例如,如果有 5 个 id = 'abc' 那么它只会在结果中显示 1 个 id = 'abc'。

根据以下示例输出,我如何区分它以仅显示 1 个 ABC123 而不是 2 个 ABC123?

我放置的 distinctBy 完全删除了输出中的 FruitTypeCode 字段,我可以知道导致它的原因吗?

示例输入

<GetListOfCategoriesDetailResponse xmlns="" xmlns:s="">
    <GetListOfCategoriesDetailResult xmlns:a="" xmlns:i="">
        <a:CategoryDetail>
            <a:Category>ABC123 Kook</a:Category>
            <a:CategoryClass>Apple</a:CategoryClass>
            <a:FruitTypeGrouping/>
        </a:CategoryDetail>
        <a:CategoryDetail>
            <a:Category>ABC123 Loop</a:Category>
            <a:CategoryClass>Apple</a:CategoryClass>
            <a:FruitTypeGrouping/>
        </a:CategoryDetail>
        <a:CategoryDetail>
            <a:Category>BCD344 78JL</a:Category>
            <a:CategoryClass>Apple</a:CategoryClass>
            <a:FruitTypeGrouping/>
        </a:CategoryDetail>
    </GetListOfCategoriesDetailResult>
</GetListOfCategoriesDetailResponse>

当前输出

 <ns0:FoodProducts>
     <ns0:FoodProduct>
         <ns0:FruitTypes>
             <ns0:FruitType FruitTypeCode="ABC123" FruitTypeName="Apple" FruitTypeGroup="">
                 <ns0:Descriptions>
                     <ns0:Description>
                         <ns0:Text>This is sweet</ns0:Text>
                     </ns0:Description>
                 </ns0:Descriptions>
             </ns0:FruitType>
             <ns0:FruitType FruitTypeCode="ABC123" FruitTypeName="Apple" FruitTypeGroup="">
                 <ns0:Descriptions>
                     <ns0:Description>
                         <ns0:Text>This is not sweet</ns0:Text>
                     </ns0:Description>
                 </ns0:Descriptions>
             </ns0:FruitType>
             <ns0:FruitType FruitTypeCode="BCD344" FruitTypeName="Apple" FruitTypeGroup="">
                 <ns0:Descriptions>
                     <ns0:Description>
                         <ns0:Text></ns0:Text>
                     </ns0:Description>
                 </ns0:Descriptions>
             </ns0:FruitType>
     </ns0:FoodProduct>
 </ns0:FoodProducts>

预期输出

 <ns0:FoodProducts>
     <ns0:FoodProduct>
         <ns0:FruitTypes>
             <ns0:FruitType FruitTypeCode="ABC123" FruitTypeName="Apple" FruitTypeGroup="">
                 <ns0:Descriptions>
                     <ns0:Description>
                         <ns0:Text>This is sweet</ns0:Text>
                     </ns0:Description>
                 </ns0:Descriptions>
             </ns0:FruitType>
             <ns0:FruitType FruitTypeCode="BCD344" FruitTypeName="Apple" FruitTypeGroup="">
                 <ns0:Descriptions>
                     <ns0:Description>
                         <ns0:Text></ns0:Text>
                     </ns0:Description>
                 </ns0:Descriptions>
             </ns0:FruitType>
     </ns0:FoodProduct>
 </ns0:FoodProducts>

我到目前为止的数据编织代码

%output application/xml
 %namespace ns0 
 %namespace ns01 
 %namespace ns1 
 ---
 {
     ns0#FoodProductRS: {
         ns0#FoodProducts: {
             ns0#FoodProduct: {
                 ns0#FruitTypes: {         
  ((payload.ns01#GetListOfCategoriesDetailResponse.ns01#GetListOfCategoriesDetailResult.*ns1#CategoryDetail filter $.ns1#CategoryClass == "Apple" map ((categoryDetail , indexOfCategoryDetail) -> {
                         ns0#FruitType @(FruitTypeCode: (categoryDetail.ns1#Category splitBy " ")[0], FruitTypeName: categoryDetail.ns1#CategoryClass , FruitTypeGroup: categoryDetail.ns1#FruitTypeGrouping): {
                             ns0#Descriptions: {
                                 ns0#Description: {
                                     ns0#Text: categoryDetail.ns1#LongDescription
                                 }
                             }

                         }
                     })) distinctBy $.ns0#FruitType.@FruitTypeCode)
                 }
             }
         }
     }
 } 

我已经在 mule 3.9.2 中试过你的脚本,它应该可以工作,但遗憾的是你遇到了一个问题(它将出现在 3.9.3 上)。为了解决它,您应该首先制作 distinctBy。

%output application/xml
     %namespace ns0 a
     %namespace ns01 a
     %namespace ns1 a
     ---
     {
         ns0#FoodProductRS: {
             ns0#FoodProducts: {
                 ns0#FoodProduct: {
                     ns0#FruitTypes: {         
                        (
                            (payload.ns01#GetListOfCategoriesDetailResponse.ns01#GetListOfCategoriesDetailResult.*ns1#CategoryDetail 
                                filter $.ns1#CategoryClass == "Apple" 
                                distinctBy ($.ns1#Category splitBy " ")[0]
                                map ((categoryDetail , indexOfCategoryDetail) -> {
                                     ns0#FruitType @(FruitTypeCode: (categoryDetail.ns1#Category splitBy " ")[0], FruitTypeName: categoryDetail.ns1#CategoryClass , FruitTypeGroup: categoryDetail.ns1#FruitTypeGrouping): {
                                         ns0#Descriptions: {
                                             ns0#Description: {
                                                 ns0#Text: categoryDetail.ns1#LongDescription
                                             }
                                         }

                                     }
                                })
                             ) 
                        )
                     }
                 }

您使用的是什么 mule 版本?