我想循环并找到以字符串开头的元素,并以数字结尾,但我不确定如何使用ends-with()
我在这里有此代码
*[starts-with(name(), 'cup') and ends-with(name(), '_number')]
ps:不确定应用程序正在使用
xpath 2.0
这在xpath 2.0中是直接的,其中此表达式
//*[matches(name(), '^cup.*d$')]
将选择所有名称以cup
开头的元素,并根据要求以数字结尾。
XPATH 1.0
由于XPath 1.0缺少正则态度,ends-with()
,并且功能可以测试字符串是否为数字,因此您的请求与XPATH 1.0更为复杂。这是一种可能的工作解决方案:
//*[starts-with(name(), 'cup')
and number(substring(name(),string-length(name())))
= number(substring(name(),string-length(name())))]
请注意,第二子句是Dimitre Novatchev进行测试的巧妙方式 在XPATH 1.0中,字符串是否为数字。
这是检查以XPATH 1.0中的数字结束的较短方法:
//*[starts-with(name(), 'cup')
and not(translate(substring(name(),string-length(name())), '0123456789', ''))]
我相信 ends-with
不在xpath 1.0中,您必须使用attleast xpath 2.0,然后您可以使用 matches()
将字符串与数字匹配,例如:
matches(name(), '.*d+$')
`xpath将是:
*[starts-with(name(), 'cup') and matches(name(), '.*d+$')]
或就像@kjhughes在他的回答中提到的那样:
*[matches(name(), '^cup.*d+$')]