搜索 XML 属性并将其分组

Search an XML attrbiute and Group it

我需要搜索一个 XML 属性值,将其分组并形成相应的 JSON 元素。

现在举例:Label 是我的 AttributeID,我有 2 个属性 Label。 (实时 - 可以是 'n' 次。) 需要将 2 个标签分组为单个 JSON 元素。

VendorClass、VendorDivision 和 VendorDept 都应包含在 Vendor Json 属性中。 VendorClass attributeValue 应映射到 Vendor 内的 classCode,其他 2 个代码应为 null。 同样,VendorDept attributeValue 应映射到 DeptCode。VedorDivision 属性值映射到 DivisionCode。

O/PJSON(映射): type - 应该总是大写字母 - 映射到 attributeID, 名称 - 小写字母 - 映射到“attributeId_attributeValue”, id/code - 是 attribute_value

下面是我的i/pXML

<CurrentRule Currency="USD"
    CurrentStatus="ACTIVE"
    Priority="0"
    RuleCategory="Current"
    RuleType="COMBINATION"
    >
    <CurrentRuleTargetAttributeValueList/>
    <CurrentRuleAttributeValueList>
        <CurrentRuleAttributeValue 
            TriggerAttributeID="Label"
            TriggerAttributeValue="10"/>
        <CurrentRuleAttributeValue 
            TriggerAttributeID="Label"
            TriggerAttributeValue="1003"/>
        <CurrentRuleAttributeValue 
        TriggerAttributeID="ABCDCode"
            TriggerAttributeValue="AC"/>
        <CurrentRuleAttributeValue 
            TriggerAttributeID="ABCDCode"
            TriggerAttributeValue="FD"/>
        <CurrentRuleAttributeValue 
            TriggerAttributeID="VendorClass"
            TriggerAttributeValue="00N"/>
        <CurrentRuleAttributeValue
            TriggerAttributeID="VendorDept"
            TriggerAttributeValue="100"/>
        <CurrentRuleAttributeValue
            TriggerAttributeID="VendorDivision"
            TriggerAttributeValue="10"/>
        <CurrentRuleAttributeValue 
            TriggerAttributeID="VendorMarket"
            TriggerAttributeValue="QVC"/>
        <CurrentRuleAttributeValue
            TriggerAttributeID="PriceCode"
            TriggerAttributeValue="FP"/>
        <CurrentRuleAttributeValue 
            TriggerAttributeID="ProductNumber"
            TriggerAttributeValue="A0000"/>
        <CurrentRuleAttributeValue
            TriggerAttributeID="Trader"
            TriggerAttributeValue="1010"/>
        <CurrentRuleAttributeValue
            TriggerAttributeID="Trader"
            TriggerAttributeValue="1046"/>
    </CurrentRuleAttributeValueList>
</CurrentRule>

预期 JSON o/p:

{
    "header": {
        "type": "ORDER",
        "name": "merch-attribute-example",
        "createUser": "admin"
    },
    "filters": {
        "Labels": [
            {
                "type": "LABEL",
                "name": "Label_10",
                "LabelId": 10
            },
            {
                "type": "LABEL",
                "name": "Label_1003",
                "LabelId": 1003
            }
        ],
        "vendor": [
            {
                "type": "VENDOR",
                "name": "vendorclass_00n",
                "divisionCode": null,
                "departmentCode": null,
                "classCode": "00N"
            },
            {
                "type": "VENDOR",
                "name": "vendordept_100",
                "divisionCode": null,
                "departmentCode": "100",
                "classCode": null
            },
            {
                "type": "VENDOR",
                "name": "vendordivision_10",
                "divisionCode": "10",
                "departmentCode": null,
                "classCode": null
            }
        ],
        "abcd": [
            {
                "type": "ABCD",
                "name": "abcdcode_ac",
                "abcdCode": "AC"
            },
            {
                "type": "ABCD",
                "name": "abcdcode_fd",
                "abcdCode": "FD"
            }
        ],
        "priceCodes": [
            {
                "type": "PRICE_CODE",
                "name": "pricecode_fp",
                "code": "FP"
            }
        ],
        "products": [
            {
                "type": "PRODUCT",
                "name": "productnumber_a0000",
                "productNumbers": [
                    "A0000"
                ]
            }
        ],
        "traders": [
            {
                "type": "TRADER",
                "name": "trader_1010",
                "vendorCode": "1010"
            },
            {
                "type": "TRADER",
                "name": "trader_1046",
                "vendorCode": "1046"
            }
        ]
    }
}
enter code here

根据描述的规则,这是最佳近似值。我将留给您如何确定它是 id 还是 code 以及未明确解释的其余输出。

%dw 2.0
output application/json
---
{
    header: {
        "type": "ORDER",
        "name": "merch-attribute-example",
        "createUser": "admin"
    },
    filters: payload.CurrentRule.CurrentRuleAttributeValueList 
        mapObject ((value, key, index) -> 
            {
                attr: key.@
            }
        ) 
        pluck (($$):$)
        groupBy ((item, index) -> item.attr.TriggerAttributeID)

        mapObject ((value1, key1, index1) -> 
            (key1): value1 map 
                {
                    "type": upper(key1), 
                    name: lower(key1) ++ "_" ++ $.attr.TriggerAttributeValue,
                    id: $.attr.TriggerAttributeValue
                }
        ) 

}

输出:

{
  "header": {
    "type": "ORDER",
    "name": "merch-attribute-example",
    "createUser": "admin"
  },
  "filters": {
    "Label": [
      {
        "type": "LABEL",
        "name": "label_10",
        "id": "10"
      },
      {
        "type": "LABEL",
        "name": "label_1003",
        "id": "1003"
      }
    ],
    "ABCDCode": [
      {
        "type": "ABCDCODE",
        "name": "abcdcode_AC",
        "id": "AC"
      },
      {
        "type": "ABCDCODE",
        "name": "abcdcode_FD",
        "id": "FD"
      }
    ],
    "VendorClass": [
      {
        "type": "VENDORCLASS",
        "name": "vendorclass_00N",
        "id": "00N"
      }
    ],
    "VendorDept": [
      {
        "type": "VENDORDEPT",
        "name": "vendordept_100",
        "id": "100"
      }
    ],
    "VendorDivision": [
      {
        "type": "VENDORDIVISION",
        "name": "vendordivision_10",
        "id": "10"
      }
    ],
    "VendorMarket": [
      {
        "type": "VENDORMARKET",
        "name": "vendormarket_QVC",
        "id": "QVC"
      }
    ],
    "PriceCode": [
      {
        "type": "PRICECODE",
        "name": "pricecode_FP",
        "id": "FP"
      }
    ],
    "ProductNumber": [
      {
        "type": "PRODUCTNUMBER",
        "name": "productnumber_A0000",
        "id": "A0000"
      }
    ],
    "Trader": [
      {
        "type": "TRADER",
        "name": "trader_1010",
        "id": "1010"
      },
      {
        "type": "TRADER",
        "name": "trader_1046",
        "id": "1046"
      }
    ]
  }
}