File: fold-left-008.xq

package info (click to toggle)
qtxmlpatterns-opensource-src 5.15.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 309,144 kB
  • sloc: xml: 360,343; cpp: 91,994; ansic: 388; sh: 53; sed: 31; makefile: 23
file content (39 lines) | stat: -rw-r--r-- 1,106 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
xquery version "1.1";

(: fold-left-008 :)
(: author Michael Kay, Saxonica :)
(: get the employees who worked the maximum number of hours :)

(: insert-start :)
declare variable $input-context external;
(: insert-end :)

declare function local:hours($emp as element(employee)) as xs:integer {
  sum($emp/hours/xs:integer(.))
};

declare function local:highest($f as function(item()) as xs:anyAtomicType, $seq as item()*) {
    fold-left(
      function($highestSoFar as item()*, $this as item()*) as item()* {
        if (empty($highestSoFar))
        then $this
        else 
          let $thisValue := $f($this)
          let $highestValue := $f($highestSoFar[1])
          return
            if ($thisValue gt $highestValue)
            then $this
            else if ($thisValue eq $highestValue)
            then ($highestSoFar, $this)
            else $highestSoFar
      }, (), $seq)
};  


let $doc as document-node(element(works)) := $input-context return

<hardest-workers>{
  local:highest(local:hours#1, $doc/works/employee)
}</hardest-workers>