Re: xpath string index function

On 2011-09-28, at 14:47, Lee Feigenbaum wrote:

> On 9/28/2011 9:26 AM, Steve Harris wrote:
>> On 2011-09-28, at 14:07, Sandro Hawke wrote:
>> 
>>> As discussed in yesterday's telecon, I asked an expert (Liam Quin, XML
>>> activity lead), who confirmed there is no function in xpath to find the
>>> position of a string inside another one, and that people string-before,
>>> and can just use the length of that if they really need the position.
>> 
>> I note that will give you the 0-based offset of the string :)
>> 
>> Also, fn:substring-before returns a zero-length string in the case of a non-match, so it's rather tricky to work with:
>> 
>> IF(fn:starts-with(?x, "foo") || fn:string-length(fn:substring-before(?x, "foo"))>  0, fn:string-length(fn:substring-before(?x, "foo")) + 1, 0)
>> 
>> …or something like it will give you the 1-based index of a substring.
> 
> isn't there a contains function?

Ah, yes, fn:contains, so you could write:

   IF(fn:contains(?x, "foo"), fn:string-length(fn:substring-before(?x, "foo")) + 1, 0)

which is less horrible.

- Steve

-- 
Steve Harris, CTO, Garlik Limited
1-3 Halford Road, Richmond, TW10 6AW, UK
+44 20 8439 8203  http://www.garlik.com/
Registered in England and Wales 535 7233 VAT # 849 0517 11
Registered office: Thames House, Portsmouth Road, Esher, Surrey, KT10 9AD

Received on Wednesday, 28 September 2011 13:53:11 UTC