WSO2 ESB - Switch Mediator

Switch Mediator

Switch mediator is another XPath filter. The XPath will be evaluated and will return a string. This string is matched against the regular expression in each of cases in the specified order. If a matchig case is found it will be executed. Further proccessing of the case statements won't happen after a matching is found. If none of the case statements are matching and default case is specified, it will be executed.

Syntax

 <switch source="xpath">
   <case regex="string">
     mediator+
   </case>+
   <default>
     mediator+
   </default>?
 </switch>

The <switch> mediator will evaluate the given source XPath expression into its string value, and match it against the given regular expressions. If the specified cases does not match and a default case exists, it will be executed.

UI Configuration

Switch

Here are the options to configure the Switch Mediator.

Case

Switch Case Mediator

Here are the options to configure the Switch Case Mediator.

Default

The Switch Default Mediator will be a child mediator for the Switch Mediator. It is to define a defalut case for the current routing switch. If all the cases were not matched with the switch value, the sequence defined as children to the default mediator will be taken as the route of the message.

Example

  <switch source="//m0:getQuote/m0:request/m0:symbol" xmlns:m0="http://services.samples/xsd">
      <case regex="IBM">
          <!-- the property mediator sets a local property on the *current* message -->
          <property name="symbol" value="Great stock - IBM"/>
      </case>
      <case regex="MSFT">
          <property name="symbol" value="Are you sure? - MSFT"/>
      </case>
      <default>
          <!-- it is possible to assign the result of an XPath expression as well -->
          <property name="symbol"
                expression="fn:concat('Normal Stock - ', //m0:getQuote/m0:request/m0:symbol)"
                xmlns:m0="http://services.samples/xsd"/>
      </default>
  </switch>
    

In this scenario it will get the text of symbol element and match it against values MSFT and IBM.