index.html
96.5 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
<?xml version="1.0" encoding="utf-8"?>
<!--XSLT Processor: SAXON 9.1.0.5 from Saxonica SAXON 9.1.0.5-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="EN" xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN">
<head>
<meta name="generator" content=
"HTML Tidy for Windows (vers 14 February 2006), see www.w3.org" />
<title>XQuery 3.0 Use Cases</title>
<style type="text/css">
/*<![CDATA[*/
code { font-family: monospace; }
div.constraint,
div.issue,
div.note,
div.notice { margin-left: 2em; }
div.issue
p.title { margin-left: -2em; }
ol.enumar { list-style-type: decimal; }
ol.enumla { list-style-type: lower-alpha; }
ol.enumlr { list-style-type: lower-roman; }
ol.enumua { list-style-type: upper-alpha; }
ol.enumur { list-style-type: upper-roman; }
li p { margin-top: 0.3em;
margin-bottom: 0.3em; }
sup small { font-style: italic;
color: #8F8F8F;
}
div.exampleInner pre { margin-left: 1em;
margin-top: 0em; margin-bottom: 0em}
div.exampleOuter {border: 4px double gray;
margin: 0em; padding: 0em}
div.exampleInner { background-color: #d5dee3;
border-top-width: 4px;
border-top-style: double;
border-top-color: #d3d3d3;
border-bottom-width: 4px;
border-bottom-style: double;
border-bottom-color: #d3d3d3;
padding: 4px; margin: 0em }
div.exampleWrapper { margin: 4px }
div.exampleHeader { font-weight: bold;
margin: 4px}
div.issue { border-bottom-color: black;
border-bottom-style: solid;
border-bottom-width: 1pt;
margin-bottom: 20pt;
}
th.issue-toc-head { border-bottom-color: black;
border-bottom-style: solid;
border-bottom-width: 1pt;
}
/*]]>*/
</style>
<link rel="stylesheet" type="text/css" href=
"http://www.w3.org/StyleSheets/TR/W3C-WD.css" />
</head>
<body>
<div class="head">
<p><a href="http://www.w3.org/"><img src=
"http://www.w3.org/Icons/w3c_home" alt="W3C" height="48" width=
"72" /></a></p>
<h1><a name="title" id="title"></a>XQuery 3.0 Use Cases</h1>
<h2><a name="w3c-doctype" id="w3c-doctype"></a>W3C Working Draft 14
December 2010</h2>
<dl>
<dt>This version:</dt>
<dd><a href=
"http://www.w3.org/TR/2010/WD-xquery-30-use-cases-20101214/">http://www.w3.org/TR/2010/WD-xquery-30-use-cases-20101214/</a></dd>
<dt>Latest version:</dt>
<dd><a href=
"http://www.w3.org/TR/xquery-30-use-cases/">http://www.w3.org/TR/xquery-30-use-cases/</a></dd>
<dt>Previous versions:</dt>
<dd><a href=
"http://www.w3.org/TR/2008/WD-xquery-11-use-cases-20081203/">http://www.w3.org/TR/2008/WD-xquery-11-use-cases-20081203/,</a>
<a href=
"http://www.w3.org/TR/2008/WD-xquery-11-use-cases-20080711/">http://www.w3.org/TR/2008/WD-xquery-11-use-cases-20080711/</a></dd>
<dt>Editor:</dt>
<dd>Tim Kraska, FLWOR Foundation <a href=
"mailto:tim@flworfound.org"><tim@flworfound.org></a></dd>
</dl>
<p>This document is also available in these non-normative formats:
<a href="xquery-30-use-cases.xml">XML</a>.</p>
<p class="copyright"><a href=
"http://www.w3.org/Consortium/Legal/ipr-notice#Copyright">Copyright</a> © 2010 <a href="http://www.w3.org/"><acronym title="World Wide Web Consortium">W3C</acronym></a><sup>®</sup>
(<a href="http://www.csail.mit.edu/"><acronym title=
"Massachusetts Institute of Technology">MIT</acronym></a>, <a href=
"http://www.ercim.eu/"><acronym title=
"European Research Consortium for Informatics and Mathematics">ERCIM</acronym></a>,
<a href="http://www.keio.ac.jp/">Keio</a>), All Rights Reserved.
W3C <a href=
"http://www.w3.org/Consortium/Legal/ipr-notice#Legal_Disclaimer">liability</a>,
<a href=
"http://www.w3.org/Consortium/Legal/ipr-notice#W3C_Trademarks">trademark</a>
and <a href=
"http://www.w3.org/Consortium/Legal/copyright-documents">document
use</a> rules apply.</p>
</div>
<hr />
<div>
<h2><a name="abstract" id="abstract"></a>Abstract</h2>
<p>This document specifies usage scenarios for XML Query (XQuery)
3.0.</p>
</div>
<div>
<h2><a name="status" id="status"></a>Status of this Document</h2>
<p><em>This section describes the status of this document at the
time of its publication. Other documents may supersede this
document. A list of current W3C publications and the latest
revision of this technical report can be found in the <a href=
"http://www.w3.org/TR/">W3C technical reports index</a> at
http://www.w3.org/TR/.</em></p>
<p>This is a <a href=
"http://www.w3.org/2005/10/Process-20051014/tr.html#maturity-levels">
Working Draft</a> as described in the <a href=
"http://www.w3.org/2005/10/Process-20051014/tr.html">Process
Document</a>. It has been developed by the W3C <a href=
"http://www.w3.org/XML/Query/">XML Query Working Group</a>, which
is part of the <a href="http://www.w3.org/XML/Activity">XML
Activity</a>. The Working Group expects to eventually publish this
document as a Working Group Note.</p>
<p>This document provides a number of use cases designed to
evaluate XQuery 3.0, the requirements for which are specified in
<a href="#xquery-30-requirements">[XQuery 3.0 Requirements]</a>.
Organizations and individuals should review this document to
ascertain whether or not adequate coverage of the requirements is
provided by these use cases.</p>
<p>This Working Draft is being published concurrently with the
XQuery 3.0 spec published on the same date. It has been updated to
include additional use cases and to align it with the correct
syntax of XQuery 3.0.</p>
<p>Please report errors in this document using W3C's <a href=
"http://www.w3.org/Bugs/Public/">public Bugzilla system</a>
(instructions can be found at <a href=
"http://www.w3.org/XML/2005/04/qt-bugzilla">http://www.w3.org/XML/2005/04/qt-bugzilla</a>).
If access to that system is not feasible, you may send your
comments to the W3C XSLT/XPath/XQuery public comments mailing list,
<a href=
"mailto:public-qt-comments@w3.org">public-qt-comments@w3.org</a>.
It will be very helpful if you include the string “[XQuery30UC]” in
the subject line of your report, whether made in Bugzilla or in
email. Please use multiple Bugzilla entries (or, if necessary,
multiple email messages) if you have more than one comment to make.
Archives of the comments and responses are available at <a href=
"http://lists.w3.org/Archives/Public/public-qt-comments/">http://lists.w3.org/Archives/Public/public-qt-comments/</a>.</p>
<p>Publication as a Working Draft does not imply endorsement by the
W3C Membership. This is a draft document and may be updated,
replaced or obsoleted by other documents at any time. It is
inappropriate to cite this document as other than work in
progress.</p>
<p>This document was produced by a group operating under the
<a href="http://www.w3.org/Consortium/Patent-Policy-20040205/">5
February 2004 W3C Patent Policy</a>. W3C maintains a <a href=
"http://www.w3.org/2004/01/pp-impl/18797/status#disclosures">public
list of any patent disclosures</a> made in connection with the
deliverables of the group; that page also includes instructions for
disclosing a patent. An individual who has actual knowledge of a
patent which the individual believes contains <a href=
"http://www.w3.org/Consortium/Patent-Policy-20040205/#def-essential">
Essential Claim(s)</a> must disclose the information in accordance
with <a href=
"http://www.w3.org/Consortium/Patent-Policy-20040205/#sec-Disclosure">
section 6 of the W3C Patent Policy</a>.</p>
</div>
<div class="toc">
<h2><a name="contents" id="contents"></a>Table of Contents</h2>
<p class="toc">1 <a href="#ucforxq30">Use Cases for XML Query
3.0</a><br />
2 <a href="#data">Shopping Scenario: Schema and Data</a><br />
    2.1 <a href="#dtdproducts">Schema</a><br />
    2.2 <a href="#dataproducts">Sample
Data</a><br />
3 <a href="#groupby">Use Case "Group By" - Queries which require
value-based grouping</a><br />
    3.1 <a href="#d2e229">Schema and Sample
Data</a><br />
        3.1.1 <a href=
"#dtdbooks">Schema Q7-Q8</a><br />
        3.1.2 <a href=
"#databooks">Sample Data Q7-Q8</a><br />
    3.2 <a href=
"#groupby-queries-results">Queries and Results</a><br />
        3.2.1 <a href=
"#groupby_q1">Q1</a><br />
        3.2.2 <a href=
"#groupby_q2">Q2</a><br />
        3.2.3 <a href=
"#groupby_q3">Q3</a><br />
        3.2.4 <a href=
"#groupby_q4">Q4</a><br />
        3.2.5 <a href=
"#groupby_q5">Q5</a><br />
        3.2.6 <a href=
"#groupby_q6">Q6</a><br />
        3.2.7 <a href=
"#groupby_b1">Q7</a><br />
        3.2.8 <a href=
"#groupby_b2">Q8</a><br />
4 <a href="#windowing">Use Case "Windowing" - Queries which require
windowing</a><br />
    4.1 <a href="#d2e431">Schema and Sample
Data</a><br />
        4.1.1 <a href=
"#dtdarrange_rows">XML Schema for Q1</a><br />
        4.1.2 <a href=
"#dataarrange_rows">Sample Data for Q1</a><br />
        4.1.3 <a href=
"#dtdhead_para">XML Schema for Q2</a><br />
        4.1.4 <a href=
"#datahead_para">Sample Data for Q2</a><br />
        4.1.5 <a href=
"#dtdterm_def_list">XML Schema for Q3</a><br />
        4.1.6 <a href=
"#dataterm_def_list">Sample Data for Q3</a><br />
        4.1.7 <a href=
"#dtddelayed_events_day1">XML Schema for Q4-Q6</a><br />
        4.1.8 <a href=
"#datadelayed_events_day1">Sample Data for Q4-Q6</a><br />
        4.1.9 <a href=
"#dtdday1">XML Schema for Q7-Q13</a><br />
        4.1.10 <a href=
"#dataday1">Sample Data for Q7-Q13</a><br />
        4.1.11 <a href=
"#dtdrss">XML Schema for Q14-Q16</a><br />
        4.1.12 <a href=
"#datarss">Sample Data for Q14-Q16</a><br />
        4.1.13 <a href=
"#dtdcXML">XML Schema for Q17-Q19</a><br />
        4.1.14 <a href=
"#datacXML">Sample Data for Q17-Q19</a><br />
    4.2 <a href=
"#windowing-queries-results">Queries and Results</a><br />
        4.2.1 <a href=
"#windowing_arrange_rows">Q1</a><br />
        4.2.2 <a href=
"#windowing_head_para">Q2</a><br />
        4.2.3 <a href=
"#windowing_term_def_list">Q3</a><br />
        4.2.4 <a href=
"#windowing_1moving_avarage_time">Q4</a><br />
        4.2.5 <a href=
"#windowing_2exponential_smoothing_1">Q5</a><br />
        4.2.6 <a href=
"#windowing_3outlier_detection">Q6</a><br />
        4.2.7 <a href=
"#windowing_1SEQ_A1h">Q7</a><br />
        4.2.8 <a href=
"#windowing_2SEQ_TimePerPerson">Q8</a><br />
        4.2.9 <a href=
"#windowing_2SEQ_TimePerPerson_overall">Q9</a><br />
        4.2.10 <a href=
"#windowing_3Seq_Not_A">Q10</a><br />
        4.2.11 <a href=
"#windowing_4SEQ_yA">Q11</a><br />
        4.2.12 <a href=
"#windowing_5NOSEQ_BothIn">Q12</a><br />
        4.2.13 <a href=
"#windowing_6AGG_Enter_3X_1">Q13</a><br />
        4.2.14 <a href=
"#windowing_annoying_authors">Q14</a><br />
        4.2.15 <a href=
"#windowing_interesting_topics">Q15</a><br />
        4.2.16 <a href=
"#windowing_summary_by_authors">Q16</a><br />
        4.2.17 <a href=
"#windowing_Q2_most_valuable_customer">Q17</a><br />
        4.2.18 <a href=
"#windowing_Q3_transaction_time">Q18</a><br />
        4.2.19 <a href=
"#windowing_Q4_ship_together">Q19</a><br />
5 <a href="#count-use-cases">Use Case "Count" - Queries which
require output numbering</a><br />
    5.1 <a href="#d2e942">Schema and Sample
Data</a><br />
    5.2 <a href="#d2e950">Queries and
Results</a><br />
        5.2.1 <a href=
"#count-numbering">Q1</a><br />
        5.2.2 <a href=
"#count-windowing">Q2</a><br />
        5.2.3 <a href=
"#count-top-k">Q3</a><br />
6 <a href="#outer-join-uc">Use Case "Outer For" - Queries which
require outer joins</a><br />
    6.1 <a href="#d2e1025">Schema and Sample
Data</a><br />
    6.2 <a href="#d2e1033">Queries and
Results</a><br />
        6.2.1 <a href=
"#outer-join-1">Q1</a><br />
        6.2.2 <a href=
"#simplified-flwor-2">Q2</a><br />
7 <a href="#try-catch-use-cases">Use Case "Try-Catch" - Queries
which require to recover from errors</a><br />
    7.1 <a href="#dataerrorproducts">Sample
Data</a><br />
    7.2 <a href="#d2e1096">Queries and
Results</a><br />
        7.2.1 <a href=
"#try-catch-1">Q1</a><br />
        7.2.2 <a href=
"#try-catch-2">Q2</a><br />
        7.2.3 <a href=
"#try-catch-3">Q3</a><br /></p>
<h3><a name="appendices" id="appendices"></a>Appendices</h3>
<p class="toc">A <a href="#acknowledgements">Acknowledgements</a>
(Non-Normative)<br />
B <a href="#ChangeLog">Change Log</a> (Non-Normative)<br />
    B.1 <a href="#id-2008-01-30">30 January
2008</a><br />
    B.2 <a href="#id-2008-11-17">17 November
2008</a><br />
C <a href="#references">References</a> (Non-Normative)<br /></p>
</div>
<hr />
<div class="body">
<div class="div1">
<h2><a name="ucforxq30" id="ucforxq30"></a>1 Use Cases for XML
Query 3.0</h2>
<p>The use cases listed below were created by the XML Query Working
Group to illustrate important applications for the new features in
XQuery 3.0. Each use case is focused on a specific application
area, and contains a XML Schema and example input data. Each use
case specifies a set of queries that might be applied to the input
data, and the expected results for each query. Since the English
description of each query is concise, the expected results form an
important part of the definition of each query, specifying the
expected output format.</p>
<p>Most of the listed use cases are based on a simple shopping
scenario described in section <a href="#data">Schema and Data</a>.
Use cases which require special data have a seperate subsection
describing the XML Schema and input data. All use cases assume that
input is provided in the form of one or more documents with
specific names. For instance, the products in a document may be
accessed with expressions like this:</p>
<div class="exampleInner">
<pre>
doc("products.xml")//product
</pre></div>
<p>Several implementors have asked us to make the queries from
these use cases available in a separate file in order to make it
easier for them to test their parsers. These queries may be found
in <a href="#UseCaseQueries">[Use Case Sample Queries]</a></p>
<p>To make output more readable, the output of queries has been
formatted using whitespace which may not be returned by a query
processor. This whitespace should not be considered normative for
the correctness of results.</p>
<p>These queries were tested with a dynamic implementation of
XQuery. Some queries may require additional type declarations to be
used with an implementation that implements the Static Typing
feature.</p>
</div>
<div class="div1">
<h2><a name="data" id="data"></a>2 Shopping Scenario: Schema and
Data</h2>
<p>If not stated otherwise all use cases in this document are based
on a simplified shopping scenario containing three documents. The
first document, products.xml, contains a list of products, whereas
stores.xml includes a list of stores, and sales-records.xml
contains the list of sales.</p>
<div class="div2">
<h3><a name="dtdproducts" id="dtdproducts"></a>2.1 Schema</h3>
<p>The document product.xml uses the following XML Schema:</p>
<div class="exampleInner">
<pre>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="products">
<xs:complexType>
<xs:sequence>
<xs:element name="product" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="category" type="xs:string"/>
<xs:element name="price" type="xs:double"/>
<xs:element name="cost" type="xs:double"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</pre></div>
<p>The sales-records.xml document follows this XML Schema:</p>
<div class="exampleInner">
<pre>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="sales">
<xs:complexType>
<xs:sequence>
<xs:element name="record" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="product-name" type="xs:string"/>
<xs:element name="store-number" type="xs:integer"/>
<xs:element name="qty" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</pre></div>
<p>The stores.xml document is valid to this XML Schema:</p>
<div class="exampleInner">
<pre>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="stores">
<xs:complexType>
<xs:sequence>
<xs:element name="store" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="store-number" type="xs:byte"/>
<xs:element name="state" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</pre></div>
</div>
<div class="div2">
<h3><a name="dataproducts" id="dataproducts"></a>2.2 Sample
Data</h3>
<p>The content of the document products.xml is:</p>
<div class="exampleInner">
<pre>
<products xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="products.xsd">
<product>
<name>broiler</name>
<category>kitchen</category>
<price>100</price>
<cost>70</cost>
</product>
<product>
<name>toaster</name>
<category>kitchen</category>
<price>30</price>
<cost>10</cost>
</product>
<product>
<name>blender</name>
<category>kitchen</category>
<price>50</price>
<cost>25</cost>
</product>
<product>
<name>socks</name>
<category>clothes</category>
<price>5</price>
<cost>2</cost>
</product>
<product>
<name>shirt</name>
<category>clothes</category>
<price>10</price>
<cost>3</cost>
</product>
</products>
</pre></div>
<p>The content of sales-records.xml is:</p>
<div class="exampleInner">
<pre>
<sales xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="sales.xsd">
<record>
<product-name>broiler</product-name>
<store-number>1</store-number>
<qty>20</qty>
</record>
<record>
<product-name>toaster</product-name>
<store-number>2</store-number>
<qty>100</qty>
</record>
<record>
<product-name>toaster</product-name>
<store-number>2</store-number>
<qty>50</qty>
</record>
<record>
<product-name>toaster</product-name>
<store-number>3</store-number>
<qty>50</qty>
</record>
<record>
<product-name>blender</product-name>
<store-number>3</store-number>
<qty>100</qty>
</record>
<record>
<product-name>blender</product-name>
<store-number>3</store-number>
<qty>150</qty>
</record>
<record>
<product-name>socks</product-name>
<store-number>1</store-number>
<qty>500</qty>
</record>
<record>
<product-name>socks</product-name>
<store-number>2</store-number>
<qty>10</qty>
</record>
<record>
<product-name>shirt</product-name>
<store-number>3</store-number>
<qty>10</qty>
</record>
</sales>
</pre></div>
<p>The content of stores.xml is:</p>
<div class="exampleInner">
<pre>
<stores xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="stores.xsd">
<store>
<store-number>1</store-number>
<state>CA</state>
</store>
<store>
<store-number>2</store-number>
<state>CA</state>
</store>
<store>
<store-number>3</store-number>
<state>MA</state>
</store>
<store>
<store-number>4</store-number>
<state>WA</state>
</store>
</stores>
</pre></div>
</div>
</div>
<div class="div1">
<h2><a name="groupby" id="groupby"></a>3 Use Case "Group By" -
Queries which require value-based grouping</h2>
<p>This use case contains several sample queries in which value
based grouping is needed.</p>
<div class="div2">
<h3><a name="d2e229" id="d2e229"></a>3.1 Schema and Sample
Data</h3>
<p>Queries 1-6 are based on a simplified shop scenario with
products, sales records and different shop locations. The schema
and data can be found in section <a href="#data">Schema and
Data</a>. Queries 7 and 8 are based on a bibliography document
shown below:</p>
<div class="div3">
<h4><a name="dtdbooks" id="dtdbooks"></a>3.1.1 Schema Q7-Q8</h4>
<p>Q7 and Q8 use an input document named "books.xml", with the
following XML Schema:</p>
<div class="exampleInner">
<pre>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="bib">
<xs:complexType>
<xs:sequence>
<xs:element name="book" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="author" type="xs:string" maxOccurs="unbounded"/>
<xs:element name="title" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</pre></div>
</div>
<div class="div3">
<h4><a name="databooks" id="databooks"></a>3.1.2 Sample Data
Q7-Q8</h4>
<p>Here are the contents of books.xml:</p>
<div class="exampleInner">
<pre>
<bib xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="books.xsd">
<book>
<author>Jim Melton</author>
<author>Alan Simon</author>
<title>SQL:1999</title>
</book>
<book>
<author>Jim Melton</author>
<title>Advanced SQL:1999</title>
</book>
<book>
<author>Alan Simon</author>
<title>Strategic Database Technology</title>
</book>
<book>
<author>Jim Melton</author>
<author>Andrew Eisenberg</author>
<title>Understanding SQL and Java Together</title>
</book>
<book>
<author>Jim Melton</author>
<author>Stephen Buxton</author>
<title>Querying XML</title>
</book>
</bib>
</pre></div>
</div>
</div>
<div class="div2">
<h3><a name="groupby-queries-results" id=
"groupby-queries-results"></a>3.2 Queries and Results</h3>
<div class="div3">
<h4><a name="groupby_q1" id="groupby_q1"></a>3.2.1 Q1</h4>
<p>Group sales by product, list name and total quantity of each
product.</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
<sales-qty-by-product>{
for $sales in doc("sales-records.xml")/*/record
let $pname := $sales/product-name
group by $pname
order by $pname
return
<product name="{$pname}">{
sum($sales/qty)
}</product>
}</sales-qty-by-product>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<sales-qty-by-product>
<product name="blender">250</product>
<product name="broiler">20</product>
<product name="shirt">10</product>
<product name="socks">510</product>
<product name="toaster">200</product>
</sales-qty-by-product>
</pre></div>
</div>
<div class="div3">
<h4><a name="groupby_q2" id="groupby_q2"></a>3.2.2 Q2</h4>
<p>Group sales by state (a property of the store) and category (a
property of the product). Order groups by the grouping keys. For
each group, show the total quantity sold.</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
<result>{
for $sales in doc("sales-records.xml")/*/record
let $state := doc("stores.xml")/*/store[store-number = $sales/store-number]/state
let $category := doc("products.xml")/*/product[name = $sales/product-name]/category
group by $state, $category
order by $state, $category
return
<group>
{$state, $category}
<total-qty>{sum($sales/qty)}</total-qty>
</group>
}</result>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<result>
<group>
<state>CA</state>
<category>clothes</category>
<total-qty>510</total-qty>
</group>
<group>
<state>CA</state>
<category>kitchen</category>
<total-qty>170</total-qty>
</group>
<group>
<state>MA</state>
<category>clothes</category>
<total-qty>10</total-qty>
</group>
<group>
<state>MA</state>
<category>kitchen</category>
<total-qty>300</total-qty>
</group>
</result>
</pre></div>
</div>
<div class="div3">
<h4><a name="groupby_q3" id="groupby_q3"></a>3.2.3 Q3</h4>
<p>Group sales by state (a property of the store) and category (a
property of the product). Order groups by the grouping keys. For
each group, show the total revenue (defined as sales/qty *
product/price).</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
<result>{
for $sales in doc("sales-records.xml")/*/record
let $state := doc("stores.xml")/*/store[store-number = $sales/store-number]/state,
$product := doc("products.xml")/*/product[name = $sales/product-name],
$category := $product/category,
$revenue := $sales/qty * $product/price
group by $state, $category
order by $state, $category
return
<group>
{$state, $category}
<total-revenue>{sum($revenue)}</total-revenue>
</group>
}</result>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<result>
<group>
<state>CA</state>
<category>clothes</category>
<total-revenue>2550</total-revenue>
</group>
<group>
<state>CA</state>
<category>kitchen</category>
<total-revenue>6500</total-revenue>
</group>
<group>
<state>MA</state>
<category>clothes</category>
<total-revenue>100</total-revenue>
</group>
<group>
<state>MA</state>
<category>kitchen</category>
<total-revenue>14000</total-revenue>
</group>
</result>
</pre></div>
</div>
<div class="div3">
<h4><a name="groupby_q4" id="groupby_q4"></a>3.2.4 Q4</h4>
<p>Combine the input documents into a three-level hierarchy based
on state, category, and product. At the product level, show the
total quantity sold of each product. List items alphabetically at
each level of the hierarchy.</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
<result>{
for $store in doc("stores.xml")/*/store
let $state := $store/state
group by $state
order by $state
return
<state name="{$state}">{
for $product in doc("products.xml")/*/product
let $category := $product/category
group by $category
order by $category
return
<category name="{$category}">{
for $sales in doc("sales-records.xml")/*/record[store-number = $store/store-number
and product-name = $product/name]
let $pname := $sales/product-name
group by $pname
order by $pname
return
<product name="{$pname}" total-qty="{sum($sales/qty)}" />
}</category>
}</state>
}</result>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<result>
<state name="CA">
<category name="clothes">
<product name="socks" total-qty="510"/>
</category>
<category name="kitchen">
<product name="broiler" total-qty="20"/>
<product name="toaster" total-qty="150"/>
</category>
</state>
<state name="MA">
<category name="clothes">
<product name="shirt" total-qty="10"/>
</category>
<category name="kitchen">
<product name="blender" total-qty="250"/>
<product name="toaster" total-qty="50"/>
</category>
</state>
</result>
</pre></div>
</div>
<div class="div3">
<h4><a name="groupby_q5" id="groupby_q5"></a>3.2.5 Q5</h4>
<p>List all stores in ascending order by store number. For each
store, list the products sold in that store, in descending order by
quantity sold. Illustrates ordering among and within groups.</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
<result>{
for $sales in doc("sales-records.xml")/*/record
let $storeno := $sales/store-number
group by $storeno
order by $storeno
return
<store number = "{$storeno}">{
for $s in $sales
order by xs:int($s/qty) descending
return
<product name = "{$s/product-name}" qty = "{$s/qty}"/>
}</store>
}</result>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<result>
<store number="1">
<product name="socks" qty="500"/>
<product name="broiler" qty="20"/>
</store>
<store number="2">
<product name="toaster" qty="100"/>
<product name="toaster" qty="50"/>
<product name="socks" qty="10"/>
</store>
<store number="3">
<product name="blender" qty="150"/>
<product name="blender" qty="100"/>
<product name="toaster" qty="50"/>
<product name="shirt" qty="10"/>
</store>
</result>
</pre></div>
</div>
<div class="div3">
<h4><a name="groupby_q6" id="groupby_q6"></a>3.2.6 Q6</h4>
<p>List all stores whose total profit is greater than 100, in
descending order by total profit. Note: total profit for a store is
the sum over all sales in that store, of the quantity sold times
the difference between price and cost for the item sold.
Illustrates cross-document computation, filtering of groups,
ordering by a non-grouping key.</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
<result>{
for $sales in doc("sales-records.xml")/*/record
let $storeno := $sales/store-number,
$product := doc("products.xml")/*/product[name = $sales/product-name],
$prd := $product,
$profit := $sales/qty * ($prd/price - $prd/cost)
group by $storeno
let $total-store-profit := sum($profit)
where $total-store-profit > 100
order by $total-store-profit descending
return
<store number = "{$storeno}" total-profit = "{$total-store-profit}"/>
}</result>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<result>
<store number="3" total-profit="7320"/>
<store number="2" total-profit="3030"/>
<store number="1" total-profit="2100"/>
</result>
</pre></div>
</div>
<div class="div3">
<h4><a name="groupby_b1" id="groupby_b1"></a>3.2.7 Q7</h4>
<p>Group books by author. Create a group for each individual
author. A book with multiple authors should appear in the groups
for each of its authors. Alphabetize the authors and the book
titles within each author group.</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
<result>{
for $book in doc("books.xml")/*/book
for $author in $book/author
group by $author
order by $author
return
<author name="{$author}">{
for $b in $book
order by $b/title
return
<title> {fn:data($b/title)} </title>
}</author>
}</result>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<result>
<author name="Alan Simon">
<title>SQL:1999</title>
<title>Strategic Database Technology</title>
</author>
<author name="Andrew Eisenberg">
<title>Understanding SQL and Java Together</title>
</author>
<author name="Jim Melton">
<title>Advanced SQL:1999</title>
<title>Querying XML</title>
<title>SQL:1999</title>
<title>Understanding SQL and Java Together</title>
</author>
<author name="Stephen Buxton">
<title>Querying XML</title>
</author>
</result>
</pre></div>
</div>
<div class="div3">
<h4><a name="groupby_b2" id="groupby_b2"></a>3.2.8 Q8</h4>
<p>Group books by author. Create a group for each distinct ordered
list of authors. Each book should be grouped with other books that
have the same ordered list of authors. Alphabetize the book titles
within each group.</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
<result>{
for $book in doc("books.xml")/*/book
let $author-list := fn:string-join($book/author, ', ')
group by $author-list
order by $author-list
return
<author-list names="{$author-list}">{
for $b in $book
order by $b/title
return
<title> {fn:data($b/title)} </title>
}</author-list>
}</result>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<result>
<author-list names="Alan Simon">
<title>Strategic Database Technology</title>
</author-list>
<author-list names="Jim Melton">
<title>Advanced SQL:1999</title>
</author-list>
<author-list names="Jim Melton, Alan Simon">
<title>SQL:1999</title>
</author-list>
<author-list names="Jim Melton, Andrew Eisenberg">
<title>Understanding SQL and Java Together</title>
</author-list>
<author-list names="Jim Melton, Stephen Buxton">
<title>Querying XML</title>
</author-list>
</result>
</pre></div>
</div>
</div>
</div>
<div class="div1">
<h2><a name="windowing" id="windowing"></a>4 Use Case "Windowing" -
Queries which require windowing</h2>
<p>This use case covers queries that require windowing or
positional grouping, which can be seen as a special form of
windowing. Windowing means that the queries require selecting
subsequences based on certain characterisics of an underlying
sequence.</p>
<div class="div2">
<h3><a name="d2e431" id="d2e431"></a>4.1 Schema and Sample
Data</h3>
<p>Windowing is required in various scenarios from formatting up to
streaming applications. The listed queries try to cover different
scenarios and require therefore also various types of input data.
The following sections describe for every query the used data and
the according schema.</p>
<div class="div3">
<h4><a name="dtdarrange_rows" id="dtdarrange_rows"></a>4.1.1 XML
Schema for Q1</h4>
<p>Q1 uses an input document named arrange_rows.xml, with the
following XML Schema arrange_rows.xsd:</p>
<div class="exampleInner">
<pre>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="doc">
<xs:complexType>
<xs:sequence>
<xs:element name="data" type="xs:string" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</pre></div>
</div>
<div class="div3">
<h4><a name="dataarrange_rows" id="dataarrange_rows"></a>4.1.2
Sample Data for Q1</h4>
<p>The content of arrange_rows.xml is:</p>
<div class="exampleInner">
<pre>
<doc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="arrange_rows.xsd">
<data>Green</data>
<data>Pink</data>
<data>Lilac</data>
<data>Turquoise</data>
<data>Peach</data>
<data>Opal</data>
<data>Champagne</data>
</doc>
</pre></div>
</div>
<div class="div3">
<h4><a name="dtdhead_para" id="dtdhead_para"></a>4.1.3 XML Schema
for Q2</h4>
<p>Q2 uses an input document named head_para.xml, with the
following XML Schema named head_para.xsd:</p>
<div class="exampleInner">
<pre>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="body">
<xs:complexType>
<xs:sequence>
<xs:sequence maxOccurs="unbounded">
<xs:element name="h2" type="xs:string"/>
<xs:element name="p" type="xs:string" maxOccurs="unbounded"/>
</xs:sequence>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</pre></div>
</div>
<div class="div3">
<h4><a name="datahead_para" id="datahead_para"></a>4.1.4 Sample
Data for Q2</h4>
<p>The content of head_para.xml is:</p>
<div class="exampleInner">
<pre>
<body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="head_para.xsd">
<h2>heading1</h2>
<p>para1</p>
<p>para2</p>
<h2>heading2</h2>
<p>para3</p>
<p>para4</p>
<p>para5</p>
</body>
</pre></div>
</div>
<div class="div3">
<h4><a name="dtdterm_def_list" id="dtdterm_def_list"></a>4.1.5 XML
Schema for Q3</h4>
<p>Q3 uses an input document named term_def_list.xml, with the
following XML Schema named term_def_list.xsd:</p>
<div class="exampleInner">
<pre>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="doc">
<xs:complexType>
<xs:sequence>
<xs:sequence maxOccurs="unbounded">
<xs:element name="dt" type="xs:string" maxOccurs="unbounded"/>
<xs:element name="dd" type="xs:string" maxOccurs="unbounded"/>
</xs:sequence>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</pre></div>
</div>
<div class="div3">
<h4><a name="dataterm_def_list" id="dataterm_def_list"></a>4.1.6
Sample Data for Q3</h4>
<p>The content of term_def_list.xml is:</p>
<div class="exampleInner">
<pre>
<doc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="term_def_list.xsd">
<dt>XML</dt>
<dd>Extensible Markup Language</dd>
<dt>XSLT</dt>
<dt>XSL Transformations</dt>
<dd>A language for transforming XML</dd>
<dd>A specification produced by W3C</dd>
</doc>
</pre></div>
</div>
<div class="div3">
<h4><a name="dtddelayed_events_day1" id=
"dtddelayed_events_day1"></a>4.1.7 XML Schema for Q4-Q6</h4>
<p>Q4 - Q6 use an input document named temp_events.xml, with the
following XML Schema named temp_events.xsd:</p>
<div class="exampleInner">
<pre>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="stream">
<xs:complexType>
<xs:sequence>
<xs:element name="event" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="time" type="xs:integer" use="required"/>
<xs:attribute name="temp" type="xs:double" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</pre></div>
</div>
<div class="div3">
<h4><a name="datadelayed_events_day1" id=
"datadelayed_events_day1"></a>4.1.8 Sample Data for Q4-Q6</h4>
<p>The content of temp_events.xml is:</p>
<div class="exampleInner">
<pre>
<stream xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="temp_events.xsd">
<event temp="10" time="1"/>
<event temp="8" time="2"/>
<event temp="6" time="3"/>
<event temp="13" time="4"/>
<event temp="33" time="5"/>
<event temp="10" time="6"/>
<event temp="10" time="7"/>
</stream>
</pre></div>
</div>
<div class="div3">
<h4><a name="dtdday1" id="dtdday1"></a>4.1.9 XML Schema for
Q7-Q13</h4>
<p>Q7 - Q13 use an input document named person_events.xml, with the
following XML Schema named person_events.xsd:</p>
<div class="exampleInner">
<pre>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="stream">
<xs:complexType>
<xs:sequence>
<xs:element name="event" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence minOccurs="0">
<xs:element name="person" type="xs:string"/>
<xs:element name="direction" type="xs:string"/>
</xs:sequence>
<xs:attribute name="time" type="xs:dateTime" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</pre></div>
</div>
<div class="div3">
<h4><a name="dataday1" id="dataday1"></a>4.1.10 Sample Data for
Q7-Q13</h4>
<p>The content of person_events.xml is:</p>
<div class="exampleInner">
<pre>
<?xml version="1.0" encoding="UTF-8"?>
<stream xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="person_events.xsd">
<event time="2006-01-01T01:00:00-00:00"/>
<event time="2006-01-01T10:30:00-00:00">
<person>Anton</person>
<direction>in</direction>
</event>
<event time="2006-01-01T11:00:00-00:00">
<person>Barbara</person>
<direction>in</direction>
</event>
<event time="2006-01-01T11:15:00-00:00">
<person>Clara</person>
<direction>in</direction>
</event>
<event time="2006-01-01T12:15:00-00:00">
<person>Clara</person>
<direction>out</direction>
</event>
<event time="2006-01-01T14:00:00-00:00">
<person>Barbara</person>
<direction>out</direction>
</event>
<event time="2006-01-01T15:00:00-00:00">
<person>Anton</person>
<direction>out</direction>
</event>
<event time="2006-01-01T23:00:00-00:00"/>
<event time="2006-01-02T01:00:00-00:00"/>
<event time="2006-01-02T11:00:00-00:00">
<person>Anton</person>
<direction>in</direction>
</event>
<event time="2006-01-02T12:00:00-00:00">
<person>Clara</person>
<direction>in</direction>
</event>
<event time="2006-01-02T12:10:00-00:00">
<person>Clara</person>
<direction>out</direction>
</event>
<event time="2006-01-02T12:15:00-00:00">
<person>Clara</person>
<direction>in</direction>
</event>
<event time="2006-01-02T12:20:00-00:00">
<person>Clara</person>
<direction>out</direction>
</event>
<event time="2006-01-02T12:25:00-00:00">
<person>Clara</person>
<direction>in</direction>
</event>
<event time="2006-01-02T12:40:00-00:00">
<person>Clara</person>
<direction>out</direction>
</event>
<event time="2006-01-02T14:00:00-00:00">
<person>Clara</person>
<direction>in</direction>
</event>
<event time="2006-01-02T16:00:00-00:00">
<person>Anton</person>
<direction>out</direction>
</event>
<event time="2006-01-02T16:15:00-00:00">
<person>Clara</person>
<direction>out</direction>
</event>
<event time="2006-01-02T23:00:00-00:00"/>
</stream>
</pre></div>
</div>
<div class="div3">
<h4><a name="dtdrss" id="dtdrss"></a>4.1.11 XML Schema for
Q14-Q16</h4>
<p>Q14 - Q16 use as input document a slightly modified RSS document
named rss.xml. It mainly differs from RSS 2.0 in the way dates are
expressed in the data. The XML Schema, rss.xsd, is as follows:</p>
<div class="exampleInner">
<pre>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="rss">
<xs:complexType>
<xs:sequence>
<xs:element name="channel">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="link" type="xs:anyURI"/>
<xs:element name="description" type="xs:string"/>
<xs:element name="language" type="xs:string"/>
<xs:element ref="item" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="category" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="pubDate" type="xs:dateTime"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</pre></div>
</div>
<div class="div3">
<h4><a name="datarss" id="datarss"></a>4.1.12 Sample Data for
Q14-Q16</h4>
<p>The content of rss.xml is:</p>
<div class="exampleInner">
<pre>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="rss.xsd">
<channel>
<title>DBIS RSS</title>
<link>http://www.dbis.ethz.ch</link>
<description>The windowing dummy RSS.</description>
<language>en-us</language>
<item>
<title>Why use cases are important Part 1.</title>
<category>Workshop</category>
<author>rokas@e-mail.de</author>
<pubDate>2003-06-03T09:00:00</pubDate>
</item>
<item>
<title>Why use cases are important Part 2.</title>
<category>Workshop</category>
<author>rokas@e-mail.de</author>
<pubDate>2003-06-03T09:00:00</pubDate>
</item>
<item>
<title>Why use cases are important Part 3.</title>
<category>Workshop</category>
<author>rokas@e-mail.de</author>
<pubDate>2003-06-03T10:00:00</pubDate>
</item>
<item>
<title>Extending XQuery with Window Functions</title>
<category>Talk</category>
<author>tim@e-mail.de</author>
<pubDate>2003-06-03T11:00:00</pubDate>
</item>
<item>
<title>XQueryP: A new programming language is born</title>
<category>Talk</category>
<author>david@e-mail.de</author>
<pubDate>2003-06-03T12:00:00</pubDate>
</item>
<item>
<title>Why use cases are annoying to write.</title>
<category>Talk</category>
<author>rokas@e-mail.de</author>
<pubDate>2003-06-04T10:00:00</pubDate>
</item>
</channel>
</rss>
</pre></div>
</div>
<div class="div3">
<h4><a name="dtdcXML" id="dtdcXML"></a>4.1.13 XML Schema for
Q17-Q19</h4>
<p>Q17-Q19 use an input document named cxml.xml. The structure of
this document is inspired by the Commerce XML Resource standard
(cXML). The document contains a sequence of events, where an event
corresponds to a simplified message of the cXML standard or an
timeclock event. The XML Schema with the file-name cxml.xsd is as
follows:</p>
<div class="exampleInner">
<pre>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="sequence">
<xs:complexType>
<xs:sequence>
<xs:choice maxOccurs="unbounded">
<xs:element name="time" type="TimeEvent"/>
<xs:element name="OrderRequest" type="OrderRequest" maxOccurs="unbounded"/>
<xs:element name="ConfirmationRequest" type="ConfirmationRequest"/>
<xs:element name="ShipNotice" type="ShipNotice" maxOccurs="unbounded"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="TimeEvent">
<xs:attribute name="date" type="xs:dateTime" use="required"/>
</xs:complexType>
<xs:complexType name="OrderRequest">
<xs:sequence>
<xs:element name="Item" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="unitPrice" type="xs:byte" use="required"/>
<xs:attribute name="quantity" type="xs:byte" use="required"/>
<xs:attribute name="partID" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="type" type="xs:string" use="required"/>
<xs:attribute name="total" type="xs:short" use="required"/>
<xs:attribute name="shipTo" type="xs:string"/>
<xs:attribute name="orderID" type="xs:string" use="required"/>
<xs:attribute name="date" type="xs:dateTime" use="required"/>
<xs:attribute name="billTo" type="xs:string" use="required"/>
</xs:complexType>
<xs:complexType name="ConfirmationRequest">
<xs:attribute name="status" type="xs:string" use="required"/>
<xs:attribute name="orderID" type="xs:string" use="required"/>
<xs:attribute name="date" type="xs:dateTime" use="required"/>
<xs:attribute name="confirmID" type="xs:string" use="required"/>
</xs:complexType>
<xs:complexType name="ShipNotice">
<xs:attribute name="orderID" type="xs:string" use="required"/>
<xs:attribute name="date" type="xs:dateTime" use="required"/>
</xs:complexType>
</xs:schema>
</pre></div>
</div>
<div class="div3">
<h4><a name="datacXML" id="datacXML"></a>4.1.14 Sample Data for
Q17-Q19</h4>
<p>The content of cxml.xml is:</p>
<div class="exampleInner">
<pre>
<sequence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="cxml.xsd">
<time date="2006-01-01T00:00:00-00:00"/>
<OrderRequest billTo="ACME1" date="2006-01-01T10:00:00-00:00"
orderID="OID01" shipTo="ACME1" total="1100" type="new">
<Item partID="ID1" quantity="10" unitPrice="100"/>
<Item partID="ID2" quantity="10" unitPrice="10"/>
</OrderRequest>
<OrderRequest billTo="ACME2" date="2006-01-01T11:00:00-00:00"
orderID="OID02" total="100" type="new">
<Item partID="ID2" quantity="10" unitPrice="10"/>
</OrderRequest>
<ConfirmationRequest confirmID="C1" date="2006-01-01T18:00:00-00:00"
orderID="OID02" status="reject"/>
<time date="2006-01-02T00:00:00-00:00"/>
<ConfirmationRequest confirmID="C1" date="2006-01-02T08:00:00-00:00"
orderID="OID01" status="accept"/>
<OrderRequest billTo="ACME1" date="2006-01-02T14:00:00-00:00"
orderID="OID03" shipTo="ACME1" total="10000" type="new">
<Item partID="ID3" quantity="100" unitPrice="100"/>
</OrderRequest>
<ConfirmationRequest confirmID="C1" date="2006-01-02T16:00:00-00:00"
orderID="OID03" status="accept"/>
<time date="2006-01-03T00:00:00-00:00"/>
<time date="2006-01-04T00:00:00-00:00"/>
<time date="2006-01-05T00:00:00-00:00"/>
<ShipNotice date="2006-01-05T08:00:00-00:00" orderID="OID01"/>
<ShipNotice date="2006-01-05T09:00:00-00:00" orderID="OID03"/>
<time date="2006-01-06T00:00:00-00:00"/>
<OrderRequest billTo="ACME2" date="2006-01-06T08:00:00-00:00"
orderID="OID04" total="100" type="new">
<Item partID="ID2" quantity="10" unitPrice="10"/>
</OrderRequest>
<time date="2006-01-07T00:00:00-00:00"/>
</sequence>
</pre></div>
</div>
</div>
<div class="div2">
<h3><a name="windowing-queries-results" id=
"windowing-queries-results"></a>4.2 Queries and Results</h3>
<div class="div3">
<h4><a name="windowing_arrange_rows" id=
"windowing_arrange_rows"></a>4.2.1 Q1</h4>
<p>Arrange a sequence of items as a table with three columns (using
as many rows as necessary).</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
declare variable $seq := fn:doc("arrange_rows.xml");
<table>{
for tumbling window $w in $seq/doc/*
start at $x when fn:true()
end at $y when $y - $x = 2
return
<tr>{
for $i in $w
return
<td>{data($i)}</td>
}</tr>
}</table>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<table>
<tr>
<td>Green</td>
<td>Pink</td>
<td>Lilac</td>
</tr>
<tr>
<td>Turquoise</td>
<td>Peach</td>
<td>Opal</td>
</tr>
<tr>
<td>Champagne</td>
</tr>
</table>
</pre></div>
</div>
<div class="div3">
<h4><a name="windowing_head_para" id=
"windowing_head_para"></a>4.2.2 Q2</h4>
<p>Convert a structure with implicit sections to a structure with
explicit sections.</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
declare variable $seq := fn:doc("head_para.xml");
<chapter>{
for tumbling window $w in $seq/body/*
start previous $s when $s[self::h2]
end next $e when $e[self::h2]
return
<section title="{data($s)}">{
for $x in $w
return
<para>{data($x)}</para>
}</section>
}</chapter>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<chapter>
<section title="heading1">
<para>para1</para>
<para>para2</para>
</section>
<section title="heading2">
<para>para3</para>
<para>para4</para>
<para>para5</para>
</section>
</chapter>
</pre></div>
</div>
<div class="div3">
<h4><a name="windowing_term_def_list" id=
"windowing_term_def_list"></a>4.2.3 Q3</h4>
<p>Within a glossary in HTML, a defined term <dt> can be
followed by a definition <dd>. The task is to group these
together within a <term> element, where a group can consist
of one or more <dt> elements followed by one or more
<dd> elements.</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
declare variable $seq := fn:doc("term_def_list.xml");
<doc>{
for tumbling window $w in $seq/doc/*
start $x when $x[self::dt]
end $y next $z when $y[self::dd] and $z[self::dt]
return
<term>{
$w
}</term>
}</doc>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<doc>
<term>
<dt>XML</dt>
<dd>Extensible Markup Language</dd>
</term>
<term>
<dt>XSLT</dt>
<dt>XSL Transformations</dt>
<dd>A language for transforming XML</dd>
<dd>A specification produced by W3C</dd>
</term>
</doc>
</pre></div>
</div>
<div class="div3">
<h4><a name="windowing_1moving_avarage_time" id=
"windowing_1moving_avarage_time"></a>4.2.4 Q4</h4>
<p>Calculate the moving average of temperature values for the 3
last seconds.</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
declare variable $timesequence := fn:doc("temp_events.xml");
let $MAX_DIFF := 2
for sliding window $w in $timesequence/stream/event
start $s_curr at $s_pos previous $s_prev
when ($s_curr/@time ne $s_prev/@time) or (empty($s_prev))
only end next $e_next
when $e_next/@time - $s_curr/@time gt $MAX_DIFF
return
avg( $w/@temp )
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
8 9 17 18
</pre></div>
</div>
<div class="div3">
<h4><a name="windowing_2exponential_smoothing_1" id=
"windowing_2exponential_smoothing_1"></a>4.2.5 Q5</h4>
<p>Single exponential smoothing (3 last values and smoothing factor
0.2)</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
declare variable $timesequence := fn:doc("temp_events.xml");
let $SMOOTH_CONST := 0.2
for sliding window $w in $timesequence/stream/event
start at $s_pos when true()
only end at $e_pos when $e_pos - $s_pos eq 2
return
round-half-to-even($SMOOTH_CONST * data($w[3]/@temp) + (1 - $SMOOTH_CONST) *
( $SMOOTH_CONST * data($w[2]/@temp) +
(1 - $SMOOTH_CONST) * data($w[1]/@temp) ), 2)
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
8.88 8.68 12.32 15.24 23.92
</pre></div>
</div>
<div class="div3">
<h4><a name="windowing_3outlier_detection" id=
"windowing_3outlier_detection"></a>4.2.6 Q6</h4>
<p>Detect outliers (current value is two times higher (lower) than
the average of the previous three values) in a sequence of temp
values.</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
declare variable $seq := fn:doc("temp_events.xml");
for sliding window $w in $seq/stream/event
start $s_curr when fn:true()
only end next $next when $next/@time > $s_curr/@time + 3
return
let $avg := fn:avg($w/@temp)
where $avg * 2 lt xs:double($next/@temp) or $avg div 2 gt xs:double($next/@temp)
return <alarm>Outlier detected. Event id:{data($next/@time)}</alarm>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<alarm>Outlier detected. Event id:5</alarm>
</pre></div>
</div>
<div class="div3">
<h4><a name="windowing_1SEQ_A1h" id="windowing_1SEQ_A1h"></a>4.2.7
Q7</h4>
<p>Notify when Barbara enters the building within 1 hour after
Anton</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
declare variable $seq := fn:doc("mydoc.xml");
<result>{
for tumbling window $w in $seq/stream/event
start $s when $s/person eq "Anton" and $s/direction eq "in"
only end $e next $n when xs:dateTime($n/@time) - xs:dateTime($s/@time) gt
xs:dayTimeDuration("PT1H")
or ($e/person eq "Barbara" and $e/direction eq "in")
or ($e/person eq "Anton" and $e/direction eq "out")
where $e/person eq "Barbara" and $e/direction eq "in"
return
<warning time="{ $e/@time }">Barbara: Anton arrived 1h ago</warning>
}</result>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<result>
<warning time="2006-01-01T11:00:00-00:00">Barbara: Anton arrived 1h ago</warning>
</result>
</pre></div>
</div>
<div class="div3">
<h4><a name="windowing_2SEQ_TimePerPerson" id=
"windowing_2SEQ_TimePerPerson"></a>4.2.8 Q8</h4>
<p>Measure the working time of each person</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
declare variable $seq := fn:doc("person_events.xml");
<result>{
for sliding window $w in $seq/stream/event
start $s when $s/direction eq "in"
only end $e when $s/person eq $e/person and
$e/direction eq "out"
return
<working-time>
{$s/person}
<time>{ xs:dateTime($e/@time) - xs:dateTime($s/@time)}</time>
</working-time>
}</result>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<result>
<working-time>
<person>Anton</person>
<time>PT4H30M</time>
</working-time>
<working-time>
<person>Barbara</person>
<time>PT3H</time>
</working-time>
<working-time>
<person>Clara</person>
<time>PT1H</time>
</working-time>
<working-time>
<person>Anton</person>
<time>PT5H</time>
</working-time>
<working-time>
<person>Clara</person>
<time>PT10M</time>
</working-time>
<working-time>
<person>Clara</person>
<time>PT5M</time>
</working-time>
<working-time>
<person>Clara</person>
<time>PT15M</time>
</working-time>
<working-time>
<person>Clara</person>
<time>PT4H15M</time>
</working-time>
</result>
</pre></div>
</div>
<div class="div3">
<h4><a name="windowing_2SEQ_TimePerPerson_overall" id=
"windowing_2SEQ_TimePerPerson_overall"></a>4.2.9 Q9</h4>
<p>Measure the overall working time for each person.</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
declare variable $seq := fn:doc("person_events.xml");
<result>{
for sliding window $w in $seq/stream/event
start $s when $s/direction eq "in"
only end $e when $s/person eq $e/person and
$e/direction eq "out"
let $person := $s/person
let $workingTime := xs:dateTime($e/@time) - xs:dateTime($s/@time)
group by $person
return
<working-time>
{$person}
<time>{ sum($workingTime) }</time>
</working-time>
}</result>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<result>
<working-time>
<person>Barbara</person>
<time>PT3H</time>
</working-time>
<working-time>
<person>Anton</person>
<time>PT9H30M</time>
</working-time>
<working-time>
<person>Clara</person>
<time>PT5H15M</time>
</working-time>
</result>
</pre></div>
</div>
<div class="div3">
<h4><a name="windowing_3Seq_Not_A" id=
"windowing_3Seq_Not_A"></a>4.2.10 Q10</h4>
<p>Display a warning if Barbara does not come to work.</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
declare variable $seq := fn:doc("person_events.xml");
<result>{
for tumbling window $w in $seq/stream/event[direction eq "in"]
start $s when fn:true()
end next $e when xs:date( xs:dateTime($s/@time) ) ne xs:date( xs:dateTime($e/@time) )
let $date := xs:date(xs:dateTime($s/@time))
where not($w[person eq "Barbara"])
return <alert date="{ $date }">Barbara did not come to work</alert>
}</result>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<result>
<alert date="2006-01-02Z">Barbara did not come to work</alert>
</result>
</pre></div>
</div>
<div class="div3">
<h4><a name="windowing_4SEQ_yA" id="windowing_4SEQ_yA"></a>4.2.11
Q11</h4>
<p>Identify every person who enters the building before Clara
withing a 15 minute timeframe (Clara's arrival time - 15
minutes).</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
declare variable $seq := fn:doc("person_events.xml");
<results>{
for tumbling window $w in $seq/stream/event[direction eq "in"]
start when true()
only end next $x when $x/person eq "Clara"
return
<result time="{ $x/@time }">{
distinct-values(for $y in $w
where (xs:dateTime($y/@time) + xs:dayTimeDuration("PT15M") ) ge xs:dateTime($x/@time)
return $y/person)
}</result>
}</results>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<results>
<result time="2006-01-01T11:15:00-00:00">Barbara</result>
<result time="2006-01-02T12:00:00-00:00"/>
<result time="2006-01-02T12:15:00-00:00">Clara</result>
<result time="2006-01-02T12:25:00-00:00">Clara</result>
<result time="2006-01-02T14:00:00-00:00"/>
</results>
</pre></div>
</div>
<div class="div3">
<h4><a name="windowing_5NOSEQ_BothIn" id=
"windowing_5NOSEQ_BothIn"></a>4.2.12 Q12</h4>
<p>Notify when both Anton and Barbara enter the office within 30
minutes of one another.</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
declare variable $seq := fn:doc("person_events.xml");
<result>{
for tumbling window $w in $seq/stream/event[direction eq "in"]
start $x when $x/person = ("Barbara", "Anton")
end next $y when xs:dateTime($y/@time) - xs:dateTime($x/@time) gt xs:dayTimeDuration("PT30M")
where $w[person eq "Anton"] and $w[person eq "Barbara"]
return
<alert date="{ xs:dateTime($y/@time) }">Anton and Barbara just arrived</alert>
}</result>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<result>
<alert time="2006-01-02T14:00:00-00:00">Clara is suspicious</alert>
</result>
</pre></div>
</div>
<div class="div3">
<h4><a name="windowing_6AGG_Enter_3X_1" id=
"windowing_6AGG_Enter_3X_1"></a>4.2.13 Q13</h4>
<p>Inform when a person enters the building at least 3 times within
1 hour</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
declare variable $seq := fn:doc("person_events.xml");
<result>{
for sliding window $w in $seq/stream/event
start $s when true()
end next $e when xs:dateTime($e/@time) - xs:dateTime($s/@time) gt
xs:dayTimeDuration("PT1H")
where count($w[person eq $s/person and direction eq "in"]) ge 3
return
<alert time="{ $e/@time }">{fn:data($s/person)} is suspicious</alert>
}</result>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<result>
<alert time="2006-01-02T14:00:00-00:00">Clara is suspicious</alert>
</result>
</pre></div>
</div>
<div class="div3">
<h4><a name="windowing_annoying_authors" id=
"windowing_annoying_authors"></a>4.2.14 Q14</h4>
<p>Find all annoying authors who have posted three consecutive
items in the RSS feed.</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
declare variable $rssfeed := fn:doc("rss.xml");
<result>{
for tumbling window $w in $rssfeed/rss/channel/item
start $first when fn:true()
end next $lookAhead when $first/author ne $lookAhead/author
where count($w) ge 3
return <annoying-author>{
$w[1]/author
}</annoying-author>
}</result>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<result>
<annoying-author>
<author>rokas@e-mail.de</author>
</annoying-author>
</result>
</pre></div>
</div>
<div class="div3">
<h4><a name="windowing_interesting_topics" id=
"windowing_interesting_topics"></a>4.2.15 Q15</h4>
<p>Every day, provide a list of interesting topics in the RSS feed.
In our example, interesting means that the title of the item
contains the specific word XQuery.</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
declare variable $rssfeed := fn:doc("rss.xml");
<result>{
for tumbling window $w in $rssfeed/rss/channel/item
start $s_curr when true()
end next $e_next when
fn:day-from-dateTime(xs:dateTime($e_next/pubDate)) ne
fn:day-from-dateTime(xs:dateTime($s_curr/pubDate))
return
<item>
<date>{xs:date(xs:dateTime($s_curr/pubDate))}</date>
{ for $item in $w
where fn:contains( xs:string($item/title), 'XQuery')
return $item/title }
</item>
}</result>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<result>
<item>
<date>2003-06-03</date>
<title>Extending XQuery with Window Functions</title>
<title>XQueryP: A new programming language is born</title>
</item>
<item>
<date>2003-06-04</date>
</item>
</result>
</pre></div>
</div>
<div class="div3">
<h4><a name="windowing_summary_by_authors" id=
"windowing_summary_by_authors"></a>4.2.16 Q16</h4>
<p>Every day, provide a summary of the RSS feed grouped by
author.</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
declare variable $rssfeed := fn:doc("rss.xml");
<result>{
for tumbling window $w in $rssfeed/rss/channel/item
start $s_curr when true()
end next $e_next when
fn:day-from-dateTime(xs:dateTime($e_next/pubDate)) ne
fn:day-from-dateTime(xs:dateTime($s_curr/pubDate))
return
<item>
<date>{xs:date(xs:dateTime($s_curr/pubDate))}</date>
{ for $a in fn:distinct-values($w/author)
return
<author name="{$a}">
<titles>
{ $w[author eq $a]/title }
</titles>
</author>
}
</item>
}</result>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<result>
<item>
<date>2003-06-03</date>
<author name="rokas@e-mail.de">
<titles>
<title>Why use cases are important Part 1.</title>
<title>Why use cases are important Part 2.</title>
<title>Why use cases are important Part 3.</title>
</titles>
</author>
<author name="tim@e-mail.de">
<titles>
<title>Extending XQuery with Window Functions</title>
</titles>
</author>
<author name="david@e-mail.de">
<titles>
<title>XQueryP: A new programming language is born</title>
</titles>
</author>
</item>
<item>
<date>2003-06-04</date>
<author name="rokas@e-mail.de">
<titles>
<title>Why use cases are annoying to write.</title>
</titles>
</author>
</item>
</result>
</pre></div>
</div>
<div class="div3">
<h4><a name="windowing_Q2_most_valuable_customer" id=
"windowing_Q2_most_valuable_customer"></a>4.2.17 Q17</h4>
<p>At the end of a day, list the most valuable customers.</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
declare variable $seq := fn:doc("cxml.xml");
<result>{
for sliding window $w in $seq/sequence/*
start $cur previous $prev
when day-from-dateTime($cur/@date) ne day-from-dateTime($prev/@date) or empty($prev)
end $end next $next
when day-from-dateTime(xs:dateTime($end/@date)) ne
day-from-dateTime(xs:dateTime($next/@date))
return
<mostValuableCustomer endOfDay="{xs:dateTime($cur/@date)}">{
let $companies := for $x in distinct-values($w/@billTo )
return <amount company="{$x}">{sum($w[@billTo eq $x]/@total)}</amount>
let $max := max($companies)
for $company in $companies
where $company eq xs:untypedAtomic($max)
return $company
}</mostValuableCustomer>
}</result>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<result>
<mostValuableCustomer endOfDay="2006-01-01T00:00:00Z">
<amount company="ACME1">1100</amount>
</mostValuableCustomer>
<mostValuableCustomer endOfDay="2006-01-02T00:00:00Z">
<amount company="ACME1">10000</amount>
</mostValuableCustomer>
<mostValuableCustomer endOfDay="2006-01-03T00:00:00Z"/>
<mostValuableCustomer endOfDay="2006-01-04T00:00:00Z"/>
<mostValuableCustomer endOfDay="2006-01-05T00:00:00Z"/>
<mostValuableCustomer endOfDay="2006-01-06T00:00:00Z">
<amount company="ACME2">100</amount>
</mostValuableCustomer>
<mostValuableCustomer endOfDay="2006-01-07T00:00:00Z"/>
</result>
</pre></div>
</div>
<div class="div3">
<h4><a name="windowing_Q3_transaction_time" id=
"windowing_Q3_transaction_time"></a>4.2.18 Q18</h4>
<p>Calculate the time needed to process an order from the request
up to the shipping.</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
declare variable $seq := fn:doc("cxml.xml");
<result>{
for sliding window $w in $seq/sequence/*
start $s when $s[self::OrderRequest]
end $e when $e/@orderID eq $s/@orderID and
$e[self::ConfirmationRequest] and $e/@status eq "reject"
or $e[self::ShipNotice]
where $e[self::ShipNotice]
return
<timeToShip orderID="{ $s/@orderID}">{xs:dateTime($e/@date) - xs:dateTime($s/@date) }</timeToShip>
}</result>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<result>
<timeToShip orderID="OID01">P3DT22H</timeToShip>
<timeToShip orderID="OID03">P2DT19H</timeToShip>
</result>
</pre></div>
</div>
<div class="div3">
<h4><a name="windowing_Q4_ship_together" id=
"windowing_Q4_ship_together"></a>4.2.19 Q19</h4>
<p>At the moment of the shipping notification, calculate if an open
request exists that can be shipped to the same address.</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
declare variable $seq := fn:doc("cxml.xml");
<result>{
for sliding window $w in $seq/sequence/*
start previous $wSPrev when $wSPrev[self::OrderRequest]
end next $wENext when $wENext/@orderID eq $wSPrev/@orderID and
( $wENext[self::ConfirmationRequest] and $wENext/@status eq "reject")
or $wENext[self::ShipNotice]
where $wENext[self::ShipNotice]
return
<bundleWith orderId="{$wSPrev/@orderID}">{
for sliding window $bundle in $w
start $bSCur
when $bSCur[self::OrderRequest] and $bSCur/@shipTo eq $wSPrev/@shipTo
end $bECur next $bENext
when $bECur/@orderID eq $bSCur/@orderID
and ($bECur[self::ConfirmationRequest] and $bECur/@status eq "reject")
or $bECur[self::ShipNotice]
where empty($bENext)
return $bSCur
}</bundleWith>
}</result>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<result>
<bundleWith orderId="OID01">
<OrderRequest billTo="ACME1" date="2006-01-02T14:00:00-00:00"
orderID="OID03" shipTo="ACME1" total="10000" type="new">
<Item partID="ID3" quantity="100" unitPrice="100"/>
</OrderRequest>
</bundleWith>
<bundleWith orderId="OID03"/>
</result>
</pre></div>
</div>
</div>
</div>
<div class="div1">
<h2><a name="count-use-cases" id="count-use-cases"></a>5 Use Case
"Count" - Queries which require output numbering</h2>
<p>The described queries in this section all require tuple
numbering. The numbering is either required in the final result or
might also be required at some point in the middle when the tuple
stream gets generated.</p>
<div class="div2">
<h3><a name="d2e942" id="d2e942"></a>5.1 Schema and Sample
Data</h3>
<p>The "Count" use cases are all based on the shopping scenario
which can be found in the section <a href="#data">Shopping Scenario
Schema and Data</a>.</p>
</div>
<div class="div2">
<h3><a name="d2e950" id="d2e950"></a>5.2 Queries and Results</h3>
<div class="div3">
<h4><a name="count-numbering" id="count-numbering"></a>5.2.1
Q1</h4>
<p>List all products in alphabatical order with a continously
increasing number.</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
<products>{
for $product in doc("products.xml")/*/product
order by $product/name
count $number
return
<product number="{$number}">{$product/*}</product>
}</products>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<products>
<product number="1">
<name>blender</name>
<category>kitchen</category>
<price>50</price>
<cost>25</cost>
</product>
<product number="2">
<name>broiler</name>
<category>kitchen</category>
<price>100</price>
<cost>70</cost>
</product>
<product number="3">
<name>shirt</name>
<category>clothes</category>
<price>10</price>
<cost>3</cost>
</product>
<product number="4">
<name>socks</name>
<category>clothes</category>
<price>5</price>
<cost>2</cost>
</product>
<product number="5">
<name>toaster</name>
<category>kitchen</category>
<price>30</price>
<cost>10</cost>
</product>
</products>
</pre></div>
</div>
<div class="div3">
<h4><a name="count-windowing" id="count-windowing"></a>5.2.2
Q2</h4>
<p>Arrange a sequence of items as a table with three columns (using
as many rows as necessary) like in <a href=
"#windowing_arrange_rows">Q1</a> from the windowing use
cases.Additionally, number every row continuously.</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
declare variable $seq := fn:doc("arrange_rows.xml");
<table>{
for tumbling window $w in $seq/doc/*
start at $x when fn:true()
end at $y when $y - $x = 2
count $rowNumber
return
<tr>
<td>{$rowNumber}</td>{
for $i in $w
return
<td>{data($i)}</td>
}</tr>
}</table>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<table>
<tr>
<td>1</td>
<td>Green</td>
<td>Pink</td>
<td>Lilac</td>
</tr>
<tr>
<td>2</td>
<td>Turquoise</td>
<td>Peach</td>
<td>Opal</td>
</tr>
<tr>
<td>3</td>
<td>Champagne</td>
</tr>
</table>
</pre></div>
</div>
<div class="div3">
<h4><a name="count-top-k" id="count-top-k"></a>5.2.3 Q3</h4>
<p>Top-K query: Return the three best-selling products (by
quantity).</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
<result>{
for $sales in doc("sales-records.xml")/*/record
let $name := $sales/product-name
group by $name
let $qty := sum($sales/qty)
order by $qty descending
count $count
where $count <= 3
return
<sale product="{$name}" qty="{$qty }"/>
}</result>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<result>
<sale product="socks" qty="510"/>
<sale product="blender" qty="250"/>
<sale product="toaster" qty="200"/>
</result>
</pre></div>
</div>
</div>
</div>
<div class="div1">
<h2><a name="outer-join-uc" id="outer-join-uc"></a>6 Use Case
"Outer For" - Queries which require outer joins</h2>
<p>This use case demonstrate how the new "outer for" clause can
help to express outer joins in XQuery.</p>
<div class="div2">
<h3><a name="d2e1025" id="d2e1025"></a>6.1 Schema and Sample
Data</h3>
<p>The "Outer For" use case is based on the shopping scenario which
can be found in the section <a href="#data">Shopping Scenario
Schema and Data</a>.</p>
</div>
<div class="div2">
<h3><a name="d2e1033" id="d2e1033"></a>6.2 Queries and Results</h3>
<div class="div3">
<h4><a name="outer-join-1" id="outer-join-1"></a>6.2.1 Q1</h4>
<p>Report all the store sales including stores which have not sold
anything.</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
<result>{
for $store in doc("stores.xml")/*/store
for $sale allowing empty in doc("sales-records.xml")/*/record[
store-number eq $store/store-number]
return
<store number="{$store/store-number}"
product="{$sale/product-name}"
state="{$store/state}"
sold="{$sale/qty}" />
}</result>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<result>
<store number="1" state="CA" product="broiler" sold="20"/>
<store number="1" state="CA" product="socks" sold="500"/>
<store number="2" state="CA" product="toaster" sold="100"/>
<store number="2" state="CA" product="toaster" sold="50"/>
<store number="2" state="CA" product="socks" sold="10"/>
<store number="3" state="MA" product="toaster" sold="50"/>
<store number="3" state="MA" product="blender" sold="100"/>
<store number="3" state="MA" product="blender" sold="150"/>
<store number="3" state="MA" product="shirt" sold="10"/>
<store number="4" state="WA" product="" sold=""/>
</result>
</pre></div>
</div>
<div class="div3">
<h4><a name="simplified-flwor-2" id="simplified-flwor-2"></a>6.2.2
Q2</h4>
<p>List all the sales records. Include also all the items which
were not sold in a store.</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
<result>{
for $store in doc("stores.xml")/*/store
for $product in doc("products.xml")/*/product
for $sale allowing empty in doc("sales-records.xml")/*/record[
store-number eq $store/store-number and
product-name eq $product/name]
return
<store number="{$store/store-number}"
state="{$store/state}"
product="{$sale/product-name}"
sold="{$sale/qty}" />
}</result>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<?xml version="1.0" encoding="UTF-8"?>
<result>
<store number="1" state="CA" product="broiler" sold="20"/>
<store number="1" state="CA" product="toaster" sold=""/>
<store number="1" state="CA" product="blender" sold=""/>
<store number="1" state="CA" product="socks" sold="500"/>
<store number="1" state="CA" product="shirt" sold=""/>
<store number="2" state="CA" product="broiler" sold=""/>
<store number="2" state="CA" product="toaster" sold="100"/>
<store number="2" state="CA" product="toaster" sold="50"/>
<store number="2" state="CA" product="blender" sold=""/>
<store number="2" state="CA" product="socks" sold="10"/>
<store number="2" state="CA" product="shirt" sold=""/>
<store number="3" state="CA" product="broiler" sold=""/>
<store number="3" state="MA" product="toaster" sold="50"/>
<store number="3" state="MA" product="blender" sold="100"/>
<store number="3" state="MA" product="blender" sold="150"/>
<store number="3" state="MA" product="socks" sold=""/>
<store number="3" state="MA" product="shirt" sold="10"/>
<store number="4" state="MA" product="broiler" sold=""/>
<store number="4" state="MA" product="toaster" sold=""/>
<store number="4" state="MA" product="blender" sold=""/>
<store number="4" state="MA" product="socks" sold=""/>
<store number="4" state="MA" product="shirt" sold=""/>
</result>
</pre></div>
</div>
</div>
</div>
<div class="div1">
<h2><a name="try-catch-use-cases" id="try-catch-use-cases"></a>7
Use Case "Try-Catch" - Queries which require to recover from
errors</h2>
<p>This use case covers queries that require to recover from
errors.</p>
<div class="div2">
<h3><a name="dataerrorproducts" id="dataerrorproducts"></a>7.1
Sample Data</h3>
<p>Q1 and Q2 are based on a errornous version of product.xml from
<a href="#data">Shopping Scenario Schema and Data</a>, whereas Q3
does not require any input data. Sample data for Q1 and Q2 named
product-err.xml are shown below:</p>
<div class="exampleInner">
<pre>
<products>
<product>
<name>broiler</name>
<category>kitchen</category>
<price>100</price>
<cost>"70"</cost>
</product>
<product>
<name>toaster</name>
<category>kitchen</category>
<price>30</price>
<cost>10</cost>
</product>
<product>
<name>blender</name>
<category>kitchen</category>
<price>50</price>
<cost>25</cost>
</product>
<product>
<name>socks</name>
<category>clothes</category>
<price>5</price>
<cost>2</cost>
</product>
<product>
<name>shirt</name>
<category>clothes</category>
<price>10</price>
<cost>3</cost>
</product>
</products>
</pre></div>
</div>
<div class="div2">
<h3><a name="d2e1096" id="d2e1096"></a>7.2 Queries and Results</h3>
<div class="div3">
<h4><a name="try-catch-1" id="try-catch-1"></a>7.2.1 Q1</h4>
<p>Calculate the margin of every product. If an error occurs,
report a user-friendly message.</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
try {
<result>{
for $product in fn:doc("product-err.xml")//product
return
<product>{$product/name}
<margin>{$product/price - $product/cost}</margin>
</product>
}</result>
} catch * {
"An error occured, please ask your consultant for help."
}
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
An error occured, please ask your consultant for help.
</pre></div>
</div>
<div class="div3">
<h4><a name="try-catch-2" id="try-catch-2"></a>7.2.2 Q2</h4>
<p>Report the margin in procent of every product. If an error
occurs the failure should be listed, but the query should still
continue to report the other values.</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
<result>{
for $product in fn:doc("product-err.xml")//product
return
try {
<product>{$product/name}
<margin>{$product/price div $product/cost}</margin>
</product>
} catch * ($errcode) {
<product>{
($product/name, "Error:", $errcode)
}</product>
}
}</result>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<?xml version="1.0" encoding="UTF-8"?>
<result>
<product>
<name>broiler</name>
Error: FORG0001
</product>
<product>
<name>toaster</name>
<margin>3</margin>
</product>
<product>
<name>blender</name>
<margin>2</margin>
</product>
<product>
<name>socks</name>
<margin>2.5</margin>
</product>
<product>
<name>shirt</name>
<margin>3.3333333333333333</margin>
</product>
</result>
</pre></div>
</div>
<div class="div3">
<h4><a name="try-catch-3" id="try-catch-3"></a>7.2.3 Q3</h4>
<p>For a sequence of inputs the n-th fibonacci number has to be
calculated using a pre-defined function. The function is built to
avoid long running times and therefore rejects high values by
throwing a user-defined error. If such an error occurs during
calculation, a warning should be displayed, but the processing
should continue.</p>
<p><em>Solution in XQuery:</em></p>
<div class="exampleInner">
<pre>
declare namespace foo='http://foo.com';
declare function local:fib-recur($n as xs:integer) as xs:integer? {
if ($n <0) then ()
else if ($n > 100) then
fn:error(fn:QName('http://foo.com', 'ValueToBig'), 'Value too big')
else if ($n = 0) then 0
else if ($n=1) then 1
else local:fib-recur($n - 1) + local:fib-recur($n - 2)
};
<result>{
for $x in (3,1,1030,5)
return
try{
<fib input="{$x}">{local:fib-recur($x)}</fib>
}catch foo:ValueToBig {
<fib input="{$x}">Number to big</fib>
}
}</result>
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
<?xml version="1.0" encoding="UTF-8"?>
<result>
<fib input="3">2</fib>
<fib input="1">1</fib>
<fib input="1030">Number to big</fib>
<fib input="5">5</fib>
</result>
</pre></div>
</div>
</div>
</div>
</div>
<div class="back">
<div class="div1">
<h2><a name="acknowledgements" id="acknowledgements"></a>A
Acknowledgements (Non-Normative)</h2>
<p>The Working Group thanks the following individuals for their
contributions:</p>
<table border="1">
<tbody>
<tr>
<td>Peter M. Fischer, Donald Kossmann, Rokas Tamosevicius</td>
<td>Use Case "windowing"</td>
</tr>
</tbody>
</table>
<p>Use case "windowing" has been previously published in <a href=
"#Fischer2006">[Windowing UC]</a>.</p>
</div>
<div class="div1">
<h2><a name="ChangeLog" id="ChangeLog"></a>B Change Log
(Non-Normative)</h2>
<div class="div2">
<h3><a name="id-2008-01-30" id="id-2008-01-30"></a>B.1 30 January
2008</h3>
<ul>
<li>
<p>Added group-by UC.</p>
</li>
<li>
<p>Added windowing UC.</p>
</li>
</ul>
</div>
<div class="div2">
<h3><a name="id-2008-11-17" id="id-2008-11-17"></a>B.2 17 November
2008</h3>
<ul>
<li>
<p>Added try-catch UC.</p>
</li>
<li>
<p>Added outer-join UC.</p>
</li>
<li>
<p>Added output-numbering UC.</p>
</li>
<li>
<p>Changed all DTDs to XML Schema.</p>
</li>
</ul>
</div>
</div>
<div class="div1">
<h2><a name="references" id="references"></a>C References
(Non-Normative)</h2>
<p>The following references are some of the works considered by the
WG in deriving its use cases.</p>
<dl>
<dt class="label"><span><a name="Fischer2006" id=
"Fischer2006"></a>Windowing UC</span></dt>
<dd>
<div><a href=
"http://www.dbis.ethz.ch/research/publications/XQWindowsUseCases.pdf">
<cite>Windows for XQuery - Use Cases</cite></a>, Peter M. Fischer,
Donald Kossmann, Tim Kraska and Rokas Tamosevicius, 2006, Technical
Report, ETH Zurich</div>
</dd>
<dt class="label"><span><a name="xmlschema-0" id=
"xmlschema-0"></a>XMLSchema0</span></dt>
<dd>
<div><a href="http://www.w3.org/TR/xmlschema-0/"><cite>XML Schema
Part 0: Primer Second Edition</cite></a>, Priscilla Walmsley and
David C. Fallside, Editors. World Wide Web Consortium,
28 Oct 2004. This version is
http://www.w3.org/TR/2004/REC-xmlschema-0-20041028/. The <a href=
"http://www.w3.org/TR/xmlschema-0/">latest version</a> is available
at http://www.w3.org/TR/xmlschema-0/.</div>
</dd>
<dt class="label"><span><a name="xmlschema-1" id=
"xmlschema-1"></a>XMLSchema1</span></dt>
<dd>
<div><a href="http://www.w3.org/TR/xmlschema-1/"><cite>XML Schema
Part 1: Structures Second Edition</cite></a>, David Beech, Henry S.
Thompson, Murray Maloney, and Noah Mendelsohn, Editors. World Wide
Web Consortium, 28 Oct 2004. This version is
http://www.w3.org/TR/2004/REC-xmlschema-1-20041028/. The <a href=
"http://www.w3.org/TR/xmlschema-1/">latest version</a> is available
at http://www.w3.org/TR/xmlschema-1/.</div>
</dd>
<dt class="label"><span><a name="xmlschema-2" id=
"xmlschema-2"></a>XMLSchema2</span></dt>
<dd>
<div><a href="http://www.w3.org/TR/xmlschema-2/"><cite>XML Schema
Part 2: Datatypes Second Edition</cite></a>, Ashok Malhotra and
Paul V. Biron, Editors. World Wide Web Consortium,
28 Oct 2004. This version is
http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/. The <a href=
"http://www.w3.org/TR/xmlschema-2/">latest version</a> is available
at http://www.w3.org/TR/xmlschema-2/.</div>
</dd>
<dt class="label"><span><a name="xquery-30" id=
"xquery-30"></a>XQuery 3.0</span></dt>
<dd>
<div><a href="http://www.w3.org/TR/xquery-30/"><cite>XQuery 3.0: An
XML Query Language</cite></a>, Jonathan Robie, Don Chamberlin,
Michael Dyck, John Snelson, Editors. World Wide Web Consortium, 14
December 2010. This version is
http://www.w3.org/TR/2010/WD-xquery-30-20101214/. The <a href=
"http://www.w3.org/TR/xquery-30/">latest version</a> is available
at http://www.w3.org/TR/xquery-30/.</div>
</dd>
<dt class="label"><span><a name="xquery-30-requirements" id=
"xquery-30-requirements"></a>XQuery 3.0 Requirements</span></dt>
<dd>
<div><a href="http://www.w3.org/TR/xquery-30-requirements/">XML
Query (XQuery) 3.0 Requirements</a>, W3C Working Draft, 16
September 2010.</div>
</dd>
<dt class="label"><span><a name="xslt-30" id="xslt-30"></a>XSLT
3.0</span></dt>
<dd>
<div><a href="http://www.w3.org/TR/xslt-30/"><cite>XSL
Transformations (XSLT) Version 3.0</cite></a> (expected), Michael
Kay, Editor. World Wide Web Consortium, 14 December 2010. This
version is http://www.w3.org/TR/2010/WD-xslt-30-20101214/. The
<a href="http://www.w3.org/TR/xslt-30/">latest version</a> is
available at http://www.w3.org/TR/xslt-30/.</div>
</dd>
<dt class="label"><span><a name="UseCaseQueries" id=
"UseCaseQueries"></a>Use Case Sample Queries</span></dt>
<dd>
<div><a href=
"http://www.w3.org/2010/12/xquery-30-use-cases/xquery-30-use-case-queries.txt">
<cite>Queries from this document</cite></a>, presented in a single
file</div>
</dd>
</dl>
</div>
</div>
</body>
</html>