[MarkLogic Dev General] how to implement an accumulator
Eric Caplain
caplain at yahoo.com
Thu Mar 13 18:53:49 PST 2008
I've some XML like:
<speech>
<speaker>Joe</speaker>
<line>blah blah blah</line>
...
<line>this that and the other thing</line>
</speech>
I can get each speaker and then get each speech of that speaker and a count of lines in each speech, but I haven't been able to get the total count of all lines by a speaker.
In the following, the function line-count returns the count of lines in each speech, but I need to accumulate these counts and return that, and I haven't been able to figure out a way to do that. Any suggestions would be appreciated.
define function line-count($s as xs:string) as xs:integer {
let $count := 0
for $_speech in doc("speeches.xml")//speech
where some $_speaker in $_speech/speeker
satisfies ($_speaker/text() = $s)
return count($_speech/line)
}
<speakers>
{
let $speech := doc("speeches.xml")//speech
for $speaker in distinct-values($speech/speaker)
order by $speaker
return
<speaker>
<name>{$speaker}</name>
<count>{ line-count($speaker) }</count>
</speaker>
}
</speakers>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://xqzone.marklogic.com/pipermail/general/attachments/20080313/c4310b42/attachment.html
More information about the General
mailing list