Search code examples
loopswso2esbdata-mappingwso2-esb

Data Transformation WS02


I have the following XML coming from a Database query and need to send the result to an API call. But I want to transform data in a different format, how do I do that. Any help would be appreciated.

Result from Database:

    `<Data>  
      <User Name="XYZ">
        <DETAILS>
           <COURSE>IS</COURSE>
           <YEAR>2016</YEAR>
           <SUBJECT>COMPUTER SCIENCE<SUBJECT>         
         </DETAILS>
       </User>
       <User Name="XYZ">
        <DETAILS>
           <COURSE>IS</COURSE>
           <YEAR>2015</YEAR>
           <SUBJECT>INFO SCIENCE<SUBJECT>         
       </DETAILS>
      </User>
     </Data> `

How I need to Transform: <Data>
<User Name="XYZ"> <DETAILS> <COURSE>IS</COURSE> <YEAR>2016</YEAR> <SUBJECT>COMPUTER SCIENCE<SUBJECT> </DETAILS> <DETAILS> <COURSE>IS</COURSE> <YEAR>2015</YEAR> <SUBJECT>INFO SCIENCE<SUBJECT>
</DETAILS> </User> </Data>


Solution

  • ForEach mediator is for transforming segments of data and replacing them in the original payload. Iterate mediator is not used for transformations as such. It is used for calling backends and aggregating responses. Please refer https://docs.wso2.com/display/ESB500/ForEach+Mediator for more details.

    From the sample you have added here, seems like you need to merge results by the username. Hence you cannot use ForEach, Iterate mediators. However, you can use Script mediator, but it is better to use XSLT mediator since it is a direct and easy XML manipulation.