Xpath获取if href包含字符串的一部分 [英] Xpath get a if href contains part of string

查看:124
本文介绍了Xpath获取if href包含字符串的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试获取所有包含href =/p/{random}/?tagged = see的元素 这是我的台词

Hi there I tried to get all elements which contain href=/p/{random}/?tagged=see Here is my line

//div[preceding::h2[text()='Most recent']]/div/div/a[@href='/p/*/?tagged=see']

我该如何解决此代码,我必须用其他东西替换'*'吗?

How I can fix this code, I must replace the '*' with something else?

推荐答案

在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']

这篇关于Xpath获取if href包含字符串的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆