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 switch case mediator, in the specified order. If a matching case is found, it will be executed, and the remaining 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.
The Switch mediator is a conditionally content aware mediator.
Syntax
<switch source="[XPath|json-eval(JSON Path)]"> <case regex="string"> mediator+ </case>+ <default> mediator+ </default>? </switch>
UI Configuration
The parameters available to configure the Switch mediator are as follows.
Parameter Name | Description |
---|---|
Source XPath | 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 | This parameter displays the number of cases currently added to the Switch mediator configuration. See Switch-case-mediator for instructions to add a case.
|
Specify default case | Click this link to add a default switch-case mediator. Adding a default switch case mediator is optional. If it is specified, it will be executed if no matching switch-case is identified. |
Switch-case mediator
- 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.
- 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.
- Click Case again and click Add Child, and add the mediator(s) you want to execute when this case matches the switching value.
Note
You can configure the mediator using XML. Click switch to source view in the Mediator window.
Example
In this example the Property mediator 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.
<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>