0

How can be selected element according his value and class with xpath?

The case:Need xpath for class="my_class" + element value = 'First element value'

text....
    <div class="my_class"> First element value </div>
text... 
<div class="my_class"> Second element value </div>
text...
<div class="not_my_class"> First element value </div>
text..

Update: please if you can write full code: xpath + php or other program language.

Thanks

Ben
  • 25,389
  • 34
  • 109
  • 165

1 Answers1

1

This xpath will give you the first element in your example:

//div[.='First element value' and @class='my_class']

However you probably don't want to use the // operator; use a specific path instead. This works in XMLSpy, not sure about in any languages' XML implementations.

Graham Clark
  • 12,886
  • 8
  • 50
  • 82
  • please can you give php example with xpath that you write? – Ben Nov 10 '10 at 14:31
  • @Yosef: sorry, I don't really know PHP. However, this question looks like it might be useful to you: http://stackoverflow.com/questions/230592 – Graham Clark Nov 10 '10 at 14:34
  • what meaning of //div instead just div? – Ben Nov 10 '10 at 16:10
  • @Yosef: `//div` means *all `div` elements descendant from document root* and `div` means *all `div` elements children of context node* –  Nov 10 '10 at 16:41
  • +1 Good answer. As a minor, do note that `@class` in (X)HTML probably holds a sequence. –  Nov 10 '10 at 16:43