What’s the XPath expression to select an attribute based on its prefix?

匿名 (未验证) 提交于 2019-12-03 08:52:47

问题:

I need to select a node with a id attribute that I only know part of the value.

If I have several <tr> elements:

<tr id="foobar[1234]"></td><tr id="foobar[1235]"></td><tr id="foobar[1236]"></td><tr id="bar[1]"></td><tr id="foobar[1237]"></td><tr id="bar[12]"></td>

I only want to select the id's that start with foobar.

I've tried:

//tr[@id='foobar*']

but it doesn’t work.

Any help? Thanks.

回答1:

//tr[starts-with(@id,'foobar')]

A list of XPath 1.0 functions: http://www.edankert.com/xpathfunctions.html

If your implementations supports XPath 2.0, you get a lot of other ones.



回答2:

Have you tried

//tr[@id*="foobar"]

I'm not certain this works, but it may.



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