index.html 48.9 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
<?xml version="1.0" encoding="utf-8"?>
<!--XSLT Processor: SAXON 8.8 from Saxonica SAXON 8.8-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.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 Scripting Extension 1.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 Scripting Extension 1.0
Use Cases</h1>
<h2><a name="w3c-doctype" id="w3c-doctype"></a>W3C Working Draft 3
December 2008</h2>
<dl>
<dt>This version:</dt>
<dd><a href=
"http://www.w3.org/TR/2008/WD-xquery-sx-10-use-cases-20081203/">http://www.w3.org/TR/2008/WD-xquery-sx-10-use-cases-20081203/</a></dd>
<dt>Latest version:</dt>
<dd><a href=
"http://www.w3.org/TR/xquery-sx-10-use-cases/">http://www.w3.org/TR/xquery-sx-10-use-cases/</a></dd>
<dt>Previous version:</dt>
<dd><a href=
"http://www.w3.org/TR/2008/WD-xquery-sx-10-use-cases-20080328/">http://www.w3.org/TR/2008/WD-xquery-sx-10-use-cases-20080328/</a></dd>
<dt>Editor:</dt>
<dd><span><a href="http://snelson.org.uk/john">John
Snelson</a></span>, Oracle Corporation <a href=
"mailto:john.snelson@oracle.com">&lt;john.snelson@oracle.com&gt;</a></dd>
</dl>
<p class="copyright"><a href=
"http://www.w3.org/Consortium/Legal/ipr-notice#Copyright">Copyright</a> &copy; 2008 <a href="http://www.w3.org/"><acronym title="World Wide Web Consortium">W3C</acronym></a><sup>&reg;</sup>
(<a href="http://www.csail.mit.edu/"><acronym title=
"Massachusetts Institute of Technology">MIT</acronym></a>, <a href=
"http://www.ercim.org/"><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 the XQuery Scripting
Extension.</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/2004/02/Process-20040205/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 the language defined in <a href=
"http://www.w3.org/TR/xquery-sx-10/">XQuery Scripting Extension
1.0</a> at the time that it was issued as a Working Draft on 28
March 2008. 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 document incorporates changes made against the previous
publication of the Working Draft of 28 March 2008. Changes to this
document since the previous publication of the Working Draft are
detailed in <a href="#id-revisions-log"><b>B Revision
Log</b></a>.</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 “[SXUC]” 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="#use-cases-for-xquery-sx">Use Cases for
XQuery Scripting Extension</a><br />
&#160;&#160;&#160;&#160;1.1 <a href="#rdb">Use Case "R" - Scripting
Relational Data</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.1 <a href=
"#rdb-dtd">Document Type Definition (DTD)</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.1.1
<a href="#dtd-for-usres.xml">DTD for users.xml</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.1.2
<a href="#dtd-for-items.xml">DTD for items.xml</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.1.3
<a href="#dtd-for-bids.xml">DTD for bids.xml</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.2 <a href=
"#rdb-data">Input Data</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.3 <a href=
"#rdb-sx-results">Queries and Results</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.3.1
<a href="#rdb-sx-results-q1">Q1</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.3.2
<a href="#rdb-sx-results-q2">Q2</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.3.3
<a href="#rdb-sx-results-q3">Q3</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.3.4
<a href="#rdb-sx-results-q4">Q4</a><br />
&#160;&#160;&#160;&#160;1.2 <a href="#xhtml">Use Case "XHTML" -
AJAX Scripting with XHTML</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.2.1 <a href=
"#xhtml-data">Input Data</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.2.2 <a href=
"#xhtml-sx-results">Queries and Results</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.2.2.1
<a href="#xhtml-sx-results-q1">Q1</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.2.2.2
<a href="#xhtml-sx-results-q2">Q2</a><br />
&#160;&#160;&#160;&#160;1.3 <a href="#webservice">Use Case "WS" -
XQuery for Web Services</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.3.1 <a href=
"#webservice-data">Input Data</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.3.2 <a href=
"#webservice-sx-results">Queries and Results</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.3.2.1
<a href="#webservice-sx-results-q1">Q1</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.3.2.2
<a href="#webservice-sx-results-q2">Q2</a><br /></p>
<h3><a name="appendices" id="appendices"></a>Appendices</h3>
<p class="toc">A <a href="#references">Normative
References</a><br />
B <a href="#id-revisions-log">Revision Log</a>
(Non-Normative)<br />
&#160;&#160;&#160;&#160;B.1 <a href="#id-log-pending">Changes in
internal WD</a><br /></p>
</div>
<hr />
<div class="body">
<div class="div1">
<h2><a name="use-cases-for-xquery-sx" id=
"use-cases-for-xquery-sx"></a>1 Use Cases for XQuery Scripting
Extension</h2>
<p>The use cases listed below were created by the <a href=
"http://www.w3.org/XML/Query/">XML Query Working Group</a> to
illustrate important applications for an XQuery scripting
extension.</p>
<div class="div2">
<h3><a name="rdb" id="rdb"></a>1.1 Use Case "R" - Scripting
Relational Data</h3>
<p>This use case is based on performing updates on the data used in
Use Case "R" from the <a href="#XQueryUseCases">[XML Query Use
Cases]</a>. The DTD and sample data from this Use Case are copied
below for convenience, and exactly match those found in the XQuery
Use Cases.</p>
<p>The data represents a relational database used by an online
auction. The auction maintains a USERS table containing information
on registered users, each identified by a unique userid, who can
either offer items for sale or bid on items. An ITEMS table lists
items currently or recently for sale, with the userid of the user
who offered each item. A BIDS table contains all bids on record,
keyed by the userid of the bidder and the item number of the item
to which the bid applies.</p>
<p>The three tables used by the online auction are below, with
their column-names indicated in parentheses.</p>
<div class="exampleInner">
<pre>
USERS ( USERID, NAME, RATING )
ITEMS ( ITEMNO, DESCRIPTION, OFFERED_BY, 
        START_DATE, END_DATE, RESERVE_PRICE ) 
BIDS ( USERID, ITEMNO, BID, BID_DATE )
</pre></div>
<div class="div3">
<h4><a name="rdb-dtd" id="rdb-dtd"></a>1.1.1 Document Type
Definition (DTD)</h4>
<p>This use case is based on three separate input documents named
users.xml, items.xml, and bids.xml. Each of the documents
represents one of the tables in the relational database described
above, using the following DTDs:</p>
<div class="div4">
<h5><a name="dtd-for-usres.xml" id="dtd-for-usres.xml"></a>1.1.1.1
DTD for users.xml</h5>
<div class="exampleInner">
<pre>
                                            
&lt;!ELEMENT users (user_tuple*)&gt; 
&lt;!ELEMENT user_tuple (userid, name, rating?)&gt; 
&lt;!ELEMENT userid (#PCDATA)&gt; 
&lt;!ELEMENT name (#PCDATA)&gt; 
&lt;!ELEMENT rating (#PCDATA)&gt;
 
</pre></div>
</div>
<div class="div4">
<h5><a name="dtd-for-items.xml" id="dtd-for-items.xml"></a>1.1.1.2
DTD for items.xml</h5>
<div class="exampleInner">
<pre>
                  
&lt;!ELEMENT items (item_tuple*)&gt;
&lt;!ELEMENT item_tuple (itemno, description, offered_by, 
                      start_date?, end_date?, reserve_price?)&gt; 
&lt;!ELEMENT itemno (#PCDATA)&gt;
&lt;!ELEMENT description (#PCDATA)&gt;
&lt;!ELEMENT offered_by (#PCDATA)&gt;
&lt;!ELEMENT start_date (#PCDATA)&gt;
&lt;!ELEMENT end_date (#PCDATA)&gt;
&lt;!ELEMENT reserve_price (#PCDATA)&gt;
</pre></div>
</div>
<div class="div4">
<h5><a name="dtd-for-bids.xml" id="dtd-for-bids.xml"></a>1.1.1.3
DTD for bids.xml</h5>
<div class="exampleInner">
<pre>
                      
&lt;!ELEMENT bids (bid_tuple*)&gt; 
&lt;!ELEMENT bid_tuple (userid, itemno, bid, bid_date)&gt; 
&lt;!ELEMENT userid (#PCDATA)&gt; 
&lt;!ELEMENT itemno (#PCDATA)&gt; 
&lt;!ELEMENT bid (#PCDATA)&gt;
&lt;!ELEMENT bid_date (#PCDATA)&gt;

                      
</pre></div>
</div>
</div>
<div class="div3">
<h4><a name="rdb-data" id="rdb-data"></a>1.1.2 Input Data</h4>
<p>Here is an abbreviated set of data showing the XML format of the
instances:</p>
<div class="exampleInner">
<pre>
&lt;items&gt;
  &lt;item_tuple&gt;
    &lt;itemno&gt;1001&lt;/itemno&gt;
    &lt;description&gt;Red Bicycle&lt;/description&gt;
    &lt;offered_by&gt;U01&lt;/offered_by&gt;
    &lt;start_date&gt;1999-01-05&lt;/start_date&gt;
    &lt;end_date&gt;1999-01-20&lt;/end_date&gt;
    &lt;reserve_price&gt;40&lt;/reserve_price&gt;
  &lt;/item_tuple&gt;
  <em>  ... Snip ... </em>
&lt;/items&gt;
&lt;users&gt;
  &lt;user_tuple&gt;
    &lt;userid&gt;U01&lt;/userid&gt;
    &lt;name&gt;Tom Jones&lt;/name&gt;
    &lt;rating&gt;B&lt;/rating&gt;
  &lt;/user_tuple&gt;
  <em>  ... Snip ... </em>
&lt;/users&gt;
&lt;bids&gt;
  &lt;bid_tuple&gt; 
    &lt;userid&gt;U02&lt;/userid&gt; 
    &lt;itemno&gt;1001&lt;/itemno&gt; 
    &lt;bid&gt;35&lt;/bid&gt; 
    &lt;bid_date&gt;1999-01-07&lt;/bid_date&gt; 
    &lt;/bid_tuple&gt; 
  &lt;bid_tuple&gt; 
  <em>  ... Snip ... </em>
&lt;/bids&gt;
                    
</pre></div>
<p>The entire data set is represented by the following table:</p>
<table border="1" summary="Data set table">
<caption>USERS</caption>
<thead>
<tr>
<td>USERID</td>
<td>NAME</td>
<td>RATING</td>
</tr>
</thead>
<tbody>
<tr>
<td>U01</td>
<td>Tom Jones</td>
<td>B</td>
</tr>
<tr>
<td>U02</td>
<td>Mary Doe</td>
<td>A</td>
</tr>
<tr>
<td>U03</td>
<td>Dee Linquent</td>
<td>D</td>
</tr>
<tr>
<td>U04</td>
<td>Roger Smith</td>
<td>C</td>
</tr>
<tr>
<td>U05</td>
<td>Jack Sprat</td>
<td>B</td>
</tr>
<tr>
<td>U06</td>
<td>Rip Van Winkle</td>
<td>B</td>
</tr>
</tbody>
</table>
<table border="1" summary="Data set table">
<caption>ITEMS</caption>
<thead>
<tr>
<td>ITEMNO</td>
<td>DESCRIPTION</td>
<td>OFFERED_BY</td>
<td>START_DATE</td>
<td>END_DATE</td>
<td>RESERVE_PRICE</td>
</tr>
</thead>
<tbody>
<tr>
<td>1001</td>
<td>Red Bicycle</td>
<td>U01</td>
<td>1999-01-05</td>
<td>1999-01-20</td>
<td>40</td>
</tr>
<tr>
<td>1002</td>
<td>Motorcycle</td>
<td>U02</td>
<td>1999-02-11</td>
<td>1999-03-15</td>
<td>500</td>
</tr>
<tr>
<td>1003</td>
<td>Old Bicycle</td>
<td>U02</td>
<td>1999-01-10</td>
<td>1999-02-20</td>
<td>25</td>
</tr>
<tr>
<td>1004</td>
<td>Tricycle</td>
<td>U01</td>
<td>1999-02-25</td>
<td>1999-03-08</td>
<td>15</td>
</tr>
<tr>
<td>1005</td>
<td>Tennis Racket</td>
<td>U03</td>
<td>1999-03-19</td>
<td>1999-04-30</td>
<td>20</td>
</tr>
<tr>
<td>1006</td>
<td>Helicopter</td>
<td>U03</td>
<td>1999-05-05</td>
<td>1999-05-25</td>
<td>50000</td>
</tr>
<tr>
<td>1007</td>
<td>Racing Bicycle</td>
<td>U04</td>
<td>1999-01-20</td>
<td>1999-02-20</td>
<td>200</td>
</tr>
<tr>
<td>1008</td>
<td>Broken Bicycle</td>
<td>U01</td>
<td>1999-02-05</td>
<td>1999-03-06</td>
<td>25</td>
</tr>
</tbody>
</table>
<table border="1" summary="Data set table">
<caption>BIDS</caption>
<thead>
<tr>
<td>USERID</td>
<td>ITEMNO</td>
<td>BID</td>
<td>BID_DATE</td>
</tr>
</thead>
<tbody>
<tr>
<td>U02</td>
<td>1001</td>
<td>35</td>
<td>1999-01-07</td>
</tr>
<tr>
<td>U04</td>
<td>1001</td>
<td>40</td>
<td>1999-01-08</td>
</tr>
<tr>
<td>U02</td>
<td>1001</td>
<td>45</td>
<td>1999-01-11</td>
</tr>
<tr>
<td>U04</td>
<td>1001</td>
<td>50</td>
<td>1999-01-13</td>
</tr>
<tr>
<td>U02</td>
<td>1001</td>
<td>55</td>
<td>1999-01-15</td>
</tr>
<tr>
<td>U01</td>
<td>1002</td>
<td>400</td>
<td>1999-02-14</td>
</tr>
<tr>
<td>U02</td>
<td>1002</td>
<td>600</td>
<td>1999-02-16</td>
</tr>
<tr>
<td>U03</td>
<td>1002</td>
<td>800</td>
<td>1999-02-17</td>
</tr>
<tr>
<td>U04</td>
<td>1002</td>
<td>1000</td>
<td>1999-02-25</td>
</tr>
<tr>
<td>U02</td>
<td>1002</td>
<td>1200</td>
<td>1999-03-02</td>
</tr>
<tr>
<td>U04</td>
<td>1003</td>
<td>15</td>
<td>1999-01-22</td>
</tr>
<tr>
<td>U05</td>
<td>1003</td>
<td>20</td>
<td>1999-02-03</td>
</tr>
<tr>
<td>U01</td>
<td>1004</td>
<td>40</td>
<td>1999-03-05</td>
</tr>
<tr>
<td>U03</td>
<td>1007</td>
<td>175</td>
<td>1999-01-25</td>
</tr>
<tr>
<td>U05</td>
<td>1007</td>
<td>200</td>
<td>1999-02-08</td>
</tr>
<tr>
<td>U04</td>
<td>1007</td>
<td>225</td>
<td>1999-02-12</td>
</tr>
</tbody>
</table>
</div>
<div class="div3">
<h4><a name="rdb-sx-results" id="rdb-sx-results"></a>1.1.3 Queries
and Results</h4>
<div class="div4">
<h5><a name="rdb-sx-results-q1" id="rdb-sx-results-q1"></a>1.1.3.1
Q1</h5>
<p>Insert a new bid for Roger Smith on item 1002, adding 10% to the
best bid received so far for this item, and report back what bid
was just entered.</p>
<p><em>Solution in the XQuery Scripting Extension:</em></p>
<div class="exampleInner">
<pre>
let $uid := doc("users.xml")/users/user_tuple[name = "Roger Smith"]/userid
let $topbid := max(doc("bids.xml")/bids/bid_tuple[itemno = 1002]/bid)
let $newbid := $topbid * 1.1
return (
  insert nodes
    &lt;bid_tuple&gt; 
      &lt;userid&gt;{ data($uid) }&lt;/userid&gt; 
      &lt;itemno&gt;1002&lt;/itemno&gt; 
      &lt;bid&gt;{ $newbid }&lt;/bid&gt; 
      &lt;bid_date&gt;1999-03-03&lt;/bid_date&gt; 
    &lt;/bid_tuple&gt;
  into doc("bids.xml")/bids,
  &lt;new_bid&gt;{ $newbid }&lt;/new_bid&gt;
)

</pre></div>
<p><em>Expected Result:</em></p>
<p>The best bid for item 1002 had been at 1200, thus Roger's bid is
at 1320.</p>
<div class="exampleInner">
<pre>
&lt;new_bid&gt;1320&lt;/new_bid&gt;
            
</pre></div>
<p><em>Expected resulting content of bids.xml:</em></p>
<div class="exampleInner">
<pre>
&lt;bids&gt;
  &lt;bid_tuple&gt; 
    &lt;userid&gt;U02&lt;/userid&gt; 
    &lt;itemno&gt;1001&lt;/itemno&gt; 
    &lt;bid&gt;35&lt;/bid&gt; 
    &lt;bid_date&gt;1999-01-07&lt;/bid_date&gt; 
  &lt;/bid_tuple&gt; 
  <em>  ... Snip ... </em>
  &lt;bid_tuple&gt; 
    &lt;userid&gt;U01&lt;/userid&gt; 
    &lt;itemno&gt;1002&lt;/itemno&gt; 
    &lt;bid&gt;400&lt;/bid&gt; 
    &lt;bid_date&gt;1999-02-14&lt;/bid_date&gt; 
  &lt;/bid_tuple&gt; 
  <em>  ... Snip ... </em>
   &lt;bid_tuple&gt; 
    &lt;userid&gt;U04&lt;/userid&gt; 
    &lt;itemno&gt;1007&lt;/itemno&gt; 
    &lt;bid&gt;225&lt;/bid&gt; 
    &lt;bid_date&gt;1999-02-12&lt;/bid_date&gt; 
  &lt;/bid_tuple&gt; 
  <em>  ... Snip ... </em>
  &lt;bid_tuple&gt; 
    &lt;userid&gt;U04&lt;/userid&gt; 
    &lt;itemno&gt;1002&lt;/itemno&gt; 
    &lt;bid&gt;1320&lt;/bid&gt; 
    &lt;bid_date&gt;1999-03-03&lt;/bid_date&gt; 
  &lt;/bid_tuple&gt; 
&lt;/bids&gt;
                                                
</pre></div>
</div>
<div class="div4">
<h5><a name="rdb-sx-results-q2" id="rdb-sx-results-q2"></a>1.1.3.2
Q2</h5>
<p>Place a bid for Roger Smith on item 1007, adding 10% to the best
bid received so far on that item, but only if the bid amount does
not exceed a given limit. Otherwise return the current top bid.</p>
<p><em>Solution in the XQuery Scripting Extension:</em></p>
<div class="exampleInner">
<pre>
let $uid := doc("users.xml")/users/user_tuple[name = "Roger Smith"]/userid
let $topbid := max(doc("bids.xml")/bids/bid_tuple[itemno = 1007]/bid)
let $newbid := $topbid * 1.1
return
  if($newbid &lt;= 240) then (
    insert nodes
      &lt;bid_tuple&gt;
        &lt;userid&gt;{ data($uid) }&lt;/userid&gt;
        &lt;itemno&gt;1002&lt;/itemno&gt;
        &lt;bid&gt;{ $newbid }&lt;/bid&gt;
        &lt;bid_date&gt;1999-03-03&lt;/bid_date&gt;
      &lt;/bid_tuple&gt;
    into doc("bids.xml")/bids,
    &lt;new_bid&gt;{ $newbid }&lt;/new_bid&gt;
  ) else (
    &lt;top_bid&gt;{ $topbid }&lt;/top_bid&gt;
  )

</pre></div>
<p><em>Expected Result:</em></p>
<p>Adding 10% to the best bid on item 1007 would require a bid of
247.5, which is more than the allowed limit of 240. Thus, the
bids.xml document does not change.</p>
<div class="exampleInner">
<pre>
&lt;top_bid&gt;225&lt;/top_bid&gt;
            
</pre></div>
</div>
<div class="div4">
<h5><a name="rdb-sx-results-q3" id="rdb-sx-results-q3"></a>1.1.3.3
Q3</h5>
<p>Erase user Dee Linquent and the corresponding associated items
and bids. Return a count of the items and bids deleted.</p>
<p><em>Solution in the XQuery Scripting Extension:</em></p>
<div class="exampleInner">
<pre>
let $user := doc("users.xml")/users/user_tuple[name = "Dee Linquent"]
let $items := doc("items.xml")/items/item_tuple[offered_by = $user/userid]
let $bids := doc("bids.xml")/bids/bid_tuple[userid = $user/userid]
return (
  delete nodes ($user, $items, $bids),
  &lt;deleted&gt;
    &lt;items&gt;{ count($items) }&lt;/items&gt;
    &lt;bids&gt;{ count($bids) }&lt;/bids&gt;
  &lt;/deleted&gt;
)
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
&lt;deleted&gt;
  &lt;items&gt;2&lt;/items&gt;
  &lt;bids&gt;2&lt;/bids&gt;
&lt;/deleted&gt;
</pre></div>
<p><em>Expected resulting content of items.xml:</em></p>
<div class="exampleInner">
<pre>
&lt;items&gt;
  &lt;item_tuple&gt;
    &lt;itemno&gt;1001&lt;/itemno&gt;
    &lt;description&gt;Red Bicycle&lt;/description&gt;
    &lt;offered_by&gt;U01&lt;/offered_by&gt;
    &lt;start_date&gt;1999-01-05&lt;/start_date&gt;
    &lt;end_date&gt;1999-01-20&lt;/end_date&gt;
    &lt;reserve_price&gt;40&lt;/reserve_price&gt;
  &lt;/item_tuple&gt;
  <em>  ... Snip ... </em>
  &lt;item_tuple&gt;
    &lt;itemno&gt;1004&lt;/itemno&gt;
    &lt;description&gt;Tricycle&lt;/description&gt;
    &lt;offered_by&gt;U01&lt;/offered_by&gt;
    &lt;start_date&gt;1999-02-25&lt;/start_date&gt;
    &lt;end_date&gt;1999-03-08&lt;/end_date&gt;
    &lt;reserve_price&gt;15&lt;/reserve_price&gt;
  &lt;/item_tuple&gt;
  &lt;item_tuple&gt;
    &lt;itemno&gt;1007&lt;/itemno&gt;
    &lt;description&gt;Racing bicycle&lt;/description&gt;
    &lt;offered_by&gt;U04&lt;/offered_by&gt;
    &lt;start_date&gt;1999-01-20&lt;/start_date&gt;
    &lt;end_date&gt;1999-02-20&lt;/end_date&gt;
    &lt;reserve_price&gt;200&lt;/reserve_price&gt;
  &lt;/item_tuple&gt;
  &lt;item_tuple&gt;
    &lt;itemno&gt;1008&lt;/itemno&gt;
    &lt;description&gt;Broken bicycle&lt;/description&gt;
    &lt;offered_by&gt;U01&lt;/offered_by&gt;
    &lt;start_date&gt;1999-02-05&lt;/start_date&gt;
    &lt;end_date&gt;1999-03-06&lt;/end_date&gt;
    &lt;reserve_price&gt;25&lt;/reserve_price&gt;
  &lt;/item_tuple&gt;
&lt;/items&gt;
</pre></div>
<p><em>Expected resulting content of users.xml:</em></p>
<div class="exampleInner">
<pre>
&lt;users&gt;
  &lt;user_tuple&gt;
    &lt;userid&gt;U01&lt;/userid&gt;
    &lt;name&gt;Tom Jones&lt;/name&gt;
    &lt;rating&gt;B&lt;/rating&gt;
  &lt;/user_tuple&gt;
  &lt;user_tuple&gt;
    &lt;userid&gt;U02&lt;/userid&gt;
    &lt;name&gt;Mary Doe&lt;/name&gt;
    &lt;rating&gt;A&lt;/rating&gt;
  &lt;/user_tuple&gt;
   &lt;user_tuple&gt;
    &lt;userid&gt;U04&lt;/userid&gt;
    &lt;name&gt;Roger Smith&lt;/name&gt;
    &lt;rating&gt;C&lt;/rating&gt;
  &lt;/user_tuple&gt;
<em>  ... Snip ... </em>
  &lt;user_tuple&gt;
   &lt;userid&gt;U06&lt;/userid&gt;
   &lt;name&gt;Rip Van Winkle&lt;/name&gt;
   &lt;rating&gt;B&lt;/rating&gt;
  &lt;/user_tuple&gt;
&lt;/users&gt;
</pre></div>
<p><em>Expected resulting content of bids.xml:</em></p>
<div class="exampleInner">
<pre>
&lt;bids&gt;
  &lt;bid_tuple&gt; 
    &lt;userid&gt;U02&lt;/userid&gt; 
    &lt;itemno&gt;1001&lt;/itemno&gt; 
    &lt;bid&gt;35&lt;/bid&gt; 
    &lt;bid_date&gt;1999-01-07&lt;/bid_date&gt; 
  &lt;/bid_tuple&gt; 
  &lt;bid_tuple&gt; 
    &lt;userid&gt;U04&lt;/userid&gt; 
    &lt;itemno&gt;1001&lt;/itemno&gt; 
    &lt;bid&gt;40&lt;/bid&gt; 
    &lt;bid_date&gt;1999-01-08&lt;/bid_date&gt; 
  &lt;/bid_tuple&gt; 
<em>  ... Snip ... </em>
  &lt;bid_tuple&gt; 
    &lt;userid&gt;U02&lt;/userid&gt; 
    &lt;itemno&gt;1002&lt;/itemno&gt; 
    &lt;bid&gt;600&lt;/bid&gt; 
    &lt;bid_date&gt;1999-02-16&lt;/bid_date&gt; 
  &lt;/bid_tuple&gt; 
  &lt;bid_tuple&gt; 
    &lt;userid&gt;U04&lt;/userid&gt; 
    &lt;itemno&gt;1002&lt;/itemno&gt; 
    &lt;bid&gt;1000&lt;/bid&gt; 
    &lt;bid_date&gt;1999-02-25&lt;/bid_date&gt; 
  &lt;/bid_tuple&gt; 
<em>  ... Snip ... </em>
  &lt;bid_tuple&gt; 
    &lt;userid&gt;U04&lt;/userid&gt; 
    &lt;itemno&gt;1007&lt;/itemno&gt; 
    &lt;bid&gt;225&lt;/bid&gt; 
    &lt;bid_date&gt;1999-02-12&lt;/bid_date&gt; 
  &lt;/bid_tuple&gt; 
&lt;/bids&gt;

</pre></div>
</div>
<div class="div4">
<h5><a name="rdb-sx-results-q4" id="rdb-sx-results-q4"></a>1.1.3.4
Q4</h5>
<p>Monitor the bidding on the helicopter. If Roger Smith does not
have the current highest bid, add a bid for him that is one higher
than the top bid, unless the bid amount exceeds a given limit. Stop
monitoring when the auction has finished.</p>
<p><em>Solution in the XQuery Scripting Extension:</em></p>
<table border="1" summary="Editorial note">
<tr>
<td align="left" valign="top" width="50%"><b>Editorial
note</b></td>
<td align="right" valign="top" width="50%">2008-01-25</td>
</tr>
<tr>
<td colspan="2" align="left" valign="top">A naive execution of this
query would form a busy loop waiting for the auction to end. This
is not very desirable, so it's possible an alternative solution
should be found.</td>
</tr>
</table>
<table border="1" summary="Editorial note">
<tr>
<td align="left" valign="top" width="50%"><b>Editorial
note</b></td>
<td align="right" valign="top" width="50%">2008-01-25</td>
</tr>
<tr>
<td colspan="2" align="left" valign="top">The fn:current-date()
function is defined by XQuery as <a href=
"http://www.w3.org/TR/xpath-functions/#stable">"stable"</a>. This
means that the query's current dateTime/date/time will not change
for the duration of the query. It needs to be decided if we intend
queries to be able to have a notion of time passing, possibly by
defining the "execution scope" differently for scripting.</td>
</tr>
</table>
<div class="exampleInner">
<pre>
declare variable $uid := doc("users.xml")/users/user_tuple
  [name = "Roger Smith"]/userid;
declare variable $item := doc("items.xml")/items/item_tuple
  [description = "Helicopter"];
declare variable $result :=
  "Error: The auction has already ended or no bids were placed";
declare variable $maximumExceeded := false();

while(xs:date($item/end_date) &gt;= fn:current-date() and not($maximumExceeded)) {
  let $bids := doc("bids.xml")/bids/bid_tuple[itemno = $item/itemno]
  let $topbid := max($bids/bid)
  let $newbid = $topbid + 1
  where $bids[bid = $topbid]/userid != $uid
  return
    if($newbid &lt;= 60000) then (
      insert nodes
        &lt;bid_tuple&gt;
          { $uid, $item/itemno }
          &lt;bid&gt;{ $newbid }&lt;/bid&gt; 
          &lt;bid_date&gt;{ fn:current-date() }&lt;/bid_date&gt; 
        &lt;/bid_tuple&gt;
      into doc("bids.xml")/bids;
      set $result := concat("What a bargain! You got a helicopter for ",
        $newbid);
    ) else (
      set $result := "Bidding exceeded 60000";
      set $maximumExceeded := true();
    )
};

$result;
</pre></div>
<p><em>Expected Result:</em></p>
<p>Since this document was published after the end date for the
auction ("1999-05-25"), the while loop will never be executed.</p>
<div class="exampleInner">
<pre>
Error: The auction has already ended or no bids were placed
</pre></div>
</div>
</div>
</div>
<div class="div2">
<h3><a name="xhtml" id="xhtml"></a>1.2 Use Case "XHTML" - AJAX
Scripting with XHTML</h3>
<p>XQuery is ideally suited to manipulating the XML trees of XHTML.
This use case speculates that XQuery could be used in the way
Javascript is now, to modify an XHTML web page whilst it is being
displayed. In this way XQuery can be used to implement effects like
those commonly referred to by the acronym AJAX.</p>
<p>This use case deals with a web page that can be used to perform
a search in a book database. In all queries, there is an assumption
that the xhtml document is supplied as the context item.</p>
<div class="div3">
<h4><a name="xhtml-data" id="xhtml-data"></a>1.2.1 Input Data</h4>
<p>The starting state of the web page consists of only a simple
XHTML form.</p>
<div class="exampleInner">
<pre>
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
    &lt;title&gt;Book Search&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;form&gt;
      Book Name: &lt;input type="text" id="txtBookName"/&gt;
      &lt;input type="button" id="btnSend" value="Start Search"/&gt;
    &lt;/form&gt;
  &lt;/body&gt;
&lt;/html&gt;
</pre></div>
</div>
<div class="div3">
<h4><a name="xhtml-sx-results" id="xhtml-sx-results"></a>1.2.2
Queries and Results</h4>
<div class="div4">
<h5><a name="xhtml-sx-results-q1" id=
"xhtml-sx-results-q1"></a>1.2.2.1 Q1</h5>
<p>Write a script to act as an "onclick" event handler for the
"btnSend" form button. The script calls a web service to search for
the book entered by the user and retrieve detailed information for
the book. While the user is waiting for the web service the message
"Loading Book" is displayed. This message is later replaced with
the detailed information for the book.</p>
<p><em>Input web page:</em></p>
<p>The web browser has already updated the content of the form's
text input with the user's search string, "XQuery".</p>
<div class="exampleInner">
<pre>
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
    &lt;title&gt;Book Search&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;form&gt;
      Book Name: &lt;input type="text" id="txtBookName"&gt;XQuery&lt;/input&gt;
      &lt;input type="button" id="btnSend" value="Start Search"/&gt;
    &lt;/form&gt;
  &lt;/body&gt;
&lt;/html&gt;
</pre></div>
<p><em>Solution in the XQuery Scripting Extension:</em></p>
<p>The element that caused the event is supplied as an external
variable called $eventNode. The web service is called using the
external functions "book:search", and "book:get".</p>
<div class="exampleInner">
<pre>
declare namespace xhtml="http://www.w3.org/1999/xhtml";
declare namespace book="http://www.example.com/booksearch";

declare simple function book:search($name as xs:string) as element(book)* external;
declare simple function book:get($isbn as xs:string) as element(bookinfo) external;

declare variable $eventNode as element() external;
declare $searchResults := ();

insert node &lt;xhtml:div&gt;Loading Book&lt;/xhtml:div&gt;
after /xhtml:html/xhtml:body/xhtml:form;

set $searchResults :=
  book:search($eventNode/preceding-sibling::xhtml:input[1]);

if($searchResults) then (
  replace node /xhtml:html/xhtml:body/xhtml:div
  with &lt;xhtml:div&gt;{
    book:get($searchResults[1]/isbn)/html/node()
  }&lt;/xhtml:div&gt;;
)
else (
  replace node /xhtml:html/xhtml:body/xhtml:div
  with &lt;xhtml:div&gt;No books found!&lt;/xhtml:div&gt;;
)
</pre></div>
<p><em>Results returned to the query from the function call
book:search("XQuery"):</em></p>
<div class="exampleInner">
<pre>
&lt;book&gt;
  &lt;title&gt;XQuery from the Experts: A Guide to the W3C XML Query Language&lt;/title&gt;
  &lt;isbn&gt;0321180607&lt;/isbn&gt;
&lt;/book&gt;
&lt;book&gt;
  &lt;title&gt;XQuery: The XML Query Language&lt;/title&gt;
  &lt;isbn&gt;0321165810&lt;/isbn&gt;
&lt;/book&gt;
</pre></div>
<p><em>Results returned to the query from the function call
book:get("0321180607"):</em></p>
<div class="exampleInner">
<pre>
&lt;bookinfo&gt;
  &lt;title&gt;XQuery from the Experts: A Guide to the W3C XML Query Language&lt;/title&gt;
  &lt;isbn&gt;0321180607&lt;/isbn&gt;
  &lt;html&gt;
    &lt;h2 xmlns="http://www.w3.org/1999/xhtml"&gt;XQuery from the Experts: A Guide to the W3C XML Query Language&lt;/h2&gt;
    ISBN: 0321180607
  &lt;/html&gt;
&lt;/bookinfo&gt;
</pre></div>
<p><em>Expected intermediate content of the web page:</em></p>
<div class="exampleInner">
<pre>
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
    &lt;title&gt;Book Search&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;form&gt;
      Book Name: &lt;input type="text" id="txtBookName"&gt;XQuery&lt;/input&gt;
      &lt;input type="button" id="btnSend" value="Start Search"/&gt;
    &lt;/form&gt;
    &lt;xhtml:div xmlns:xhtml="http://www.w3.org/1999/xhtml"&gt;Loading book&lt;/xhtml:div&gt;
  &lt;/body&gt;
&lt;/html&gt;
</pre></div>
<p><em>Expected resulting content of the web page:</em></p>
<div class="exampleInner">
<pre>
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
    &lt;title&gt;Book Search&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;form&gt;
      Book Name: &lt;input type="text" id="txtBookName"&gt;XQuery&lt;/input&gt;
      &lt;input type="button" id="btnSend" value="Start Search"/&gt;
    &lt;/form&gt;
    &lt;xhtml:div xmlns:xhtml="http://www.w3.org/1999/xhtml"&gt;
      &lt;h2&gt;XQuery from the Experts: A Guide to the W3C XML Query Language&lt;/h2&gt;
      ISBN: 0321180607
    &lt;/xhtml:div&gt;
  &lt;/body&gt;
&lt;/html&gt;
</pre></div>
</div>
<div class="div4">
<h5><a name="xhtml-sx-results-q2" id=
"xhtml-sx-results-q2"></a>1.2.2.2 Q2</h5>
<p>Write a script to act as an "onclick" event handler for the
"btnSend" form button. The script calls a web service to search for
the book entered by the user, and uses an additional web service to
find which libraries have these books on the shelves. The
information received is displayed asynchronously as it arrives.</p>
<p><em>Input web page:</em></p>
<p>The web browser has already updated the content of the form's
text input with the user's search string, "XQuery".</p>
<div class="exampleInner">
<pre>
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
    &lt;title&gt;Book Search&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;form&gt;
      Book Name: &lt;input type="text" id="txtBookName"&gt;XQuery&lt;/input&gt;
      &lt;input type="button" id="btnSend" value="Start Search"/&gt;
    &lt;/form&gt;
  &lt;/body&gt;
&lt;/html&gt;
</pre></div>
<p><em>Solution in the XQuery Scripting Extension:</em></p>
<p>The element that caused the event is supplied as an external
variable called $eventNode. The web services are called using the
external functions "book:search", and "library:find".</p>
<div class="exampleInner">
<pre>
declare namespace xhtml="http://www.w3.org/1999/xhtml";
declare namespace book="http://www.example.com/booksearch";
declare namespace library="http://www.example.com/library";

declare simple function book:search($name as xs:string)
  as element(book)* external;
declare simple function library:find($isbn as xs:string)
  as element(library)* external;

declare variable $eventNode as element() external;
declare variable $table := ();

insert node &lt;xhtml:div&gt;&lt;xhtml:table/&gt;&lt;/xhtml:div&gt;
after /xhtml:html/xhtml:body/xhtml:form;
set $table := //xhtml:table;

for $book in book:search($eventNode/preceding-sibling::xhtml:input[1])
return (
  insert node
    &lt;xhtml:tr&gt;
      &lt;xhtml:td&gt;{data($book/title)}&lt;/xhtml:td&gt;
      &lt;xhtml:td&gt;{data($book/isbn)}&lt;/xhtml:td&gt;
      &lt;xhtml:td/&gt;
    &lt;/xhtml:tr&gt;
  as last into $table;
);

for $row in $table/xhtml:tr
return (
  replace value of node $row/xhtml:td[3]
  with string-join(library:find($row/xhtml:td[2])/name, ", ");
);
</pre></div>
<p><em>Results returned to the query from the function call
book:search("XQuery"):</em></p>
<div class="exampleInner">
<pre>
&lt;book&gt;
  &lt;title&gt;XQuery from the Experts: A Guide to the W3C XML Query Language&lt;/title&gt;
  &lt;isbn&gt;0321180607&lt;/isbn&gt;
&lt;/book&gt;
&lt;book&gt;
  &lt;title&gt;XQuery: The XML Query Language&lt;/title&gt;
  &lt;isbn&gt;0321165810&lt;/isbn&gt;
&lt;/book&gt;
</pre></div>
<p><em>Results returned to the query from the function call
library:find("0321180607"):</em></p>
<div class="exampleInner">
<pre>
&lt;library&gt;
  &lt;name&gt;Bodleian Library&lt;/name&gt;
  &lt;url&gt;http://www.bodley.ox.ac.uk/&lt;/url&gt;
  &lt;lending&gt;no&lt;/lending&gt;
&lt;/library&gt;
&lt;library&gt;
  &lt;name&gt;Radcliffe Science Library&lt;/name&gt;
  &lt;url&gt;http://www.ouls.ox.ac.uk/rsl/&lt;/url&gt;
  &lt;lending&gt;yes&lt;/lending&gt;
&lt;/library&gt;
</pre></div>
<p><em>Results returned to the query from the function call
library:find("0321165810"):</em></p>
<div class="exampleInner">
<pre>
&lt;library&gt;
  &lt;name&gt;Radcliffe Science Library&lt;/name&gt;
  &lt;url&gt;http://www.ouls.ox.ac.uk/rsl/&lt;/url&gt;
  &lt;lending&gt;yes&lt;/lending&gt;
&lt;/library&gt;
</pre></div>
<p><em>Expected resulting content of the web page:</em></p>
<p>The web page will be altered as the results are received from
the web services, but the final result will look like this.</p>
<div class="exampleInner">
<pre>
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
    &lt;title&gt;Book Search&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;form&gt;
      Book Name: &lt;input type="text" id="txtBookName"&gt;XQuery&lt;/input&gt;
      &lt;input type="button" id="btnSend" value="Start Search"/&gt;
    &lt;/form&gt;
    &lt;xhtml:div xmlns:xhtml="http://www.w3.org/1999/xhtml"&gt;
      &lt;xhtml:table&gt;
        &lt;xhtml:tr&gt;
          &lt;xhtml:td&gt;XQuery from the Experts: A Guide to the W3C XML Query Language&lt;/xhtml:td&gt;
          &lt;xhtml:td&gt;0321180607&lt;/xhtml:td&gt;
          &lt;xhtml:td&gt;Bodleian Library, Radcliffe Science Library&lt;/xhtml:td&gt;
        &lt;/xhtml:tr&gt;
        &lt;xhtml:tr&gt;
          &lt;xhtml:td&gt;XQuery: The XML Query Language&lt;/xhtml:td&gt;
          &lt;xhtml:td&gt;0321165810&lt;/xhtml:td&gt;
          &lt;xhtml:td&gt;Radcliffe Science Library&lt;/xhtml:td&gt;
        &lt;/xhtml:tr&gt;
      &lt;/xhtml:table&gt;
    &lt;/xhtml:div&gt;
  &lt;/body&gt;
&lt;/html&gt;
</pre></div>
</div>
</div>
</div>
<div class="div2">
<h3><a name="webservice" id="webservice"></a>1.3 Use Case "WS" -
XQuery for Web Services</h3>
<p>This use case deals with the implementation of a REST based web
service, specifically a micro-blog.</p>
<div class="div3">
<h4><a name="webservice-data" id="webservice-data"></a>1.3.1 Input
Data</h4>
<p>The default collection contains a forest of micro blog
documents, one for each user. Initially the forest contains these
documents.</p>
<p><em>John's micro-blog:</em></p>
<div class="exampleInner">
<pre>
&lt;micro-blog user="john"&gt;
  &lt;entry timestamp="2007-10-17T10:02:53+01:00"&gt;
    &lt;text&gt;Still reading email...&lt;/text&gt;
  &lt;/entry&gt;
  &lt;entry timestamp="2007-10-17T20:19:31+01:00"&gt;
    &lt;text&gt;Writing XQuery Scripting Extension use cases (ideas, anyone?)&lt;/text&gt;
  &lt;/entry&gt;
  &lt;entry timestamp="2007-10-18T00:43:02+01:00"&gt;
    &lt;text&gt;Thinking of going to bed&lt;/text&gt;
  &lt;/entry&gt;
&lt;/micro-blog&gt;
</pre></div>
<p><em>Emily's micro-blog:</em></p>
<div class="exampleInner">
<pre>
&lt;micro-blog user="emily"&gt;
  &lt;entry timestamp="2007-10-16T18:12:51+01:00"&gt;
    &lt;text&gt;So how do you use a micro-blog?&lt;/text&gt;
  &lt;/entry&gt;
  &lt;entry timestamp="2007-10-16T18:13:20+01:00"&gt;
    &lt;text&gt;Ah, I see&lt;/text&gt;
  &lt;/entry&gt;
  &lt;entry timestamp="2007-10-17T07:33:44+01:00"&gt;
    &lt;text&gt;Morning, everybody!&lt;/text&gt;
  &lt;/entry&gt;
&lt;/micro-blog&gt;
</pre></div>
<p><em>A document containing the blog access log:</em></p>
<div class="exampleInner">
<pre>
&lt;log&gt;
&lt;/log&gt;
</pre></div>
</div>
<div class="div3">
<h4><a name="webservice-sx-results" id=
"webservice-sx-results"></a>1.3.2 Queries and Results</h4>
<div class="div4">
<h5><a name="webservice-sx-results-q1" id=
"webservice-sx-results-q1"></a>1.3.2.1 Q1</h5>
<p>Process a request from John to add a blog entry, returning a
confirmation XHTML page.</p>
<p><em>The element representing the request:</em></p>
<div class="exampleInner">
<pre>
&lt;request&gt;
  &lt;method&gt;POST&lt;/method&gt;
  &lt;url&gt;http://blog.example.com/john/add&lt;/url&gt;
  &lt;param name="text"&gt;Making a post to my XQuery blog engine&lt;/param&gt;
&lt;/request&gt;
</pre></div>
<p><em>Solution in the XQuery Scripting Extension:</em></p>
<p>The request element is bound to the external variable
$request.</p>
<div class="exampleInner">
<pre>
declare variable $request as element(request) external;

declare simple function local:error($message)
{
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
    &lt;head&gt;
      &lt;title&gt;Error&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;h1&gt;Error&lt;/h1&gt;
      &lt;p&gt;{ $message }&lt;/p&gt;
    &lt;/body&gt;
  &lt;/html&gt;
};

if($request/method = "POST") then () else
  exit with local:error(concat("You cannot use the ",
    $request/method, " method with this URL."));

let $user := replace($request/url, "^http://.*/([^/]+)/add$", "$1")
let $blog := collection()/micro-blog[@user = $user]
return (
  if($blog) then () else
    exit with local:error("Unknown user");

  insert node
    &lt;entry timestamp="{ current-dateTime() }"&gt;
      &lt;text&gt;{ $request/param[@name = "text"] }&lt;/text&gt;
    &lt;/entry&gt;
  as last into $blog;

  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
    &lt;head&gt;
      &lt;title&gt;Blog entry added for { $user }&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;h1&gt;Blog entry added for { $user }&lt;/h1&gt;
      &lt;p&gt;{ $request/param[@name = "text"] }&lt;/p&gt;
    &lt;/body&gt;
  &lt;/html&gt;;
);
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
    &lt;title&gt;Blog entry added for john&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h1&gt;Blog entry added for john&lt;/h1&gt;
    &lt;p&gt;Making a post to my XQuery blog engine&lt;/p&gt;
  &lt;/body&gt;
&lt;/html&gt;
</pre></div>
<p><em>Expected resulting content of John's micro-blog:</em></p>
<div class="exampleInner">
<pre>
&lt;micro-blog user="john"&gt;
  &lt;entry timestamp="2007-10-17T10:02:53+01:00"&gt;
    &lt;text&gt;Still reading email...&lt;/text&gt;
  &lt;/entry&gt;
  &lt;entry timestamp="2007-10-17T20:19:31+01:00"&gt;
    &lt;text&gt;Writing XQuery Scripting Extension use cases (ideas, anyone?)&lt;/text&gt;
  &lt;/entry&gt;
  &lt;entry timestamp="2007-10-18T00:43:02+01:00"&gt;
    &lt;text&gt;Thinking of going to bed&lt;/text&gt;
  &lt;/entry&gt;
  &lt;entry timestamp="2007-10-18T08:53:9+01:00"&gt;
    &lt;text&gt;Making a post to my XQuery blog engine&lt;/text&gt;
  &lt;/entry&gt;
&lt;/micro-blog&gt;
</pre></div>
</div>
<div class="div4">
<h5><a name="webservice-sx-results-q2" id=
"webservice-sx-results-q2"></a>1.3.2.2 Q2</h5>
<p>Process the same request from John, using a function to check
that "john" is a valid user name and to log the event.</p>
<p><em>The element representing the request:</em></p>
<div class="exampleInner">
<pre>
&lt;request&gt;
  &lt;method&gt;POST&lt;/method&gt;
  &lt;url&gt;http://blog.example.com/john/add&lt;/url&gt;
  &lt;param name="text"&gt;Making a post to my XQuery blog engine&lt;/param&gt;
&lt;/request&gt;
</pre></div>
<p><em>Solution in the XQuery Scripting Extension:</em></p>
<p>The request element is bound to the external variable
$request.</p>
<div class="exampleInner">
<pre>
declare variable $request as element(request) external;

declare sequential function local:check-user-and-log($username as xs:string) 
  as element(micro-blog)?
{
  declare $entry as element() :=
    &lt;access-attempt&gt;
      &lt;timestamp&gt;{fn:current-dateTime()}&lt;/timestamp&gt;
      &lt;user-name&gt;{$username}&lt;/user-name&gt;
      &lt;access-allowed/&gt;
    &lt;/access-attempt&gt;;
  declare $blog as element(micro-blog)? :=
    collection()/micro-blog[@user = $username];

  if($blog) then
    replace value of node $entry/access-allowed with "Yes"
  else
    replace value of node $entry/access-allowed with "No";

  insert node $entry as last into collection()/log;
  exit with $blog;
} 


declare simple function local:error($message)
{
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
    &lt;head&gt;
      &lt;title&gt;Error&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;h1&gt;Error&lt;/h1&gt;
      &lt;p&gt;{ $message }&lt;/p&gt;
    &lt;/body&gt;
  &lt;/html&gt;
};

if($request/method = "POST") then () else
  exit with local:error(concat("You cannot use the ",
    $request/method, " method with this URL."));

let $user := replace($request/url, "^http://.*/([^/]+)/add$", "$1")
let $blog := local:check-user-and-log($user)
return (
  if($blog) then () else
    exit with local:error("Unknown user");

  insert node
    &lt;entry timestamp="{ current-dateTime() }"&gt;
      &lt;text&gt;{ $request/param[@name = "text"] }&lt;/text&gt;
    &lt;/entry&gt;
  as last into $blog;

  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
    &lt;head&gt;
      &lt;title&gt;Blog entry added for { $user }&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;h1&gt;Blog entry added for { $user }&lt;/h1&gt;
      &lt;p&gt;{ $request/param[@name = "text"] }&lt;/p&gt;
    &lt;/body&gt;
  &lt;/html&gt;;
);
</pre></div>
<p><em>Expected Result:</em></p>
<div class="exampleInner">
<pre>
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
    &lt;title&gt;Blog entry added for john&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h1&gt;Blog entry added for john&lt;/h1&gt;
    &lt;p&gt;Making a post to my XQuery blog engine&lt;/p&gt;
  &lt;/body&gt;
&lt;/html&gt;
</pre></div>
<p><em>Expected resulting content of John's micro-blog:</em></p>
<div class="exampleInner">
<pre>
&lt;micro-blog user="john"&gt;
  &lt;entry timestamp="2007-10-17T10:02:53+01:00"&gt;
    &lt;text&gt;Still reading email...&lt;/text&gt;
  &lt;/entry&gt;
  &lt;entry timestamp="2007-10-17T20:19:31+01:00"&gt;
    &lt;text&gt;Writing XQuery Scripting Extension use cases (ideas, anyone?)&lt;/text&gt;
  &lt;/entry&gt;
  &lt;entry timestamp="2007-10-18T00:43:02+01:00"&gt;
    &lt;text&gt;Thinking of going to bed&lt;/text&gt;
  &lt;/entry&gt;
  &lt;entry timestamp="2007-10-18T08:53:9+01:00"&gt;
    &lt;text&gt;Making a post to my XQuery blog engine&lt;/text&gt;
  &lt;/entry&gt;
&lt;/micro-blog&gt;
</pre></div>
<p><em>Expected resulting content of the log:</em></p>
<div class="exampleInner">
<pre>
&lt;log&gt;
  &lt;access-attempt&gt;
    &lt;timestamp&gt;2007-10-18T08:53:9+01:00&lt;/timestamp&gt;
    &lt;user-name&gt;john&lt;/user-name&gt;
    &lt;access-allowed&gt;Yes&lt;/access-allowed&gt;
  &lt;/access-attempt&gt;
&lt;/log&gt;
</pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="back">
<div class="div1">
<h2><a name="references" id="references"></a>A Normative
References</h2>
<dl>
<dt class="label"><span><a name="xquery" id="xquery"></a>XQuery
1.0</span></dt>
<dd>
<div>World Wide Web Consortium. <em>XQuery 1.0: An XML Query
Language</em>. W3C Recommendation, 23 January 2007. See <a href=
"http://www.w3.org/TR/xquery/">http://www.w3.org/TR/xquery/</a>.</div>
</dd>
<dt class="label"><span><a name="XQueryUseCases" id=
"XQueryUseCases"></a>XML Query Use Cases</span></dt>
<dd>
<div>World Wide Web Consortium. <em>XML Query Use Cases</em>. W3C
Working Group Note, 23 March 2007. See <a href=
"http://www.w3.org/TR/xquery-use-cases/">http://www.w3.org/TR/xquery-use-cases/</a>.</div>
</dd>
<dt class="label"><span><a name="updatespec" id=
"updatespec"></a>XQuery Update Facility</span></dt>
<dd>
<div>World Wide Web Consortium. <em>XQuery Update Facility</em>.
W3C Working Draft, 28 August 2007. See <a href=
"http://www.w3.org/TR/xquery-update-10/">http://www.w3.org/TR/xquery-update-10/</a>.</div>
</dd>
<dt class="label"><span><a name="serequirements" id=
"serequirements"></a>XQuery Scripting Extension 1.0
Requirements</span></dt>
<dd>
<div>World Wide Web Consortium. <em>XQuery Scripting Extension 1.0
Requirements</em>. W3C Working Draft, 23 March 2007. See <a href=
"http://www.w3.org/TR/2007/WD-xquery-sx-10-requirements-20070323">http://www.w3.org/TR/2007/WD-xquery-sx-10-requirements-20070323</a>.</div>
</dd>
</dl>
</div>
<div class="div1">
<h2><a name="id-revisions-log" id="id-revisions-log"></a>B Revision
Log (Non-Normative)</h2>
<p>This log records the substantive changes that have been made to
this document . Minor editorial changes are not included in this
log.</p>
<div class="div2">
<h3><a name="id-log-pending" id="id-log-pending"></a>B.1 Changes in
internal WD</h3>
<ul>
<li>
<p>Added an editorial note detailing the problems with RDB Q4.</p>
</li>
<li>
<p>Added the WS Q2 use case.</p>
</li>
<li>
<p>2008-07-10: Updated the syntax to the lastest editor's
draft.</p>
</li>
<li>
<p>2008-11-09: Updated the syntax to the lastest editor's
draft.</p>
</li>
</ul>
</div>
</div>
</div>
</body>
</html>