1

Going through the Dataweaver documentaion Link:https://developer.mulesoft.com/docs/dataweave#_attribute_selector_expressions

Section 3.4 Key Present Trying out the example provide below .

Input:
       <users>
     <name>Mariano</name>
       <name>Luis</name>
        <name>Mariano</name>
  </users>

Transform:

        %dw 1.0
      %input payload application/xml   
      %output application/xml
       ---
       users: payload.users.name[?($ == "Mariano")]

when I try to give this expression in my DataWeaver it gives warning like cannot coerce a:string to a: array:(7,92). Have given the same way mentioned in the document. Could anyone please advice.

Expected Response:

           <?xml version="1.0" encoding="UTF-8"?>
          <users>
          <name>Mariano</name>
          <name>Mariano</name>
          </users>

Also in the document 1.1.2 string manipulation example wasn't working for me

        %dw 1.0
        %input payload application/xml
        %output application/json
         %function words(name) name splitBy " "
         ---
       contacts: payload.users.*user map using (parts =  words($.name)){
        firstName: parts[0],
        (secondName: parts[1]) when (sizeOf parts) > 2,
        lastName: parts[-1],
        email: "$((lower $.name) replace " " with ".")@acme.com.ar",
      address: $.street
         }

showing error like multiple marker at this line missing '}' no viable alternative at input email

Started learning and working out the examples provided. Thanks.

DavoCoder
  • 862
  • 6
  • 17
star
  • 1,493
  • 1
  • 28
  • 61

1 Answers1

1

The example in the docs has a typo, there is an * missing before name (it should be fixed):

%dw 1.0
%input payload application/xml
%output application/xml
---
users: payload.users.*name[?($ == "Mariano")]

The problem is that XML doesn't have a built-in list representation, so the list is represented by multiple occurences of a tag. The expression *name returns a list with the occurrences of name, the expression [?($ == "Mariano")] it's like a filter (I prefer filter since it is easier to understand).

The cryptic error message appears because the operator applies to a list, but payload.users.name returns the first appeareance of name. (That's why it says cannot coerce string to array).

Diego
  • 4,353
  • 1
  • 24
  • 22