0

Hi I'm trying to use Script Mediator to do a SOAP -> JSON transformation

<ns:getTestMethodResponse xmlns:ns="http://main.wsmediator">
 <ns:return>getTestMethod is called</ns:return>
</ns:getTestMethodResponse>

This is part of the response I'm receiving from the test service I've created. Question is I am unable to get the element data as mentioned in the WO2 ESB docs. Which is

mc.getPayloadXML()..::return.toString();

I've even tried

mc.getPayloadXML()..::ns:return.toString();

What am I doing wrong here? Appreciate the help.

Community
  • 1
  • 1
StO
  • 147
  • 1
  • 12

1 Answers1

1

E4X is namespace aware, so to access the element that is in a non-Default namespace you either need to declare the namespace/prefix or use a wildcard *.

Edit: Some debugging in the ESB itself showed me that we cannot use the word "return". This is a reserved word, and is not allowed, not even as part of the query string to retrieve the "return" element from your xml.

Lets assume for a moment that the element was called "value" instead, then the code would compile and work as follows:

var ns = new Namespace('http://main.wsmediator');
var xmlValue = mc.getPayloadXML()..ns::value.toString();

Another option is to use the wildcard:

var return = mc.getPayloadXML()..*::value.toString();

However changing the xml payload to adhere to JavaScript standards is not a very pretty, and possibly not a viable solution. Instead we can to try to acces the element in another way.

 <inSequence>
         <script language="js">
           var ns = new Namespace('http://main.wsmediator');
           var value = mc.getPayloadXML().*.toString();
           mc.setProperty("Asterisk", value);
         </script>
         <log>
            <property name="Asterisk" expression="get-
                 property('Asterisk')"/>
         </log>
         <respond/>
 </inSequence>

The getTestMethodResponse element is the root element when you do mc.getPayloadXML(), the asterisk gives you the children of this element. This works, yet is not very pretty because it is based on some assumptions about the message structure.

Another option might be to completely forego using the script mediator and use a PayloadFactory instead. For example:

<payloadFactory media-type="json">
      <format>
          {"return":"$1"}
      </format>
      <args>
        <arg xmlns:ns="http://main.wsmediator"
                    evaluator="xml"
                    expression="//ns:return"/>
      </args>
 </payloadFactory>

For more on E4X, check here For more on PayloadFactoryMediator check here

ophychius
  • 2,623
  • 2
  • 27
  • 49
  • Thanks for the suggestion @ophychius but this approach didn't work either. WSO2 Developer Studio didn't let me save the mediation logic. It has something to do with this line `var return = mc.getPayloadXML()..::(ns:return).toString();` – StO Jun 01 '17 at 06:39
  • Thanks, I've tried that way as well. Do you know if there are logs on Developer Studio that I could check? – StO Jun 01 '17 at 07:54
  • Have you tried replacing the namespace prefix with a wildcard? If it still causes problems can you share the complete Error / Warning that Developer Studio gives you? – ophychius Jun 01 '17 at 08:22
  • Here is the message that I'm getting **Save failed. Following error(s) have been detected. Please see the error log for more details.** **Reason: Exception initializing inline script** **Unhandled event loop exception org.eclipse.ui** – StO Jun 02 '17 at 09:35
  • I did some debugging by trying to build it directly on the ESB, this gives some more detail on error messages. Updated my answer with the findings and two possible solutions. Hope that helps. – ophychius Jun 03 '17 at 07:18