jreed121
jreed121

Reputation: 2097

XPath - find first occurance of string

I'm trying to select an anchor element by first containing the text "To Be Coded", then extracting a number from a string using substring, then using the greater than comparison operator (>0). This is what I have thus far:

/a[number(substring(text(),???,string-length()-1))>0]

An example of the HTML is:

<a class="" href="javascript:submitRequest('getRec','30', '63', 'Z')">
                                    To Be Coded&nbsp;&nbsp;(23)
</a>

My issue right now is I don't know how to find the first occurrence of the open parenthesis. I'm also not sure how to combine what I have with the contains(text(),"To Be Coded") function.

So my criteria for the selection is:

Edit: I suppose I could just "hard code" the starting position for the substring, but I'm not sure what that would be - will XPath count the white space before the text in the element? How would it handle/count the &nbsp; characters?

Upvotes: 3

Views: 908

Answers (1)

FailedDev
FailedDev

Reputation: 26940

Here try this :

a[contains(.,  'To Be Coded') and number(substring-before(substring-after(., '('), ')')) > 0]

Upvotes: 2

Related Questions