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

WSO2中XPath结合fn:matches校验数字的结果修正求助

需求与代码修改方案

问题背景

我正在做字母与数字的校验练习,目前已实现两类内容的校验,但仅能返回true或false的结果。我使用fn:matches结合正则表达式完成校验,现需修正校验逻辑,实现当输入内容非数字时,返回包含错误描述<Status>标签的XML结果。

原代码

<?xml version="1.0" encoding="UTF-8"?>
<api context="/a" name="A" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="POST">
        <inSequence>
            <property expression="json-eval($.OperationValueRegex.Value)" name="Value" scope="default" type="STRING"/>
            <payloadFactory media-type="xml">
                <format>
                    <OperationValueRegex xmlns="">
                        <Result1>$1</Result1>
                    </OperationValueRegex>
                </format>
                <args>
                    <arg evaluator="xml" expression="if($ctx:Value != '') then fn:tokenize($ctx:Value, ' ')[matches(., '^\d+$')]" xmlns:fn="http://www.w3.org/2005/xpath-functions"/>
                </args>
            </payloadFactory>
            <respond/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

示例与预期结果

测试请求(JSON)

{
    "OperationValueRegex" : {
        "Value" : "sadasdsadasd"
    }
}

预期返回(XML)

<OperationValueRegex>
    <Result1>sadasdsadasd</Result1>
    <Status>the input not correct</Status>
</OperationValueRegex>

修改后的代码

<?xml version="1.0" encoding="UTF-8"?>
<api context="/a" name="A" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="POST">
        <inSequence>
            <property expression="json-eval($.OperationValueRegex.Value)" name="Value" scope="default" type="STRING"/>
            <payloadFactory media-type="xml">
                <format>
                    <OperationValueRegex xmlns="">
                        <Result1>$1</Result1>
                        $2
                    </OperationValueRegex>
                </format>
                <args>
                    <!-- 传入原始输入值 -->
                    <arg evaluator="xml" expression="$ctx:Value"/>
                    <!-- 条件生成错误标签:非空且非纯数字时返回Status标签,否则返回空 -->
                    <arg evaluator="xml" expression="if($ctx:Value != '' and not(matches($ctx:Value, '^\d+$'))) then '&lt;Status&gt;the input not correct&lt;/Status&gt;' else ''" xmlns:fn="http://www.w3.org/2005/xpath-functions"/>
                </args>
            </payloadFactory>
            <respond/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

关键改动说明

  1. 保留原始输入:第一个参数直接传入$ctx:Value,确保<Result1>始终显示用户提交的原始内容,而非仅匹配的数字片段
  2. 动态生成错误标签:第二个参数通过if-else逻辑判断:
    • 当输入非空且不匹配纯数字正则^\d+$时,返回<Status>错误标签
    • 当输入为空或为纯数字时,返回空字符串,不渲染<Status>标签
  3. 简化正则逻辑:移除原代码中不必要的fn:tokenize,直接对整个输入字符串做纯数字校验

测试验证

  • 输入纯数字(如"Value": "98765")时,仅返回<Result1>标签
  • 输入非数字内容(如字母、符号混合)时,同时返回<Result1>和<Status>错误标签

内容的提问来源于stack exchange,提问作者Hitoshi Mario Naga Mait

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 17:57:51