43

I have the following XPath to match attributes of the class span:

//span[@class='amount']

I want to match all elements that have the class attribute of "amount" but also may have other classes as well. I thought I could do this:

//span[@class='*amount*'] 

but that doesn't work...how can I do this?

Oded
  • 489,969
  • 99
  • 883
  • 1,009
Colin Brown
  • 589
  • 1
  • 8
  • 15

2 Answers2

48

Use the following expression:

//span[contains(concat(' ', @class, ' '), ' amount ')]

You could use contains on its own, but that would also match classes like someamount. Test the above expression on the following input:

<root>
  <span class="test amount blah"/>
  <span class="amount test"/>
  <span class="test amount"/>
  <span class="amount"/>
  <span class="someamount"/>
</root>

It will select the first four span elements, but not the last one.

T.S.
  • 18,195
  • 11
  • 58
  • 78
Wayne
  • 59,728
  • 15
  • 131
  • 126
  • If suppose, I want to retrieve the text content inside the span tag then what will be the code. I have stored the result in nodelist and tried to print the content using nodelist.item(0).getFirstChild().getNodeValue(). But this doesn't return anything. Please help me out. – TheGaME Nov 04 '14 at 17:15
  • Interesting how a total of four (2 x 2) blanks are needed. – Dan Jacobson Nov 01 '20 at 21:32
26

You need to use contains method. See How to use XPath contains() here?

//span[contains(@class,'amount')]

Ashley Tate
  • 595
  • 5
  • 16
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179