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

XSLT字母数字校验问题求助:特殊字符/字母数字输入校验失败

XSLT校验逻辑问题排查与修改

问题

开发XSLT校验功能时,已实现空字符串、长度、纯数字校验,但遇到含特殊字符或字母数字组合的输入(如1234asdv)时,校验逻辑无法正确识别错误格式。

现有代码

<?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:fn="http://www.w3.org/2005/xpath-functions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output indent="yes" method="xml" omit-xml-declaration="yes"/>
        <xsl:template match="/">
            <OperationValueRegex>
                <xsl:call-template name="values">
                    <xsl:with-param name="vals" select="//OperationValueRegex/Value"/>
                </xsl:call-template>
            </OperationValueRegex>
        </xsl:template>
        <xsl:template name="values">
            <xsl:param name="vals"/>
            <xsl:choose>
                <!-- Validation Number -->
                <xsl:when test="fn:tokenize($vals, ' ')[matches(., '\d+')] and string-length($vals) = 8">
                    <Result1>
                        <xsl:value-of select="$vals"/>
                    </Result1>
                </xsl:when>
                <xsl:when test="$vals = 'NaN' or string-length($vals) != 8">
                    <Result1>
                        <Value>
                            <xsl:value-of select="$vals"/>
                        </Value>
                        <notification>input was not valid requirement maximum</notification>
                    </Result1>
                </xsl:when>
                <xsl:otherwise>
                    <Result1>
                        <Value>
                            <xsl:value-of select="$vals"/>
                        </Value>
                        <notification>Wrong Format</notification>
                    </Result1>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
    </xsl:stylesheet>
</localEntry>

请求示例与预期结果

请求(JSON)

{
    "OperationValueRegex" : {
        "Value" : "1234asdv"
    }
}

预期结果(XML)

<OperationValueRegex xmlns="http://ws.apache.org/ns/synapse" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <Result1>
        <Value>1234asdv</Value>
        <notification>Wrong Format</notification>
    </Result1>
</OperationValueRegex>

问题分析与修改方案

核心问题

  1. 纯数字校验逻辑错误:原代码用fn:tokenize($vals, ' ')[matches(., '\d+')],会将输入按空格分割后只要有一段是数字就判定通过,无法确保整个输入是纯8位数字。
  2. 空字符串校验缺失:未明确处理空输入的情况。
  3. 无效条件冗余:$vals = 'NaN'无意义,因为输入是字符串类型,不会匹配数值类型的NaN。
  4. 逻辑顺序不合理:长度校验应优先于格式校验,避免错误分支触发。

修改后的代码

<?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:fn="http://www.w3.org/2005/xpath-functions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output indent="yes" method="xml" omit-xml-declaration="yes"/>
        <xsl:template match="/">
            <OperationValueRegex>
                <xsl:call-template name="values">
                    <xsl:with-param name="vals" select="//OperationValueRegex/Value"/>
                </xsl:call-template>
            </OperationValueRegex>
        </xsl:template>
        <xsl:template name="values">
            <xsl:param name="vals"/>
            <xsl:choose>
                <!-- 空字符串校验 -->
                <xsl:when test="string-length($vals) = 0">
                    <Result1>
                        <Value>
                            <xsl:value-of select="$vals"/>
                        </Value>
                        <notification>input cannot be empty</notification>
                    </Result1>
                </xsl:when>
                <!-- 长度校验:必须为8位 -->
                <xsl:when test="string-length($vals) != 8">
                    <Result1>
                        <Value>
                            <xsl:value-of select="$vals"/>
                        </Value>
                        <notification>input was not valid requirement maximum</notification>
                    </Result1>
                </xsl:when>
                <!-- 纯数字校验:整个字符串为8位数字 -->
                <xsl:when test="matches($vals, '^\d{8}$')">
                    <Result1>
                        <xsl:value-of select="$vals"/>
                    </Result1>
                </xsl:when>
                <!-- 其他情况:含特殊字符/字母数字组合等错误格式 -->
                <xsl:otherwise>
                    <Result1>
                        <Value>
                            <xsl:value-of select="$vals"/>
                        </Value>
                        <notification>Wrong Format</notification>
                    </Result1>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
    </xsl:stylesheet>
</localEntry>

修改说明

  • 增加空字符串校验分支,优先处理空输入。
  • 调整逻辑顺序:先校验空值,再校验长度,最后校验格式。
  • 用matches($vals, '^\d{8}$')替换原错误的分割匹配逻辑,确保整个输入是8位纯数字(^表示字符串开头,$表示字符串结尾,\d{8}表示恰好8个数字)。
  • 移除无意义的$vals = 'NaN'条件。

内容的提问来源于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 05:18:34