Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xpath

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.

like image 897
Pedro Luz Avatar asked Jul 28 '10 20:07

Pedro Luz


1 Answers

//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.

like image 76
Wrikken Avatar answered Sep 26 '22 01:09

Wrikken