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>"/>
</syn:xslt>       

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"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:xs="http://www.w3.org/2001/XMLSchema"
     xmlns:fn="http://www.w3.org/2005/xpath-functions"
     exclude-result-prefixes="xs fn xsl">
<!--<xsl:param name="<PASSED PARAMETER FROM SYNAPSE>"/> ->
<xsl:template match="/">
<!-—STRUCTURE OF XML ->
</xsl:template>
</xsl:stylesheet>

Example:

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"/>
</xslt>         

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

XSLT:

<xsl:stylesheet version="2.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:xs="http://www.w3.org/2001/XMLSchema"
     xmlns:fn="http://www.w3.org/2005/xpath-functions"
       xmlns:emp="http://shriwithjava.blogspot.co.nz/empdetails/"
     exclude-result-prefixes="xs fn xsl">

<xsl:param name="EmpAddress"/>
<xsl:param name="EmpName"/>
<xsl:template match="/">
<emp:GetEmployeeDetails xmlns:emp="http://shriwithjava.blogspot.co.nz/empdetails/">
<emp:empName><xsl:value-of select="$EmpName"></emp:empName>
 <emp:empAddress><xsl:value-of select="$EmpAddress"></emp:empAddress>
    </emp:GetEmployeeDetails>
</xsl:template>
</xsl:stylesheet>

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="http://shriwithjava.blogspot.co.nz/empdetails/" type="STRING"/>

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

No comments:

Post a Comment