This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

Switch Mediator

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 case statement, in the specified order. If a matching case is found, it will be executed, and no further processing of the case statements is executed. If none of the case statements are matching, and a default case is specified, the default will be executed.



Syntax

<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.

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

Here are the options to configure the Switch Mediator:

  • Source XPath - Specify the source XPath or JSONPath to be evaluated. When specifying a JSONPath, use the format json-eval(<JSON_PATH>), such as json-eval(getQuote.request.symbol). For more information on using JSON with the ESB, see JSON Support. If you use namespaces in the expression, click Namespaces and map the namespace prefix to the correct URI.

  • Number of cases - The number of cases specified. To add a case, click Add case, which adds an empty Switch-Case mediator under the Switch mediator. Click that Switch-case mediator in the tree to configure it as described below. To add more cases, click the parent Switch mediator and click Add case again.
  • Specify default case - To add a default case that is executed when all other cases do not match, click this link, which adds an empty Switch-Default mediator under the Switch mediator. You then click that Switch-Default mediator in the tree and click Add Child to add the mediators you want to execute when all other cases do not match.
Switch-Case Mediator

After clicking Add case in the parent Switch mediator, click the Case node that is added to the tree, and then specify the regular expression to use to match the switching value.

After you have specified the expression, click the Case node again, click Add Child, and add the mediator(s) you want to execute when this case matches the switching value.


Example

The following example uses the Property mediator to set the local property named "symbol" on the current message depending on the evaluation of the string.

<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.