Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xslt 是否有一个「;“优雅”;测试属性值是否以字母开头的方法?_Xslt_Xpath - Fatal编程技术网

Xslt 是否有一个「;“优雅”;测试属性值是否以字母开头的方法?

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(@

我需要测试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(@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版中更是如此。