[WSO2 ESB] Change local name of an element of the payload

There was a requirement to change the local name of an element in the payload using a parameter in the incoming payload. For example

user sends a following payload to the ESB

<people>
   <action>submit</action>
   <person>
      <name>asanka</name>
   </person>
   <person>
      <name>nuwan</name>
   </person>
   <person>
      <name>bandara</name>
   </person>
</people>

Requirement was, value in

  <action>submit</action> 

to be the root element of the response message like below.

<submit>
<status>SUCCESS</status>
</submit>

Guess what !!! .. it is just following piece of code.

<sequence xmlns="http://ws.apache.org/ns/synapse" name="createPayload">
   
     <property name="RESPONSE_ROOT_TAG" expression="//action/text()" type="STRING"/>
         
        <script language="js">
            var rootElementName=mc.getProperty('RESPONSE_ROOT_TAG');
            mc.getEnvelope().getBody().getFirstElement().setLocalName(rootElementName);
        </script>    
</sequence>

hope this can save someone’s time 🙂 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s