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

WSO2中含choose函数的Simple XSLT数值验证问题求助

JSON转XML验证XSLT返回空值问题排查

问题概述

使用Simple XSLT处理JSON格式的数值输入,期望验证后返回XML格式响应,但测试后<Result>节点返回空值,需排查问题原因并修复。

相关代码片段

API配置代码

<?xml version="1.0" encoding="UTF-8"?>
<api context="/a" name="A" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="POST">
        <inSequence>
            <xslt key="LOCAL_XSLT_Validate"/>
            <respond/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

XSLT代码

<?xml version="1.0" encoding="UTF-8"?>
<localEntry key="LOCAL_XSLT_Validate" xmlns="http://ws.apache.org/ns/synapse">
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:template match="/">
        <!-- TODO: Auto-generated template -->
        <xsl:variable name="Value" select="/OperationValueRegex/Value/text()"/>
        <OperationValueRegex>
            <xsl:choose>
                <xsl:when test="/OperationValueRegex/Value != 0">
                    <Result1><xsl:value-of select="fn:tokenize(/OperationValueRegex/Value, ' ')[matches(., '\d+')]"/></Result1>
                </xsl:when>
            </xsl:choose>
        </OperationValueRegex>
    </xsl:template>
</xsl:stylesheet>
</localEntry>

请求示例

{
    "OperationValueRegex" : {
        "Value" : "123"
    }
}

预期结果

<OperationValueRegex>
        <Result>123</Result>
</OperationValueRegex>

实际返回结果

<OperationValueRegex>
        <Result></Result>
</OperationValueRegex>

错误原因分析

  1. XSLT版本与函数不兼容:使用的XSLT版本为1.0,但fn:tokenize和matches是XSLT 2.0及以上版本才支持的函数,1.0环境下调用这些函数会直接返回空值。
  2. JSON转XML后的节点路径错误:在Synapse中,JSON请求默认会被转换为XML格式,根节点为jsonObject,而非直接是OperationValueRegex。原XSLT中使用的/OperationValueRegex/Value路径无法匹配到实际节点,导致后续取值失败。
  3. 元素名称不匹配:预期结果中的元素是<Result>,但XSLT代码中输出的是<Result1>,即使逻辑正确也无法得到预期的元素名称。
  4. 判断逻辑冗余:/OperationValueRegex/Value != 0的判断逻辑不合理,当Value为字符串类型的数字时,与数值0比较可能出现类型不匹配的问题。

修复方案及代码

修复后的XSLT代码

<?xml version="1.0" encoding="UTF-8"?>
<localEntry key="LOCAL_XSLT_Validate" xmlns="http://ws.apache.org/ns/synapse">
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <OperationValueRegex>
            <xsl:variable name="value" select="/jsonObject/OperationValueRegex/Value/text()"/>
            <xsl:choose>
                <xsl:when test="matches($value, '\d+')">
                    <Result><xsl:value-of select="$value"/></Result>
                </xsl:when>
                <!-- 可添加else分支处理非数字情况 -->
                <xsl:otherwise>
                    <Result>Invalid Value</Result>
                </xsl:otherwise>
            </xsl:choose>
        </OperationValueRegex>
    </xsl:template>
</xsl:stylesheet>
</localEntry>

关键修复点说明

  • 升级XSLT版本至2.0,原生支持matches等函数,无需fn前缀。
  • 修正XPath路径为/jsonObject/OperationValueRegex/Value/text(),匹配JSON转XML后的实际节点结构。
  • 将输出元素名称改为<Result>,与预期结果一致。
  • 优化判断逻辑,直接用matches验证Value是否为数字,逻辑更准确。
  • 添加<xsl:otherwise>分支处理非数字输入的情况,增强鲁棒性。

可选API配置优化

若Synapse未自动处理JSON转XML,可在inSequence中添加如下配置确保转换:

<property name="messageType" value="application/xml" scope="axis2"/>
<property name="ContentType" value="application/xml" scope="axis2"/>

验证结果

使用原请求示例测试,修复后将返回预期结果:

<OperationValueRegex>
        <Result>123</Result>
</OperationValueRegex>

内容的提问来源于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.20 22:57:28