[xquery-talk] string manipulation

John Snelson jsnelson at sleepycat.com
Sun Feb 5 17:51:32 PST 2006


Unfortunatley, the predicate "[1 to last() - 1]" will not work, since 
the predicate only selects the node at the numeric position when given 
an expression that returns a /single/ numeric value. "1 to last() - 1" 
will return a sequence of numeric values.

In order to do what you want, you will need to use "fn:subsequence()":

string-join(subsequence(tokenize("book/title/author", "/"), 1, last() - 
1), "/")

Instead of that, you could also try:

replace("book/title/author", "/.*?$", "")

or this:

let $str := "book/title/author" return
substring-before($str, concat("/", substring-after($str, "/")))

I'm sure there are other good ways to acheive the result you want.

John

Ryan Grimm wrote:
> Hi,
> Fortunately XQuery has some really cool string manipulation functions.
> One way to accomplish what you are looking for is:
> 
> string-join(tokenize("book/title/author", "/")[1 to last() - 1], "/")
> 
> This might look a bit more complicated then expected but it really isn't 
> bad if you break it down.
> The tokenize() function will split up a string into tokens based on the 
> second argument.  So tokenize("book/title/author", "/") will return a 
> sequence of three tokens ("book", "title", "author").  The [1 to last() 
> - 1] part is going to select the first of these tokens through the 
> second to last one.  This will leave you with a sequence of: ("book", 
> "title").  Lastly we can join that sequence back together with the 
> string-join() function thus adding back in the /'s.
> This will always strip off the rest of the string after the last "/" 
> regardless of how many /'s there are.  So if you have something like: 
> book/title/pubdate/author you will get back: book/title/pubdate.
> 
> Hope that helps.
> 
> --Ryan
> 
> On Feb 4, 2006, at 11:05 PM, fatma helmy wrote:
> 
>> dear all
>> i am trying to use string manipulation in xquery to
>> truncate the last part of a string for example
>> suppose my string is book/title/author i need to
>> truncate starting from the last / so it will become
>> book/title only , i need to do this for varied length
>> strings
>>
>>
>>
>>
>> __________________________________________________
>> Do You Yahoo!?
>> Tired of spam?  Yahoo! Mail has the best spam protection around
>> http://mail.yahoo.com
>> _______________________________________________
>> talk at xquery.com
>> http://xquery.com/mailman/listinfo/talk
>>
> 
> _______________________________________________
> talk at xquery.com
> http://xquery.com/mailman/listinfo/talk


-- 
John Snelson, Berkeley DB XML Engineer
Sleepycat Software, Inc
http://www.sleepycat.com

Contracted to Sleepycat through Parthenon Computing Ltd
http://blog.parthcomp.com/dbxml


More information about the talk mailing list