[xquery-talk] string manipulation

Ryan Grimm grimm at oreilly.com
Sun Feb 5 10:41:29 PST 2006


Hi John,

> 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.

The behavior you are describing for the expression "1 to last() - 1" is 
exactly what I was looking for.  Putting this type of expression inside 
of a predicate is something that I make heavy use of.  However, it 
appears that it is something that not all xquery engines support.  
Doing this works perfectly well in Mark Logic but I just tested it 
using Saxon and it does not work there.
I checked out the spec and I can't find mention of this feature being 
present but it makes for a very handy short form of subsequence().

--Ryan


On Feb 5, 2006, at 9:51 AM, John Snelson wrote:

> 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
> _______________________________________________
> talk at xquery.com
> http://xquery.com/mailman/listinfo/talk
>



More information about the talk mailing list