xpath - using contains with a wildcard

老子叫甜甜 提交于 2021-02-07 12:49:17

问题


I have the following, and trying to see if there's a better approach. I know it cn be done using starts-with/contains. I'm testing with firefox 10, which I believe implements xpath 2.+.

Test node is

<a id="foo">
.
.
.
<a id="foo1">
.
<a id="foo2">

Is there a way to use wildcards to be able to get the foo1/foo2 nodes..

Something like

//a[@id =* 'foo'] 

or 

//a[contains(@id*,'foo')] 

Which would say, give me the "a" where the id starts with "foo" but has additional chars... This would then skip the 1st node with the "foo"

I thought I had seen an rticle on this, but can't find it!

As I recall, the article stated that xpath had a set of operators that could be used to designate the start/end of a given pattern in a string.

thanks


回答1:


Use:

//a[@id[starts-with(., 'foo') and string-length() > 3]]



回答2:


Not a wildcard, but probably what you need nonetheless:

//a[(@id!='foo') and starts-with(@id,'foo')]

See W3C XPath spec.



来源:https://stackoverflow.com/questions/12393858/xpath-using-contains-with-a-wildcard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!