XPath - 使用带通配符的contains函数

9

我有以下内容,并试图寻找更好的方法。我知道可以使用 starts-with/contains 方法来实现。我正在测试火狐浏览器 10,我相信它已经实现了 xpath 2.+。

测试节点为

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

有没有办法使用通配符来获取foo1/foo2节点?
例如:
//a[@id =* 'foo'] 

or 

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

有一个需求:给我“a”,但它的id以“foo”开头并有额外字符...这将跳过第一个具有“foo”的节点。

我记得看过一篇相关文章,但现在找不到了!

据我所知,该文章指出xpath有一组运算符可用于指定字符串中给定模式的开始/结束。

谢谢。


1
在 JQuery 中使用 *=(而不是 XPath 中的 =*)。 - choroba
2个回答

11

使用方法:

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

1
starts-with 后面缺少一个括号。整个表达式应该为://a[@id[starts-with(., 'foo') and string-length() > 3]] - ᴠɪɴᴄᴇɴᴛ

6

虽然不是通配符,但很可能仍能满足你的需求:

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

参见W3C XPath规范


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接