简体   繁体   中英

DataWeave XML transform (list unique nested elements)

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM