[xquery-talk] string manipulation

Ryan Grimm grimm at oreilly.com
Sat Feb 4 23:28:15 PST 2006

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.


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

More information about the talk mailing list