且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Xpath获取if href包含字符串的一部分

更新时间:2023-02-23 13:34:29

在XPath 2.0或更高版本中,可以使用Regex函数,例如:

In XPath 2.0 or above, you can use Regex functions, for example :

//a[matches(@href, '/p/.*/\?tagged=see')]

或结合使用字符串函数starts-with()ends-with():

Or using combination of string functions starts-with() and ends-with() :

//a[starts-with(@href, '/p/')]
   [ends-with(@href, '/?tagged=see')]

XPath 1.0没有正则表达式或ends-with()函数,但是,您可以模拟后者:>

XPath 1.0 doesn't have regex nor ends-with() functions, however, you can simulate the latter :

//a[starts-with(@href, '/p/')]
   [substring(@href, string-length(@href) - string-length('/?tagged=see') +1) = '/?tagged=see']

简体:

//a[starts-with(@href, '/p/')]
   [substring(@href, string-length(@href) - 11) = '/?tagged=see']