Message Formatting Using XSLT mediator WSO2 ESB

In enterprise application integration it is a frequent need to format messages from one format to another. This message formatting can be done at various levels. Anyway, SOAP ("Simple Object Access Protocol")  is still most used message sending format using XML. Hence XML message  formatting plays a vital role in business application integration.
In order to convert an XML format to another XSLT ( EXtensible Stylesheet Language) is the commonly used method. in Synapse, hence in WSO2 ESB there is a mediator called "XSLT mediator" which can be used to change an incoming SOAP message to another format (maybe to a format a Data service wants, maybe to a format a Back-End service wants) and send it out.

You can download the latest version of WSO2 ESB here.

  • Unzip the pack, go to /bin and run the product by sh wso2server.sh (in Linux) or wso2server.bat (in Windows).
  • Use a proxyService to capture the incoming message. To create a proxy we need a insequence and a outsequence. Thus create two sequences as "proxyInSeq" and "proxyOut" and use that two sequences to create the new proxy service. We will call this "messageTransformProxy".
  • Go to main>>Service Bus>>Local Entries in WSO2 Management Console.
  • Click on "Add local Entries" tab and then select "Add In-lined XML Entry".
  • Give a name and put your XSLT style sheet there. We will call it "messageTransformer"
  • Now anywhere in the  mediation logic you can call this transformer and convert the message currently in the "body". Normally when a soap is received it is in the body. Thus you cann call the transformer directly using following line. Be mindful to insert required namespaces here inline.
                                       

  •    After calling the transformer the body will have the transformed message. The previous message will be lost. If you want it again you could have saved it to a property using "property mediator" like below prior to calling the transformer.

<enrich>
   <source clone="true" type="body"/>
   <target action="replace" type="property" property="INCOMING_MSG_PAYLOAD"/>
</enrich>


and if you want the original message to the body back again you can copy back what you have saved in the property back to body, maybe after you have send the transformed message to a BE service using "send" mediator or "callout" mediator.


<enrich>
    <source clone="true" type="property" property="INCOMING_MSG_PAYLOAD"/>
    <target action="replace" type="body"/>
</enrich>



Following is some simple transformations.

Remove a tag in a message 

Consider incoming message


<customerDetail>
    <name>Hasitha Hiranya</name>
    <age>26</age>
    <address>Kandy,Sri Lana</address>
    <preferredName>Hasitha</preferredName>
</customerDetail>


XSLT style sheet 

<?xml version="1.0" encoding="UTF-8"?>
<localEntry xmlns="http://ws.apache.org/ns/synapse"
            key="LogCustomerAddressToMainCustomerAddressTransformer">
   <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:output omit-xml-declaration="yes"/>
      <xsl:template match="node()|@*">
         <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
         </xsl:copy>
      </xsl:template>
      <xsl:template match="preferredName"/>
   </xsl:stylesheet>
   <description/>
</localEntry>


Transformed message will be

<customerDetail>
    <name>Hasitha Hiranya</name>
    <age>26</age>
    <address>Kandy,Sri Lana</address>
</customerDetail>

Rename a tag in a message

Consider incoming message


<cus:addCustomer xmlns:cus="http://test.com">
    <customerDetail>
        <name>Hasitha Hiranya</name>
        <age>26</age>
        <address>Kandy,Sri Lana</address>
    </customerDetail>
</cus:addCustomer>


XSLT style sheet 



<?xml version="1.0" encoding="UTF-8"?>
<localEntry xmlns="http://ws.apache.org/ns/synapse"
            key="AddCustomerToEditCustomerTransformer">
   <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                   xmlns:cus="http://test.com"
                   version="1.0">
      <xsl:output omit-xml-declaration="yes"/>
      <xsl:template match="@*|node()">
         <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
         </xsl:copy>
      </xsl:template>
      <xsl:template match="cus:addCustomer">
         <cus:editCustomer>
            <xsl:apply-templates select="@*|node()"/>
         </cus:editCustomer>
      </xsl:template>
   </xsl:stylesheet>
   <description/>
</localEntry>




Transformed message will be



<cus:editCustomer xmlns:cus="http://test.com">
    <customerDetail>
        <name>Hasitha Hiranya</name>
        <age>26</age>
        <address>Kandy,Sri Lana</address>
    </customerDetail>
</cus:editCustomer>



Hasitha Hiranya

5 comments:

  1. Hi I didn't understand how to apply xslt transformation in the proxy body

    ReplyDelete
  2. Hi, nice description about Message Formatting Using XSLT .Thanks for your help..

    -Aparna
    Theosoft

    ReplyDelete
  3. I really never read about this before. Thanks for giving us great knowledge about this. Thanks......


    Family Mediation Service & Mediation Services

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete

Instagram