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>
问题分析与修改方案
核心问题
- 纯数字校验逻辑错误:原代码用
fn:tokenize($vals, ' ')[matches(., '\d+')],会将输入按空格分割后只要有一段是数字就判定通过,无法确保整个输入是纯8位数字。 - 空字符串校验缺失:未明确处理空输入的情况。
- 无效条件冗余:
$vals = 'NaN'无意义,因为输入是字符串类型,不会匹配数值类型的NaN。 - 逻辑顺序不合理:长度校验应优先于格式校验,避免错误分支触发。
修改后的代码
<?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
相关产品推荐
相关产品推荐

