Dynamically Selecting SOAP Message Elements in WSO2 ESB

Most recently I faced a requirement where I had to select elements dynamically from a SOAP response message which comes to WSO2 ESB. The use case is as follows.

I have a proxy service where I get a request from a client and let's say the request looks like following.

1:  <Request>  
2:      <Values>  
3:          <value>2</value>  
4:      </Values>  
5:  </Request>  

The value can be changed for each request. However when the request is sent to the backend server from ESB we get a response as following

1:  <Response>  
2:      <Events>  
3:          <Event><TestEntry>Entry Val1</TestEntry></Event>  
4:          <Event><TestEntry>Entry Val2</TestEntry></Event>  
5:          <Event><TestEntry>Entry Val3</TestEntry></Event>  
6:          <Event><TestEntry>Entry Val4</TestEntry></Event>  
7:          <Event><TestEntry>Entry Val5</TestEntry></Event>  
8:      </Events>  
9:  </Response>  

Depending on the Value specified in the Request, we need to extract the specified number of event entries from the response (If the value in the request is 2 then we will have to extract two event entries from the response).

In order to achieve the requirement I had to follow the bellow configurations.

Sequence Calling the xslt Transform

Use the xslt mediator to transform the incoming payload to ESB

1:  <sequence name="get_document_list_seq" trace="disable" mlns="http://ws.apache.org/ns/synapse">  
2:   <property expression="//value" name="limit"  
3:    scope="default" type="STRING" xmlns:ns="http://org.apache.synapse/xsd"/>  
4:   <xslt key="payload_transform" source="//Response"  
5:    xmlns:ns="http://org.apache.synapse/xsd"  
6:    xmlns:s11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">  
7:    <property expression="get-property('limit')" name="PARAM_NAME"/>  
8:   </xslt>  
9:   <respond/>  
10:  </sequence>  

XSLT Transformation to transform the payload and extract the elements

With the Property set with the name PARAM_NAME which passed to the xslt from the sequence above, will use to determine the number of elements to be extracted.

1:  <localEntry key="payload_transform" xmlns="http://ws.apache.org/ns/synapse">  
2:   <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  
3:    <xsl:output indent="yes" method="xml" omit-xml-declaration="yes"/>  
4:    <xsl:param name="PARAM_NAME"/>  
5:    <xsl:template match="/">  
6:     <list>  
7:      <responseList>  
8:       <xsl:for-each select="//Response/Events/Event[position()&lt;=number($PARAM_NAME)]">  
9:         <entry>  
10:          <xsl:value-of select="TestEntry"/>  
11:         </entry>  
12:       </for-each>  
13:      </responseList>  
14:     </list>  
15:    </xsl:template>  
16:   </xsl:stylesheet>  
17:  </localEntry>  


Popular posts from this blog

Enabling Custom SSL Profiles in WSO2 ESB 4.8.1

Using WSO2 ESB HTTP Endpoints to define Restful Endpoints

Integrating WSO2 ESB Connectors in real world integration Scenarios