0
votes

I have below xml and need to transform it using mule data weave transformation.

<Colleges>
  <College>
    <name>C1</name>
    <Students>
      <Student>
        <name>s1</name>
        <dept>d1</dept>
      </Student>
      <Student>
        <name>s2</name>
        <dept>d2</dept>
      </Student>
      <Student>
        <name>s3</name>
        <dept>d3</dept>
      </Student>
      <Student>
        <name>s4</name>
        <dept>d3</dept>
      </Student>
    </Students>
  </College>
</Colleges>

I want to transform it to below xml by getting unique departments from above xml.

<Departments>
  <name>d1</name>
  <name>d2</name>
  <name>d3</name>
</Departments>

Any suggestion would be helpful.

1

1 Answers

1
votes

This should do the trick:

%dw 1.0
%output application/xml
---
Departments: payload.Colleges.College.Students.*Student mapObject (
    name: $.dept
) distinctBy $.name

Post the example payload to localhost:8081/

    <flow name="departments">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/xml
---
Departments: payload.Colleges.College.Students.*Student mapObject (
    name: $.dept
) distinctBy $.name]]></dw:set-payload>
        </dw:transform-message>
    </flow>