3

I have id's (all divs I am afraid) like this:

<div id ='p1:pdr0'>
<div id = 'p1:pdr0:content'>
<div id = 'p1:pdr0:blahblahblah'>
...

I want just to find the first one. I could use the entire id, but the first part could change. I want to do ends-with(@id,'pdr0') but there is no ends-with().

Someone did suggest using a substring(length-3). I did look up in google. It showed a function but not a specific example of how to use it in an xpath. Would it just be //div[fn:(substring(fn:length(@id)-3))]? Probably not.

Can someone show me an example of using substring and length of an id in an xpath expression? (By the way the exact id names are not exactly that. Suffice to say they all contain the same text but some have more).

1 Answer 1

2

ends-with() is a part of XPath 2.0 and hence cannot be used in your case.

There is contains(), but, if we are talking about pure XPath approach, it would not help since pdr0 is also present in id attributes of other div elements.

Note that, if the order of the div elements is the one you've provided, you can rely on the position of the element (indexing starts from 1):

//div[contains(@id, 'pdr0')][1]

The more reliable option would be to simulate ends-with() using substring() with string-length():

//div[substring(@id, string-length(@id) - 3) = 'pdr0']
2
  • exactly what I wanted. Thanks
    – Tony
    Apr 2, 2015 at 18:42
  • Looking for the accept button. I know I did it before but I don't see it. A few minutes later -- found it ;-)
    – Tony
    Dec 4, 2015 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.