Thursday, August 13, 2015

How to use XSLT Mediator in WSO2 ESB

XSLT mediator is used to transform the message as we do in script and payload factory mediator. We can create a structure of xml as required to send or receive in request or response.

To add an xslt mediator go to the IN sequence of the service then add child --àtransform ---à XSLT

You will see the below screen.

Add the details as required then click Apply and finish to add xslt mediator.
To use it direct in synapse file follow the syntax.

<syn:localEntry key="<KEY_NAME>" src="<FILE_PATH>"/>

  • In Key, you have to give a name by which XSLT can be referred in synapse.
  • In source, you have to provide the XSLT file name with its complete path.

Now to call the XSLT in the service, define below syntax, you can also pass some variable in it which will be picked up in XSLT as required.

<syn:xslt key="<KEY_NAME>">
<syn:property name="<PROPERTY_NAME>" expression="<EXPRESSION_VALUE>" type="<DATA_TYPE>"/>

Write above code to use XSLT, there are some values you have to provide as below.

  • In key, you have to pass the name of the value which has been defined in in “localEntry
  • In property, you can pass variable which will be picked up in XSLT.
  • In expression, you can pass dynamic variable value which we have got from other source.
  • In type, we have to pass data type like String or Integer etc.

XSLT Structure:

<xsl:stylesheet version="2.0"
     exclude-result-prefixes="xs fn xsl">
<!--<xsl:param name="<PASSED PARAMETER FROM SYNAPSE>"/> ->
<xsl:template match="/">


In example we are trying to get employee details through XSLT in desired structure.

Here we are defining the XSLT to be used.

<localEntry key="EmplyeeDetails_XSLT" src="file:repository/conf/EMP/resources/xslt/EmplyeeDetails.xslt"/>

To call this XSLT we are defining below code and also passing two parameters.

<xslt key="EmplyeeDetails_XSLT">
<property name="EmpAddress" expression="get-property('EmpAddress')" type="STRING"/>
<property name="EmpName" expression="get-property('EmpName')" type="STRING"/>

This is the XSLT we have created to transform the message.


<xsl:stylesheet version="2.0"
     exclude-result-prefixes="xs fn xsl">

<xsl:param name="EmpAddress"/>
<xsl:param name="EmpName"/>
<xsl:template match="/">
<emp:GetEmployeeDetails xmlns:emp="">
<emp:empName><xsl:value-of select="$EmpName"></emp:empName>
 <emp:empAddress><xsl:value-of select="$EmpAddress"></emp:empAddress>

In synapse if we want to get data from this XSLT we can extract value by below property.

<syn:property name="EmpName" expression="//emp:empName"  xmlns:emp="" type="STRING"/>

By the property mediator we can get the transformed value from the XSLT mediator.