Switch Mediator¶
The Switch Mediator is an XPath or JSONPath filter. The XPath or JSONPath expression is evaluated and returns a string. This string is matched against the regular expression in each switch case, in the specified order. If a matching case is found, it is executed, and the remaining cases are not processed. If none of the case statements match and a default case is specified, the default case will be executed.
Syntax¶
<switch source="[XPath|json-eval(JSON Path)]">
<case regex="string">
mediator+
</case>+
<default>
mediator+
</default>
</switch>
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) . If you use namespaces in the expression, click the edit icon, then click Add Namespace to 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.
|
Specify default case | Adding a default case is optional. If it is specified, it will be executed if no matching case is identified. |
Switch-case mediator¶
- To add a case, go to edit the switch and click Add parameter under Case Branches.
- It will open a form where a regular expression can be added in the Case Regex parameter. By submitting, it will add an empty case under the switch mediator as a child.
- Click on the
+
mark under a chase to add the mediator(s) you want to execute when this case matches the switching value.
Examples¶
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 the 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">
<case regex="IBM">
<!-- the property mediator sets a local property on the *current* message -->
<property name="symbol" scope="default" type="STRING" value="Great stock - IBM"/>
</case>
<case regex="MSFT">
<property name="symbol" scope="default" type="STRING" 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" scope="default" type="STRING" expression="fn:concat('Normal Stock - ', //m0:getQuote/m0:request/m0:symbol)"/>
</default>
</switch>