You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否在switch的case regex中使用表达式替代固定匹配字符串?

WSO2 ESB 动态正则路由问题解答

你的原始路由代码如下,作用是当$body/output的值为A或B时,将消息转发到指定端点:

<switch description="Path based on the output" source="$body/output">
    <case regex="A|B">
        <call description="Send request to endpoint">
            <endpoint key="endpoint"/>
        </call>
    </case>
    <default/>
</switch>

问题1:能不能把"A|B"存在名为output的local-entry里,用<case regex="get-property('output')">作为正则值?

不行。WSO2 ESB的<case>标签的regex属性属于静态配置项,部署阶段就会被解析,不支持运行时通过get-property()这类动态表达式获取值。这么写的话,ESB会把get-property('output')当成字面量正则去匹配,根本不会读取对应local-entry的内容,自然达不到预期效果。

问题2:如果上述方式不可行,能不能把local-entry的内容存到序列再调用,还是必须用固定字符串?

不用硬写固定字符串,完全可以通过动态逻辑实现需求,具体步骤如下:

  1. 先从local-entry中取出预存的正则表达式,存入属性:
    <property name="regexPattern" expression="get-property('output')"/>
    
  2. 使用脚本中介(以JavaScript为例)完成动态正则匹配,标记匹配结果:
    <script language="js"><![CDATA[
        // 获取$body/output节点的文本值
        var outputValue = mc.getPayloadXML().getElementsByTagName("output")[0].textContent;
        // 获取预存的正则表达式
        var regexStr = mc.getProperty("regexPattern");
        var regex = new RegExp(regexStr);
        // 根据匹配结果设置标记属性
        mc.setProperty("isMatch", regex.test(outputValue) ? "true" : "false");
    ]]></script>
    
  3. 最后通过Switch中介根据匹配结果路由:
    <switch source="$ctx:isMatch">
        <case regex="true">
            <call description="Send request to endpoint">
                <endpoint key="endpoint"/>
            </call>
        </case>
        <default/>
    </switch>
    

这样就实现了用local-entry中存储的动态正则表达式做路由判断,无需硬编码固定字符串。

内容的提问来源于stack exchange,提问作者Cris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 18:03:27