[xquery-talk] Looping in Xquery

Peter Stadler stadler at weber-gesamtausgabe.de
Tue Sep 4 02:44:18 PDT 2012


An alternative would be to use string-join(). First join all the strings and then calculate the length:
{string-length(string-join($ltnfiles//subtopic[@id = 'DEF']/short-definition/p/text), '')}

All the best
Peter

PS: A great resource for xquery functions is http://www.xqueryfunctions.com/ 
You will find lots of stuff there ...

Am 04.09.2012 um 11:37 schrieb John Snelson:

> You're close - the missing part is that you want to use the sum() function, ie:
> 
> <short-desc>{
>  sum(
>    for $shrtdesc in $ltnfiles//subtopic[@id = 'DEF']//short-definition
>    return string-length($shrtdesc/p/text)
>  )
> }</short-desc>
> 
> John
> 
> On 04/09/12 10:15, Mailing Lists Mail wrote:
>> Dear Xquery Adults,
>> 
>> I am very new here.. I have some issues with getting my requirements
>> working.. I guess, this should be very simple, but dont seem to get my
>> heads around... THe following is what I want..
>> 
>> I have loads of XMLs in the Data Store ( markLogic)
>> 
>> I have to run a query to do the return the string lengths of certain
>> elements from 100 XMLs./
>> 
>> So my pseudo code will be :
>> 
>> <results>
>> For all XMLs in the Data store
>> {
>> 
>>    <file filename = "filename of the current file ">
>> 
>>           <desc> {length of desc at //xx/y/desc }
>>     <!--
>>    There could be more than one desc, in which case, i would like the
>> sum of all the string lengths of each desc
>> -->
>>  </desc>
>> 
>> <short-desc>
>> {
>>    string-length ( //xx//yy/short-desc)
>> }
>> 
>> </short-desc>
>> 
>>   </file>
>> 
>> </results>
>> 
>> 
>> My attempts:
>> 
>> 
>>  declare namespace dict = "http://www.lexis-nexis.com/glp/dict";
>> for $ltnfiles in doc()
>>   let $NAME:= document-uri($ltnfiles)
>> return (
>>  <ltn  >
>>   <name>
>>    {$NAME}
>>   </name>
>>    <short-desc>
>>    {string-length(string($ltnfiles//subtopic[@id =
>> 'DEF']/short-definition/p/text))}
>>    </short-desc>
>>  </ltn>
>> )
>> THe above works fine, if there is only one short-definition.. FOr more
>> than one shortdefiitions, it comes up with the error message obviously
>> because the string does not allow 2 arguments, while the stuff passed
>> into the function is a sequence...
>> 
>> How do I do another loop inside the first one, for each short-defiitin
>> ... SOmething like :
>> 
>> for $ltnfiles in doc()
>>   let $NAME:= document-uri($ltnfiles)
>> return (
>>  <ltn  >
>>   <name>
>>    {$NAME}
>>   </name>
>>    {
>>    for $shrtdesc in $ltnfiles//subtopic[@id = 'DEF']//short-definition
>>    <short-desc>
>>    {
>>    string-length( $shrtdesc/p/text)
>>    }
>>    </short-desc>
>>     }
>>  </ltn>
>> )
>>  DOEstNT WORK .. :( This wont give me what I want as What I want is a
>> sum (string-lengths of all the short-def) .. But atleast i thought i
>> would get something like
>> 
>> <short-desc>100</short-desc>
>> <short-desc>344</short-desc>
>> 
>> ( I would have liked <short-desc>444</short-desc> )
>> 
>> ANy help would be highly appreciated ..
>> 
>> Thanks
>> 
>> Dak.
>> 
>> 
> 
> 
> -- 
> John Snelson, Lead Engineer                    http://twitter.com/jpcs
> MarkLogic Corporation                         http://www.marklogic.com
> _______________________________________________
> talk at x-query.com
> http://x-query.com/mailman/listinfo/talk

-- 
Peter Stadler
Carl-Maria-von-Weber-Gesamtausgabe
Arbeitsstelle Detmold
Gartenstr. 20
D-32756 Detmold
Tel. +49 5231 975-665
Fax: +49 5231 975-668
stadler at weber-gesamtausgabe.de
www.weber-gesamtausgabe.de








More information about the talk mailing list