gpt4 book ai didi

xml - 使用xpath函数ends-with()查找数字

转载 作者:行者123 更新时间:2023-12-03 17:25:39 25 4
gpt4 key购买 nike

我想循环查找以字符串开头并以数字结尾的元素,但是我不确定如何使用ends-with()

我这里有这段代码

*[starts-with(name(), 'cup') and ends-with(name(), '_number')]


ps:不确定应用程序正在使用哪个版本的xpath

最佳答案

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())))]


请注意,第二个子句是 a clever way by Dimitre Novatchev to test in XPath 1.0 whether a string is a number

这是检查XPath 1.0中是否以数字结尾的较短方法:

//*[starts-with(name(), 'cup') 
and not(translate(substring(name(),string-length(name())), '0123456789', ''))]

关于xml - 使用xpath函数ends-with()查找数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44719931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com