16
XML to XML transformation in Mule

Xml to xml transformation in mule

Embed Size (px)

Citation preview

Page 1: Xml to xml transformation in mule

XML to XML transformation in Mule

Page 2: Xml to xml transformation in mule

As we know, there can be multiple types of payload we need to deal daily, starting from JSON, XML or String, we also require different type of data transformation in our application like XML to JSON, JSON to XML or String etc . In Mule application, we have a various set of transformers that we can use to obtain our required data format.

Here we will be discussing on transformation of XML to XML format of data. If you have gone through my previous slides on JSON to JSON transformation here , you will realize how easy it is to perform such type of transformation in Mule. Here we will take the same example, but will be doing it on XML instead of JSON this time.

Page 3: Xml to xml transformation in mule

So, let us consider, we have the following XML payload as an input to our application :-

<RootTag><name>Anirban Sen Chowdhary</name><id>37</id></RootTag>

.

Page 4: Xml to xml transformation in mule

We can see here the XML contains only 2 elements, that is name and id.

So, now if this XML data is coming as an input and we require to transform/modify the payload into another XML format and produce it as an output, something like the following :-

<RootTag><name>Anirban Sen Chowdhary</name><id>37</id><designation>Director</designation></RootTag>

Page 5: Xml to xml transformation in mule

And here you can see the modified XML has a new element designation as a third element . Now, the challenge here is to modify the existing input XML payload and to design an output XML payload from it, which will contain this additional element in it with the same format.

So, how can we transform/modify the payload into another XML format in an easy way. We know Mule has extremely powerful tools called Datamapper but that limited to Mule enterprise edition.

Page 6: Xml to xml transformation in mule

How to transform or modify a XML payload in Mule ??

Page 7: Xml to xml transformation in mule

A simple way of doing it is using XSLT in Mule. Mule do have XSLT support in it’s flow, which makes the life of a developer easy in transforming/modifying XML in the required format.So, here is the Mule flow which can be use to transform XML payload from one format to another using XSLT :-

You can see above, our Mule flow is receiving the input XML in the flow, which contains name and id node.

Page 8: Xml to xml transformation in mule

After that, we are setting the designation variable with a value, which will be passed into our XSLT, and it will be adding it there as a node called designation. So, the XML output coming after XSLT will be the modified/transformed XML containing additional node called designation.The XSL file here named as Transform.xsl should be kept under src/main/resources folder and the xsl file will be as following :-

Page 9: Xml to xml transformation in mule

The graphical representation of this flow will be as follows :-

Page 10: Xml to xml transformation in mule

Testing our application ……

Page 11: Xml to xml transformation in mule

Now, we are done with our coding and will deploy our application and test it.Once deployed on Mule server, we can test our application using RestClient or any other client you wish.

So, we can see we are ready to test our Mule flow and the input XML is already in the body which is going to be posted to our application.

Page 12: Xml to xml transformation in mule

once we click the send button we will get the following response in our client from our Mule flow :-

Here, you can see here the input XML has been modified and transformed into another XML format as output, where the new element designation has been added.

Page 13: Xml to xml transformation in mule

Conclusion

Page 14: Xml to xml transformation in mule

So, you can find transforming or modifying a XML payload into another XML format is extremely easy in Mule using the XSLT .

And that means you can design your output XML payload in any format you require using XSLT easily in your Mule flow and Mule also support this by it’s XSLT transformer !!!

That’s all !!! I’ve hopefully been clear enough in displaying the implementation of XSLT and XSLT transformer in designing and modifying your XML payload which you can produce as an output from your Mule application.

I hope you liked this !!!

Page 15: Xml to xml transformation in mule

Hope you enjoyed the simple yet an amazing trick in Mule

You can also find this post here :- http://bushorn.com/xml-to-xml-transformation-in-mule/

Page 16: Xml to xml transformation in mule

Thank You