Xslt 是否有一个「;“优雅”;测试属性值是否以字母开头的方法?
我需要测试attibute值是否以字母开头。如果没有,我将在它前面加上“ID_u3;”,这样它将是属性值的有效ID类型。 我目前有以下几点(测试值不以数字开头-我知道这些属性值只以字母或数字开头),但我希望有一种更优雅的方式:Xslt 是否有一个「;“优雅”;测试属性值是否以字母开头的方法?,xslt,xpath,Xslt,Xpath,我需要测试attibute值是否以字母开头。如果没有,我将在它前面加上“ID_u3;”,这样它将是属性值的有效ID类型。 我目前有以下几点(测试值不以数字开头-我知道这些属性值只以字母或数字开头),但我希望有一种更优雅的方式: <xsl:if test="not(starts-with(@value, '1')) and not(starts-with(@value, '2')) and not(starts-with(@value, '3')) and not(starts-with(@
<xsl:if test="not(starts-with(@value, '1')) and not(starts-with(@value, '2')) and not(starts-with(@value, '3')) and not(starts-with(@value, '4')) and not(starts-with(@value, '5')) and not(starts-with(@value, '6')) and not(starts-with(@value, '7')) and not(starts-with(@value, '8')) and not(starts-with(@value, '9')) and not(starts-with(@value, '0')) ">
我正在使用XSLT1.0。
提前谢谢。它稍微短了一点,虽然不是很优雅或明显:
<xsl:if test="not(number(translate(substring(@value, 1, 1),'0','1')))">
其基本思想是测试第一个字符是否为数字。需要调用translate()
,因为0
和NaN
都计算为false
,我们需要0
在not()
调用中被视为true
。
<xsl:if test="string(number(substring(@value,1,1)))='NaN'">
substring()
从@value
值中勾选第一个字符number()
函数计算该字符
NaN
string()
函数将其作为字符串计算,并检查它是否为NaN
substring()
函数从@value
值中勾选第一个字符number()
函数计算该字符
NaN
string-length()
not(number(substring(@value,1,1)) = number(substring(@value,1,1)) )
not(contains('0123456789', substring(@value,1,1)))
not(number(substring(@value, 1, 1)+1))
或使用:
not(number(substring(@value,1,1)) = number(substring(@value,1,1)) )
not(contains('0123456789', substring(@value,1,1)))
not(number(substring(@value, 1, 1)+1))
最后,这可能是验证您的条件的最短XPath 1.0表达式:
not(number(substring(@value,1,1)) = number(substring(@value,1,1)) )
not(contains('0123456789', substring(@value,1,1)))
not(number(substring(@value, 1, 1)+1))
XSLT1.0中的字符串处理操作通常是可能的,但它们很少优雅。@Michael-true,但这些答案肯定比我得到的更优雅+一个很好的答案。感谢您提供有关“0”返回false的信息。回答很好-+1。第三条也是最短的路是一个很好的转折点。杰奎琳:是的,XPath是一种了不起的语言,在2.0版和3.0版中更是如此。