求助:WSO2 MI Server4.0.0中Sequence Mediator算术运算失效问题
解决WSO2属性中介数值自增失败的问题
在WSO2中直接用属性中介的表达式$ctx:count + $ctx:increment会触发字符串拼接而非数值加法,这就是你得到错误结果的原因——哪怕属性类型设为INTEGER,XPath表达式里直接引用变量时仍会按字符串处理,比如"10"+"1"会得到"101"而非11。
以下是两种可行的修复方案:
方案1:使用XPath sum()函数
利用sum()函数对数值集合求和,确保运算为数值类型:
<property name="count" scope="default" type="INTEGER" value="10"/> <property name="increment" scope="default" type="INTEGER" value="1"/> <property expression="sum(($ctx:count, $ctx:increment))" name="result" scope="default" type="INTEGER"/> <log> <property expression="$ctx:result" name="result"/> </log>
方案2:用number()显式转换类型
通过number()函数将每个变量转为数值后再执行加法:
<property name="count" scope="default" type="INTEGER" value="10"/> <property name="increment" scope="default" type="INTEGER" value="1"/> <property expression="number($ctx:count) + number($ctx:increment)" name="result" scope="default" type="INTEGER"/> <log> <property expression="$ctx:result" name="result"/> </log>
这两种方案都能正确执行数值加法,且最终result属性会被转为INTEGER类型,满足你更新数据库的需求。
内容的提问来源于stack exchange,提问作者sathish Sankarzz
相关产品推荐
相关产品推荐

