W3C home > Mailing lists > Public > public-rdf-dawg@w3.org > July to September 2011

Re: xpath string index function

From: Steve Harris <steve.harris@garlik.com>
Date: Wed, 28 Sep 2011 14:26:48 +0100
Cc: SPARQL Working Group <public-rdf-dawg@w3.org>
Message-Id: <62B65AE9-9C04-4152-A14A-1352945D2FE9@garlik.com>
To: Sandro Hawke <sandro@w3.org>
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.

- 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:27:20 GMT

This archive was generated by hypermail 2.3.1 : Tuesday, 26 March 2013 16:15:46 GMT