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 filter. The XPath is evaluated and return 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. Once the match is found, further processing of the case statements won't happen. If none of the case statements are matching and a 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 do not match and a default case exists, it will be executed.


UI Configuration

Following Mediators are available:

Switch Mediator

Here are the options to configure the Switch Mediator:

  • Source XPath - Here you can specify the source XPath to be evaluated.
  • Number of cases - The number of cases specified. You can add cases by clicking the "Add Case" link.
  • Specify default case - The link to add the default case. (This case is to be executed when all the other cases are not matching.)

Tip

You can click the Namespaces link to add namespaces if you are providing an expression. You will be provided another panel named "Namespace Editor," where you can provide any number of namespace prefixes and URL that you have used in the XPath expression.

Switch-Case Mediator

The option to configure the Switch Case Mediator:

  • Case Value (Regular Expression) - A regular expression to match with switching value.
Switch-Default Mediator

The Switch Default Mediator will be a child mediator for the Switch Mediator. It is used to define a default 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.

Note

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


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.