XML to XML transformation via Smooks Mediator in WSO2 ESB


Smooks is an extensible framework for building applications for processing XML and non XML data (CSV, EDI, Java etc). This example demonstrates how xml to xml transformation can be done using the smooks mediator in WSO2 ESB. WSO2 ESB provides XSLT, FastXSLT, Enrich, PayloadFactory, Smooks and XQuery mediators to transform messages from XML to XML. Choice of a particular mediator out of these, depends on the complexity of the use case. For a simple transformation  enrich or PayloadFactory would be the best choice. For a complex transformation XSLT, FastXSLT, XQuery or Smooks can be used. In this article I will use smooks mediator for xml to xml transformation.


Having said that let’s get some hands on smooks using WSO2 ESB. For this sample I am using WSO2ESB 4.8.1 version. First locate the axis2.xml file under the \$ESB_HOME/repository/conf/axis2 directory and enable vfs transport. For this uncomment the vfs transport sender and receiver elements.  Then move on to \$ESB_HOME/bin directory and execute ./wso2server.sh startup script to start the ESB.


Smooks consists of a configuration file where all the transformation specific stuffs are stated. A sample smooks configuration [1] used for this article is given below. Also all the configurations used in this article can be found here [2].




Let’s place this smooks configuration file inside the registry. For this in the web interface under the main menu, click on browse, and create a collection named smooks under the _system/config. Then save the above configuration in your file system and name it as ‘smooks_config.xml’. Move on to the created collection named ‘smooks’ and add this configuration file as a resource to that.


Then go to the source view under the main menu and add the following proxy configuration.



The input that we need to transform is given below. Take that and save into a file named input.xml under the file system location specified for the transport.vfs.FileURI parameter.




The proxy will pick that up from the specified file system location via the vfs transport and transform it as specified in the smooks_config.xml file. Then the output will be logged in the ESB console. A sample output is given below.




Here I have covered only xml to xml transformation scenario via the smooks mediator. You may refer to our documentation sample [3] for text to xml transformation. There are lot more combinations remaining. I hope that you would try them out.


References

Comments

  1. is there anyway to transform without locating input fo smooks mediator in a specific file?
    I mean mapping message's content directly.

    ReplyDelete
  2. No, You have to specify smooks configuration in a file and place it inside the registry and later you have to refer that file inside the smooks mediator using the config-key parameter. You can not map it directly.

    ReplyDelete

Post a Comment

Popular posts from this blog

Introducing Java Reactive Extentions in to a SpringBoot Micro Service

Optimal binary search trees

Combining the emissions of multiple Observables together using RxJava Zip operator in a Spring Boot Micro service