Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Modifying Multi-Rooted XML Payloads in WSO2 ESB

DZone's Guide to

Modifying Multi-Rooted XML Payloads in WSO2 ESB

WSO2 ESB can's handle multi-rooted XML. In this article, learn how to adjust your code to prevent this issue to prevent unexpected behavior.

Free Resource

The Integration Zone is brought to you in partnership with Cloud Elements. What's below the surface of an API integration? Download The Definitive Guide to API Integrations to start building an API strategy.

When working with message transformation, we need to modify payloads. We may be removing an element or adding a new element, but there can be a payload when you remove the main root element to add a new element and the payload becomes fully or partially multi-rooted. In general, multi-rooted XML is fundamentally wrong, so the ESB cannot handle such scenarios. As a result, the ESB will not behave as you are expecting. If you use the same XPath with some other external XPath testing tools or website, you will learn that your expression is not wrong, even though you are not getting the expected output.

Let's consider the payload below:

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

Try the below XPath expression with some XPath tool or website, such as xpathtester.com. You will get the following result:

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

The problem here is that the name and id elements do not have a proper root element, so the ESB will fail to produce the output.

Use Case

The above payload needs to be converted to a 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>

That means you need to remove the student element and add an employee element.

Solution

You can use the payload factory as media-type=”xml” (to produce an XML result) and evaluator as JSON to overcome this issue. The 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>


Your API is not enough. Learn why (and how) leading SaaS providers are turning their products into platforms with API integration in the ebook, Build Platforms, Not Products from Cloud Elements.

Topics:
esb ,wso2 ,wso2 esb ,integration ,xml

Published at DZone with permission of Krishantha Dinesh. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}