[xquery-talk] Looping in Xquery
John Snelson
john.snelson at marklogic.com
Tue Sep 4 02:37:35 PDT 2012
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
More information about the talk
mailing list