Martin Cousin
Martin Cousin

Reputation: 25

How to combine map and a filter for an XML-attribute in dataweave 2.0?

I trying to select a certain XML out fo a larger collection, using a filter to get only the one with the right attribute.

Without the filter I receive every last item in the current collection, but just can't come up with the correct syntax for the filter...

My XML-data:

<customer>
  <profile>
    <custom-attributes>
      <custom-attribute attribute-id="customerCredit">0.0</custom-attribute>
      <custom-attribute attribute-id="customerIDS">12345</custom-attribute>
      <custom-attribute attribute-id="sscid">00001</custom-attribute>
    </custom-attributes>
  /<profile>
</customer>

My dataweave filter:

payload.ns0#customers.*ns0#customer map ( customer , indexOfCustomer ) -> {
    (customer.ns0#profile.*ns0#"custom-attributes" filter ($.ns0#"custom-attribute".@"attribute-id" == "customerIDS") map {
     "keys" : $
     }
     )      

I would like to receive "keys: 12345" for the example above, but due to the filter it's simply skipped.

Upvotes: 0

Views: 1650

Answers (1)

Ryan Carter
Ryan Carter

Reputation: 11606

Your input doesnt match your dw script so it's hard to tell, there is no namespaces and "customers" element is missing.

But based on your input and your output you can achieve it via just a filter:

%dw 2.0
output application/json
---
payload.*customer map {
    keys: $.profile."custom-attributes".*"custom-attribute" filter($.@"attribute-id"=="customerIDS")    
}

Outputs:

[
  {
    "keys": [
      "12345"
    ]
  }
]

Or based on your example, no need to use map either:

%dw 2.0
output application/json
---
keys: payload.customer.profile."custom-attributes".*"custom-attribute" filter($.@"attribute-id"=="customerIDS")

If you provide a more detailed input and output you expect we can help more.

Upvotes: 2

Related Questions