Modify multi rooted xml payload in WSO2 ESB

When we working with message transformation we need to modify payload may be removing element or adding new element. but there can be a payload when you remove main root element to add new element payload become fully or partially multi rooted. speak in general multi rooted xml are fundamentally wrong. so ESB cannot handle such scenarios. as a result ESB will not behave as you expecting. if you use same xpath with some other external xpath testing tools or website you will learn that your expression is not wrong eventhough you are not getting expected output.

lets consider below payload

<student>
    <name>Krishantha</name>
    <id>1208</id>
    <details>
        <core>Softtware Engineering</core>
        <alternate>database</alternate>
        <employement>yes</employement>
        <country>SL</country>
    </details>
</student>

try bellow xpath expression with some xpath tool or website such as http://www.xpathtester.com/xpath

you will get following result.

<name>Krishantha</name>
<id>1208</id>
<details>
      <core>Softtware Engineering</core>
      <alternate>database</alternate>
      <employement>yes</employement>
      <country>SL</country>
</details>

problem here is name and id element does not have proper root element. so ESB will fail to produce the output.

use-case

above payload need to be convert to payload like this.

<employee>
    <name>Krishantha</name>
    <id>1208</id>
    <details>
        <core>Softtware Engineering</core>
        <alternate>database</alternate>
        <employement>yes</employement>
        <country>SL</country>
    </details>
</employee>

which mean need to remove student element and add employee element.

solution

you can use payload factory as media-type=”xml” (to produce xml result) and evaluator as json to overcome this issue. below sequence will process that without any issue.

<?xml version="1.0" encoding="UTF-8"?>
<sequence xmlns="http://ws.apache.org/ns/synapse" name="rebuildPayloadseq">

   <payloadFactory media-type="xml">
      <format>
         <employee xmlns="">$1</employee>
      </format>
      <args>
         <arg evaluator="json" expression="$.student" />
      </args>
   </payloadFactory>

   <loopback />
</sequence>

source payload (input)

<student>
    <name>Krishantha</name>
    <id>1208</id>
    <details>
        <core>Softtware Engineering</core>
        <alternate>database</alternate>
        <employement>yes</employement>
        <country>SL</country>
    </details>
</student>

result response (output)

<employee>
    <name>Krishantha</name>
    <id>1208</id>
    <details>
        <core>Softtware Engineering</core>
        <alternate>database</alternate>
        <employement>yes</employement>
        <country>SL</country>
    </details>
</employee>
Share this on your world...Share on Facebook
Facebook
Share on Google+
Google+
Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin
Email this to someone
email
Print this page
Print

Leave a Reply