This documentation is for WSO2 Enterprise Service Bus version 4.9.0 . View documentation for the latest release.

All docs This doc

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The Switch Mediator is an XPath or JSONPath filter. The XPath or JSONPath is evaluated and returns a string. This string is matched against the regular expression in each switch case statementmediator, in the specified order. If a matching case is found, it will be executed, and no further processing of the case statements is executedremaining switch case mediators are not processed. If none of the case statements are matching, and a default case is specified, the default will be executed.

...

Code Block
XML
XML
<switch source="[XPath|json-eval(JSON Path)]">
   <case regex="string">
     mediator+
   </case>+
   <default>
     mediator+
   </default>?
</switch>

...

UI Configuration

This section describes how to configure the mediator in design view. You can also configure the mediator using XML by clicking switch to source view in the Mediator window.

Image Removed

To add the Switch mediator, click Add Child or Add Sibling in the sequence, and then click Filter -> Switch.

The parameters available to configure the Switch mediator are as follows.

...

  1. To add a case, click Add case, which adds an empty switch-case mediator under the Switch mediator. A switch-case mediator would appear as a child of the Switch mediator in the mediator tree as shown below.
     
  2. Click Case to configure the switch-case mediator. The page will expand to display the section shown below where a regular expression can be added in the Case Value (Regular Expression) parameter.
     
  3. Click Case again and click Add Child, and add the mediator(s) you want to execute when this case matches the switching value.

 

Example

...

Info
titleNote

You can configure the mediator using XML. Click switch to source view in the Mediator window.

Image Added

...

Example

In this example the Property mediator to set  sets the local property named "symbol" on the current message depending on the evaluation of the string. It will get the text of symbol element and match it against the values MSFT and IBM. If the text does not match either of these symbols, the default case will be executed.

Code Block
XML
XML
<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 or JSON Path 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. If the text does not match either of these symbols, the default case will be executed.