[xquery-talk] Looping in Xquery- Braces?
John Snelson
john.snelson at marklogic.com
Tue Sep 4 03:04:20 PDT 2012
I think this error depends on the context of the two expressions you've
given below - which in and of themselves look fine.
In an expression context, you need a comma between two direct element
constructors:
<a/>, <b/>
When directly inside another direct element constructor, no comma is
necessary (because the syntax is emulating XML itself):
<root><a/><b/></root>
When you use curly braces inside a direct element constructor, you
return to an expression context, so commas are necessary again:
<root>{ <a/>, <b/> }</root>
You can mix the two styles:
<root>{ <a/>, <b/> }<c/><d/></root>
John
On 04/09/12 10:58, Mailing Lists Mail wrote:
> Here is what Stumps me about the braces.. Since JOhn's solution worked,
> I wanted to do the same for other element lengths...
> so I did
>
> <short-desc>{
> sum(
>
> for $shrtdesc in $ltnfiles//subtopic[@id = 'DEF']//short-definition
> return string-length($shrtdesc/p/__text)
> )
> }</short-desc>
>
>
> <long-desc>{
> sum(
>
> for $shrtdesc in $ltnfiles//subtopic[@id = 'DEF']//long-definition
> return string-length($shrtdesc/p/__text)
> )
> }</long-desc>
>
>
> Ended up getting the following error...
>
>
> At line 24 column 3:
>
> 22.
> 23. <long-desc>
> 24. {
> 25. sum(
> 26.
>
>
> ANy Ideas please?
>
>
>
>
>
>
> On Tue, Sep 4, 2012 at 10:37 AM, John Snelson
> <john.snelson at marklogic.com <mailto:john.snelson at marklogic.com>> wrote:
>
> 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
> <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 <mailto:talk at x-query.com>
> http://x-query.com/mailman/__listinfo/talk
> <http://x-query.com/mailman/listinfo/talk>
>
>
--
John Snelson, Lead Engineer http://twitter.com/jpcs
MarkLogic Corporation http://www.marklogic.com
More information about the talk
mailing list