-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathXQuery-Style-Guide.xml
1946 lines (1852 loc) · 90.2 KB
/
XQuery-Style-Guide.xml
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
<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="http://docbook.org/xml/5.1/rng/docbook.rng" schematypens="http://relaxng.org/ns/structure/1.0"?>
<?xml-model href="http://docbook.org/xml/5.1/sch/docbook.sch" type="application/xml" schematypens="http://purl.oclc.org/dsdl/schematron"?>
<article
xmlns="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink"
version="5.1">
<info>
<title>XQuery Style Conventions</title>
<author>
<orgname>xqDoc</orgname>
</author>
<author>
<personname>
<firstname>Loren</firstname>
<surname>Cahlander</surname>
</personname>
</author>
</info>
<section>
<title>Introduction</title>
<section>
<title>Why Have Style Conventions</title>
<para>Style conventions are important for a number of reasons:</para>
<itemizedlist>
<listitem>
<para>Common style conventions will decrease the long-term maintenance cost associated with XQuery modules.</para>
</listitem>
<listitem>
<para>Hardly any XQuery module will be maintained for its whole life by the original author.</para>
</listitem>
<listitem>
<para>Style conventions improve the readability of the XQuery modules, allowing everyone to understand the XQuery more quickly and thoroughly.</para>
</listitem>
</itemizedlist>
<para>The style conventions proposed in this document will not ensure the XQuery authored for one XQuery engine will work on a different XQuery engine. However, the migration to a different XQuery engine can be simplified by adhering to a consistent style when authoring XQuery modules.</para>
<para>Since the XQuery language is fully composable, any of the expressions can be nested within another, it is an insurmountable task to completely define style guidelines that will cover all cases. Instead, we have opted to offer general guidelines that should cover the more common scenarios.</para>
</section>
<section>
<title>Acknowledgments</title>
<para>This document reflects XQuery Style Conventions for the <citerefentry>
<refentrytitle>November 2005 W3C XQuery Language Candidate Recommendation</refentrytitle>
</citerefentry> . When XQuery becomes an official recommendation and as we receive feedback from the XQuery development commnity, this document will be updated as appropriate.</para>
<para>Many of the concepts and guidelines contained in this document have been borrowed with permission from the <citerefentry>
<refentrytitle>Code Conventions for the Java Programming Language</refentrytitle>
</citerefentry>, Copyright 1995-1999 Sun Microsystems, Inc. All rights reserved. Rather than re-inventing the wheel, we felt it would be best to leverage a familiar tested and proven style that has been widely embraced.</para>
</section>
</section>
<section>
<title>Module Organization</title>
<para>There are two basic types of module organizations for XQuery, library and main modules. The guidelines for each module type will be presented in the following sections. Appendixes A and B offer representative examples for both a main module and library module that follow the style guidelines described in this document. For complete details on XQuery main modules and library modules, we suggest reviewing the BNF for XQuery available from the W3C web site.</para>
<section>
<title>Library Modules</title>
<para>As a general rule, an XQuery library module will normally contain one or more XQuery functions and global variable declarations. All of these functions and variables should be related together in some meaningful fashion (related processing, support one another, etc.)</para>
<para>The XQuery library module should contain the sections identified below in the listed order.</para>
<orderedlist>
<listitem>
<para>XQuery version declaration</para>
</listitem>
<listitem>
<para>Beginning comments</para>
</listitem>
<listitem>
<para>Module declaration</para>
</listitem>
<listitem>
<para>Prolog</para>
</listitem>
</orderedlist>
<section>
<title>XQuery version declaration</title>
<para>All library modules should explicitly indicate the version of the XQuery specification that they adhere to as well as any optional special encoding information.</para>
<informalexample>
<programlisting>xquery version "1.0";
or
xquery version "1.0" encoding "UTF-8";</programlisting>
</informalexample>
</section>
<section>
<title>Beginning comments</title>
<para>All library modules should begin with an XQuery-style comment that lists the module name, version information, date, copyright, any vendor specific proprietary extension dependencies, XQuery specification and a module overview. See section 4 for more information on XQuery commenting style.</para>
<informalexample>
<programlisting>(:
: Module Name: Sample Library Module
: Module Version: 1.0
: Date: January 6, 2006
: Copyright: xqDoc.org …
: Proprietary XQuery Extensions Used: None
: XQuery Specification: November 2005
: Module Overview: This is a sample library module …
:)</programlisting>
</informalexample>
</section>
<section>
<title>Module declaration</title>
<para>All libraries must provide a module namespace declaration.</para>
<informalexample>
<programlisting>module namespace pfx=”some-uri”;</programlisting>
</informalexample>
</section>
<section>
<title>Prolog</title>
<para>The following table defines the recommended ordering of items in the prolog if they exist.</para>
<informaltable
frame="all"
rowsep="1"
colsep="1">
<tgroup
cols="2"
align="left">
<colspec
colname="c1"
colnum="1"
colwidth="1*"/>
<colspec
colname="c2"
colnum="2"
colwidth="1*"/>
<thead>
<row>
<entry>Item</entry>
<entry>Notes</entry>
</row>
</thead>
<tbody>
<row>
<entry>Imports</entry>
<entry>Any schema imports followed by module imports.</entry>
</row>
<row>
<entry>Namespace Declarations</entry>
<entry>The default function and element namespaces should precede any other namespace declarations.</entry>
</row>
<row>
<entry>Static Context Declarations</entry>
<entry>
<para>Static context declarations should appear in the following order:</para>
<itemizedlist>
<listitem>
<para>BaseURI</para>
</listitem>
<listitem>
<para>Copy Namespaces</para>
</listitem>
<listitem>
<para>Boundary Space</para>
</listitem>
<listitem>
<para>Default Collation</para>
</listitem>
<listitem>
<para>Ordering Mode</para>
</listitem>
<listitem>
<para>Empty Order</para>
</listitem>
<listitem>
<para>Construction</para>
</listitem>
</itemizedlist>
</entry>
</row>
<row>
<entry>Option Declarations</entry>
<entry>Implementation specific options.</entry>
</row>
<row>
<entry>Variable Declarations</entry>
<entry>Global variables.</entry>
</row>
<row>
<entry>Function Declarations</entry>
<entry>User defined functions.</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</section>
</section>
<section>
<title>Main Modules</title>
<para>As a general rule, an XQuery main module will normally contain just a query body. While the W3C XQuery specification allows for the definition of global variables and functions within a main module, this practice is generally not recommended since it prevents the reusability of these functions and variables by other library and main modules2 . However, since not all XQuery engines support the ability to import modules, it may become necessary in those situations to define functions and global variables within a main module. One exception to this general rule is that global variables marked as ‘external’ should only be defined in a main module. </para>
<para>The XQuery main module should contain the sections identified below in the listed order. </para>
<orderedlist>
<listitem>
<para>XQuery version declaration</para>
</listitem>
<listitem>
<para>Beginning comments</para>
</listitem>
<listitem>
<para>Prolog</para>
</listitem>
<listitem>
<para>Query body</para>
</listitem>
</orderedlist>
<section>
<title>XQuery version declaration</title>
<para>All main modules should explicitly indicate the version of the XQuery specification that they adhere to as well as any optional special encoding information.</para>
<informalexample>
<programlisting>xquery version "1.0";
or
xquery version "1.0" encoding "UTF-8";</programlisting>
</informalexample>
</section>
<section>
<title>Beginning comments</title>
<para>All main modules should begin with an XQuery-style comment that lists the module name, version information, date, copyright, any vendor specific proprietary extension dependencies, XQuery specification and module overview. </para>
<informalexample>
<programlisting>(:
: Module Name: Sample Main Module
: Module Version: 1.0
: Date: January 6, 2006
: Copyright: xqDoc.org …
: Proprietary XQuery Extensions Used: None
: XQuery Specification: November 2005
: Module Overview: This is a sample main module …
:)</programlisting>
</informalexample>
</section>
<section>
<title>Prolog</title>
<para>The following table defines the recommended ordering of items in the prolog if they exist.</para>
<informaltable
frame="all"
rowsep="1"
colsep="1">
<tgroup
cols="2"
align="left">
<colspec
colname="c1"
colnum="1"
colwidth="1*"/>
<colspec
colname="c2"
colnum="2"
colwidth="1*"/>
<thead>
<row>
<entry>Item</entry>
<entry>Notes</entry>
</row>
</thead>
<tbody>
<row>
<entry>Imports</entry>
<entry>Any schema imports followed by module imports.</entry>
</row>
<row>
<entry>Namespace Declarations</entry>
<entry>The default function and element namespaces should precede any other namespace declarations.</entry>
</row>
<row>
<entry>Static Context Declarations</entry>
<entry>
<para>Static context declarations should appear in the following order:</para>
<itemizedlist>
<listitem>
<para>BaseURI</para>
</listitem>
<listitem>
<para>Copy Namespaces</para>
</listitem>
<listitem>
<para>Boundary Space</para>
</listitem>
<listitem>
<para>Default Collation</para>
</listitem>
<listitem>
<para>Ordering Mode</para>
</listitem>
<listitem>
<para>Empty Order</para>
</listitem>
<listitem>
<para>Construction</para>
</listitem>
</itemizedlist>
</entry>
</row>
<row>
<entry>Option Declarations</entry>
<entry>Implementation specific options.</entry>
</row>
<row>
<entry>Variable Declarations</entry>
<entry>
<para>As a general rule, global variables should not be defined in a main module. However, if the XQuery engine does not support the ability to import modules, it may be necessary to define global variables in a main module.</para>
<para>One exception to this rule is that global variables marked as ‘external’, should only be defined in a main module.</para>
</entry>
</row>
<row>
<entry>Function Declarations</entry>
<entry>
<para>As a general rule, functions should not be defined in a main module. However, if the XQuery engine does not support the ability to import modules, it may be necessary to define functions in a main module.</para>
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</section>
<section>
<title>Query body</title>
<para>The Query body contains the XQuery expression(s) to evaluate.</para>
</section>
</section>
</section>
<section>
<title>Indentation</title>
<para>A consistent value should be used for the default unit of indentation to provide a clear indication of nesting and readability. The exact construction of the indentation (spaces vs. tabs) is unspecified. </para>
<section>
<title>Line Length</title>
<para>Avoid lines longer than 80 characters, since they’re not handled well by many terminals and tools.</para>
<note>
<para>Examples for use in documentation should have a shorter line length—generally no more than 70 characters.</para>
</note>
</section>
<section>
<title>Wrapping Lines</title>
<para>When an expression will not fit on a single line, break it according to these general principles:</para>
<itemizedlist>
<listitem>
<para>Break after commas.</para>
</listitem>
<listitem>
<para>Align the new line with the beginning of the expression at the same level on the previous line.</para>
</listitem>
<listitem>
<para>If the above rules lead to confusing XQuery or to XQuery that’s squished up against the right margin, just use the default indentation value.</para>
</listitem>
</itemizedlist>
<para>If no breaks are necessary, the function call should look as follows:</para>
<programlisting>some-function(expression1, expression2)</programlisting>
<para>Here are some examples of breaking function calls:</para>
<programlisting>some-function(longExpression1,
longExpression2)
some-function(longExpression1, longExpression2,
longExpression3, longExpression4, longExpression5)
some-function(longExpression1,
another-function(longExpression2,
longExpression3))</programlisting>
<para>Following are examples of breaking an arithmetic expression. The first is preferred, since the break occurs outside the parenthesized expression, which is at a higher level.</para>
<programlisting>let $longName1 := longName2 * (longName3 + longName4 - longName5)
+ 4 * longname6
let $longName1 := longName2 * (longName3 + longName4
- longName5) + 4 * longname6 </programlisting>
<para>Following are two examples of indenting function declarations. The first is the conventional case. The second would shift the second and third lines to the far right if it used conventional indentation, so instead it indents by the default indentation value. The body of the function should also be indented by the default indentation value.</para>
<programlisting>(: CONVENTIONAL INDENTATION :)
declare function some-function(arg1 as item(), arg2 as item(),
arg3 as item(), arg4 as item())
as item()
{
...
};
(: INDENT TO AVOID VERY DEEP INDENTS :)
declare function some-unction(arg1 as item(),
arg2 as item(),
arg3 as item())
as item()
{
let $x := “test”
….
};</programlisting>
<para>Line wrapping for if statements should generally use double the default indentation value, since the default indentation value might make seeing the body difficult. For example:</para>
<programlisting>(: DON’T USE THIS INDENTATION :)
if ((condition1 and condition2)
or (condition3 and condition4)
or (condition5 and condition6))
then
do-something-about-it()
else
()
(: USE THIS INDENTATION INSTEAD :)
if ((condition1 and condition2)
or (condition3 and condition4)
or (condition5 and condition6))
then
do-something-about-it()
else
()
(: OR USE THIS :)
if ((condition1 and condition2) or (condition3 and condition4)
or (condition5 and condition6))
then
do-something-about-it()
else
()</programlisting>
</section>
<section>
<title>Sequences</title>
<para>When specifying a long sequence of XQuery expressions, place the first expression following the opening parenthesis and list each subsequent expression on a separate line. Put the closing parenthesis on the same line and following the last expression</para>
<programlisting>(expression1,
expression2,
…
expressionn)</programlisting>
<para>When specifying a short sequence of XQuery expressions, place the first expression following the opening parenthesis and list each subsequent expression on the same line delimited by a comma and space. Put the closing parenthesis on the same line and following the last expression.</para>
<programlisting>(expression1, expression2)</programlisting>
</section>
<section>
<title>Annotations</title>
<para></para>
<programlisting>(:~
Search the metadata
@param $q The query string
@param $start The offset into the search results where the response begins
@param $pagelength The number of result items to return from the start position
@param $facets
This is a string containing the selected facets for the query and each facet is separtated by a double-tilde '~~'.
A facet is represented by the facet name and the value separated by a colon ':'
@param $debug If this is set to true, then the raw search results are returned.
@return The search results as JSON.
:)
declare
%rest:path("/search")
%rest:query-param-1('q', '{$q}', '*')
%rest:query-param-2('start', '{$start}', '1')
%rest:query-param-3('pagelength', '{$pagelength}', '10')
%rest:query-param-4('facets', '{$facets}', '')
%rest:query-param-5('debug', '{$debug}')
%rest:produces("application/json")
function emh-search:perform-search(
$q as xs:string,
$start as xs:integer?,
$pagelength as xs:integer?,
$facets as xs:string?,
$debug as xs:boolean?)
as document()
{
...
};
</programlisting>
</section>
<section>
<title>Embedded XQuery</title>
<para>When embedding XQuery statements within XML or XHTML markup, the following conventions should be followed. The opening and closing curly brackets should follow and precede the corresponding markup tags and the initial XQuery statement be indented by the default indentation value from the markup tag. After that, the rules described above should be followed for the XQuery expression.</para>
<programlisting><tag>{
if (condition) then
do-something-about-it()
else
()
}</tag></programlisting>
<para>If the XQuery statement is simple, it is also acceptable to include the statement on the same line as the markup tags. In this situation, the opening and closing curly brackets should follow and precede the corresponding markup tags and the XQuery statement should be delimited by a single space from the curly brackets. </para>
<programlisting><tag>{ $tag }</tag></programlisting>
</section>
</section>
<section>
<title>Comments</title>
<note>
<para>See “XQuery Source File Examples” contained in Appendix A and B for examples of the comment formats described here.</para>
</note>
<para>XQuery modules can have two kinds of comments: implementation comments and documentation comments. Implementation comments are those found in the W3C XQuery Specification, are delimited by (: ... :). Documentation comments (known as “doc comments”) have been defined by xqdoc.org and are delimited by (:~ ... :). Doc comments can be extracted to HTML files using the xqDoc tool.</para>
<para>Implementation comments are means for commenting out sections of a module or for comments about the particular implementation. Doc comments are meant to describe the specification of the module, from an implementation-free perspective to be read by others who might not necessarily have the module at hand.</para>
<para>Comments should be used to give overviews of the module and provide additional information that is not readily available in the module itself. Comments should contain only information that is relevant to reading and understanding the module. </para>
<section>
<title>Implementation Comment Formats</title>
<para>Modules can have three styles of implementation comments: block, single-line, trailing and end-of-line.</para>
<section>
<title>Block Comments</title>
<para>Block comments are used to provide descriptions of modules, functions, variables and algorithms. Block comments may be used at the beginning of each module and before each function. They can also be used in other places, such as within functions. Block comments inside a function should be indented to the same level as the XQuery fragment they describe.</para>
<para>A block comment should be preceded by a blank line to set it apart from the rest of the code. If multiple lines are needed, begin each subsequent line with a “:” and vertically align the colons.</para>
<programlisting>(:
: Here is a block comment. It is a rather long comment to provide an
: example for a multi-line block comment.
:)</programlisting>
</section>
<section>
<title>Single-Line Comments</title>
<para>Short comments can appear on a single line indented to the level of the XQuery that follows. If a comment can’t be written in a single line, it should follow the block comment format (see section 4.1.1). A single-line comment should be preceded by a blank line. Here’s an example of a singleline comment XQuery:</para>
<programlisting>if (condition) then
(: Handle the condition. :)
...
else
()</programlisting>
</section>
<section>
<title>Trailing Comments</title>
<para>Very short comments can appear on the same line as the XQuery they describe, but should be © xqdoc.org Version 1.0 12 shifted far enough to separate them from the statements. If more than one short comment appears in a chunk of XQuery, they should all be vertically aligned.</para>
<para>Here’s an example of a trailing comment in XQuery:</para>
<programlisting>if (condition) then
some-function() (: a comment :)
else
another-function() (: a comment :) </programlisting>
</section>
</section>
<section>
<title>Documentation Comments</title>
<para>xqDoc comments should be used to document XQuery library and main modules in a manner similar to how Javadoc comments are used to document Java classes. With the documentation close to the source, it increases the chances that the documentation will be kept current and with tools provided by xqDoc, useful documentation can be quickly and easily generated. </para>
<para>xqDoc style comments begin with a '(:~' and end with a ':)' Like Javadoc, the following tags have special meaning within an xqDoc comment. In addition, the values provided for each of these tags can contain embedded MarkDown markup to enhance or emphasize the xqDoc XHTML presentation. The beginning text up to the first xqDoc tag (i.e. @author) in a xqDoc comment block is assumed to be description text for the component being documented.</para>
<glosslist>
<glossentry>
<glossterm>@author</glossterm>
<glossdef>
<para>The @author tag identifies the author for the documented component. Zero or more @author tags can
be specified (one per author) </para>
<programlisting>@author Loren Cahlander</programlisting>
</glossdef>
</glossentry>
<glossentry>
<glossterm>@version</glossterm>
<glossdef>
<para>The @version tag identifies the version of the documented component. Zero or more @version tags
can be specified (one per version) but in reality only a single @version tag would normally make
sense. The value for the @version tag can be an arbitrary string. </para>
<programlisting>@version 1.0 </programlisting>
</glossdef>
</glossentry>
<glossentry>
<glossterm>@since</glossterm>
<glossdef>
<para>The @since tag identifies the version when a documented component was supported. Zero or many
@since tags can be specified, but in reality only a single @since tag would normally make sense. The
value for the @since tag can be an arbitrary string but should likely match an appropriate version
value. </para>
<programlisting>@since 1.0</programlisting>
</glossdef>
</glossentry>
<glossentry>
<glossterm>@see</glossterm>
<glossdef>
<para>The @see tag provides the ability to hypertext link to an external web site, a library or main module
contained in xqDoc, a specific function (or variable) defined in a library or main module contained in
xqDoc, or arbitrary text. To link to an external site, use a complete URL such as
http://www.xquery.com. To link to a library or main module contained in xqDoc, simply provide the
URI for the library or main module. To link to a specific function (or variable) defined in an xqDoc
library or main module, simply provide the URI for the library or main module followed by a ';' and
finally the function or variable name. To provide a name for a link, simply include a second ';'
followed by the name. To provide text, simply include the 'text'. Multiple @see tags can be specified
(one per link or string of text). </para>
<programlisting>@see http://www.xquery.com
@see http://xqdoc.org/xqdoc/xqdoc-display
@see http://xqdoc.org/xqdoc/xqdoc-display;build-link
@see http://xqdoc.org/xqdoc/xqdoc-display;$months
@see http://xqdoc.org/xqdoc/xqdoc/xqdoc-display;$months;month variable
@see http://www.xquery.com;;xquery
@see some text</programlisting>
</glossdef>
</glossentry>
<glossentry>
<glossterm>@param</glossterm>
<glossdef>
<para>The @param tag identifies the parameters associated with a function. For each parameter in a
function, there should be a @param tag. The @param tag should be followed by the parameter name
(as indicated in the function signature) and then the parameter description.</para>
<programlisting>@param $name The username </programlisting>
</glossdef>
</glossentry>
<glossentry>
<glossterm>@return</glossterm>
<glossdef>
<para>The @return tag describes what is returned from a function. Zero or one @return tags can be
specified. </para>
<programlisting>@return Sequence of names matching the search criteria</programlisting>
</glossdef>
</glossentry>
<glossentry>
<glossterm>@deprecated</glossterm>
<glossdef>
<para>The @deprecated tag identifies the identifies the documented component as being deprecated. The
string of text associated with the @deprecated tag should indicate when the item was deprecated and
what to use as a replacement. </para>
<programlisting>@deprecated As of 1.0 and replaced with add-user </programlisting>
</glossdef>
</glossentry>
<glossentry>
<glossterm>@error</glossterm>
<glossdef>
<para>The @error tag identifies the types of errors that can be generated by the function. Zero or more
@error tags can be specified. An arbitrary string of text can be provided for a value. </para>
<programlisting>@error The requested URI does not exist </programlisting>
</glossdef>
</glossentry>
<glossentry>
<glossterm>@custom</glossterm>
<glossdef>
<para></para>
<programlisting>@custom:foo foo value</programlisting>
</glossdef>
</glossentry>
</glosslist>
<para>A representative library module xqDoc comment is included below. This comment would precede the module declaration statement for the library module. </para>
<programlisting>(:~
: This module provides the functions that control the Web presentation
: of xqDoc. The logic contained in this module is not specific to any
: XQuery implementation.
: It should also be noted that these functions not only support the
: real-time presentation of the xqDoc information but are also used
: for the static offline presentation mode as well. The static offline
: presentation mode has advantages because access to a native XML
: database is not needed when viewing the xqDoc information ... it is
: only needed when generating the offline materials.
:
: @author Darin McBeath
: @version 1.0
:)
module namespace xqdoc="http://xqdoc.org/xqdoc/display" </programlisting>
<para>A representative library module xqDoc function comment is included below. Note how embedded MarkDown markup can be used within an xqDoc description to enhance the presentation when the xqDoc comments are viewed. This comment would precede the function declaration statement in the library module. </para>
<programlisting>(:~
: The controller for constructing the xqDoc HTML information for
: the specified library module. The following information for
: each library module will be generated.
:
: * Module introductory information
: * Global variables declared in this module
: * Modules imported by this module
: * Summary information for each function defined in the module
: * Detailed information for each function defined in the module
:
: @param $uri the URI for the library module
: @param $local indicates whether to build static HTML link for offline
: viewing or dynamic links for real-time viewing.
: @return XHTML.
:)
declare function xqdoc:print-module($uri as xs:string, $local as xs:boolean)
as element()*
{</programlisting>
<para>For further details about doc comments and xqDoc, see the xqDoc home page at: http://xqdoc.org</para>
<para>As a general rule, if you need to give information about a module, import, variable, or function that isn’t appropriate for documentation, use an implementation block comment (see section 4.1.1) or single-line (see section 4.1.2) comment immediately after the declaration. For example, details about the implementation of a module should go in such an implementation block and not in the module doc comment.</para>
</section>
</section>
<section>
<title>Declarations</title>
<para></para>
<section>
<title>Local variable declarations</title>
<para>One local variable declaration (and assignment) per let clause is recommended since it helps with clarity. For example,</para>
<programlisting>let $givenName := “Loren”
let $surname := “Cahlander”</programlisting>
<para>is preferred over</para>
<programlisting>let $givenName := “Loren”,
$surname := “Cahlander”</programlisting>
</section>
<section>
<title>Function declarations</title>
<para>When authoring a function, the following rules should be followed:</para>
<itemizedlist>
<listitem>
<para>No space between the function name and the parenthesis “(“ starting its parameter list.</para>
</listitem>
<listitem>
<para>Follow conventions listed in section 3.2 for dealing with large parameter lists</para>
</listitem>
<listitem>
<para>The return type should appear on a separate line and vertically align with the declare function statement.</para>
</listitem>
<listitem>
<para>The opening brace “{“ should appear on the line following the return type and vertically align with the declare function statement.</para>
</listitem>
<listitem>
<para>XQuery expression(s) within the function should follow the indentation conventions described in section 3.</para>
</listitem>
<listitem>
<para>The closing brace and semi-colon “};” should appear on a separate line and vertically align with the declare function statement</para>
<programlisting>declare function some-function($arg1 as item(),
$arg2 as item())
as item()
{
let $givenName := “Darin”
…
};</programlisting>
</listitem>
</itemizedlist>
</section>
</section>
<section>
<title>Expressions</title>
<para>There are a number of expressions defined in the XQuery specification. Since XQuery is a fully composable language (specifically since expressions can be nested within one another), it is a difficult task to completely cover all scenarios. Instead, the following sections highlight the recommended format for a handful of expressions that are frequently used when authoring XQuery and offer some general guidelines.</para>
<section>
<title>if-the-else expressions</title>
<para>A general rule to follow is that the ‘if’, ‘else’, ‘else if’ associated with the same ‘if’ clause should all be on a separate line and vertically aligned. The expression associated with the true or false condition should be indented (as described in section 3). The if-then-else class of expressions should have one of the following forms:</para>
<programlisting>if (condition)
then
...
else
...
if (condition)
then
...
else if (condition)
then
...
else
...</programlisting>
<para>For simple and short if-then-else expressions, it is also acceptable to include the entire expression on a single line. In this scenario, the if-then-else expression should have the following form: </para>
<programlisting>if (condition) then ... else ...</programlisting>
</section>
<section>
<title>FLWOR expressions</title>
<para>The FLWOR expression is more complex than the basic if-then-else expression. A general rule to follow is that the ‘let’, ‘for’, ‘where’, ‘order by’, and ‘return’ clauses associated with the same FLWOR expression should all be on a separate line and vertically aligned. The expressions associated with each of these clauses should be on the same line as the clause or on the following line indented by the default indentation value (if the expression is lengthy and fairly complex). A FLWOR expression embedded within another FLWOR expression’s ‘return’ or ‘where’ clause should be indented by the default indentation value (on the following line). The FLWOR class of expressions should have one of the following forms:</para>
<programlisting>let $set := (3,2,1)
for $i in $set
return $i
let $set := (3,2,1)
for $i in $set
where $i > 1
order by $i
return $i
let $set1 := (3,2,1)
let $set2 := (1,2,3)
for $x in $set2
for $y in $set1
return $x + $y
let $value := “some value”
return $value
let $set1 := (3,2,1)
let $set2 := (1,2,3)
for $x in $set2
for $y in $set1
return
if ($x = $y) then true() else false()
let $set := (3,2,1)
for $x in $set
return
for $j in $set
return
if ($j = $x) then true() else false()</programlisting>
</section>
<section>
<title>Typeswitch expressions</title>
<para>The typeswitch expression is another more complex expression. A general rule to follow is that the ‘typeswitch’ (and expression) should be on a separate line. Each ‘case’ (and type) or ‘default’ should be on a separate line indented by the default indentation value. Lastly, each ‘return’ should be on a separate line indented by an additional default indentation value. The expression associated with the ‘return’ should be on the same line or on the following line indented by the default indentation value (if the expression is lengthy and fairly complex). The typeswitch class of expressions should have the following form:</para>
<programlisting>typeswitch($customer/billing-address)
case $a as element(*,USAddress)
return $a/state
case $a as element(*,CanadaAddress)
return $a/province
case $a as element(*,JapanAddress)
return $a/prefecture
default
return “unknown”</programlisting>
</section>
</section>
<section>
<title>White Space</title>
<section>
<title>Blank Lines</title>
<para>Blank lines improve readability by setting off sections of XQuery that are logically related.</para>
<para>Two blank lines should always be used in the following circumstances:</para>
<itemizedlist>
<listitem>
<para>Between the main sections defined in 2.1 and 2.2</para>
</listitem>
<listitem>
<para>Between the groups of like items defined in a prolog for a module.</para>
</listitem>
</itemizedlist>
<para>One blank line should always be used in the following circumstances:</para>
<itemizedlist>
<listitem>
<para>Between items declared in a prolog (i.e. functions, namespace declarations, etc.)</para>
</listitem>
<listitem>
<para>Between the local variables in a function and subsequent statements</para>
</listitem>
<listitem>
<para>Before a block (see section 4.1.1) or single-line (see section 4.1.2) comment</para>
</listitem>
<listitem>
<para>Between logical sections inside a function to improve readability</para>
</listitem>
</itemizedlist>
<programlisting language="xquery">xquery version "1.0";
(: Beginning Comments :)
module namespace math="http://xqdoc.org/sample-math-lib";
declare default function namespace "http://www.w3.org/2005/xpath-functions";
declare default element namespace "http://www.w3.org/1999/xhtml";
declare function some-function($operand1 as xs:integer,
$operand2 as xs:integer)
as xs:integer
{
…
};</programlisting>
</section>
<section>
<title>Blank Spaces</title>
<para>Blank spaces should be used in the following circumstances:</para>
<itemizedlist>
<listitem>
<para>A keyword followed by a parenthesis should be separated by a space. Example:</para>
<programlisting>if (true()) then
...
else
()</programlisting>
</listitem>
</itemizedlist>
<para>Note that a blank space should not be used between a function name and its opening parenthesis. This helps to distinguish keywords from function invocations</para>
<itemizedlist>
<listitem>
<para>A blank space should appear after commas in argument lists.</para>
<programlisting>some-function(parm1, parm2, parm3)</programlisting>
</listitem>
<listitem>
<para>A blank space should appear after commas in sequence lists.</para>
<programlisting>let $a := (item1, item2, item3)</programlisting>
</listitem>
<listitem>
<para>All arithmetic operators should be separated from their operands by spaces.</para>
<programlisting>let $result := 1 + 2</programlisting>
</listitem>
</itemizedlist>
</section>
</section>
<section>
<title>Naming Conventions</title>
<para></para>
<informaltable
frame="none"
rowsep="0"
colsep="0">
<tgroup
cols="3"
align="left">
<colspec
colname="c1"
colnum="1"
colwidth="1*"/>
<colspec
colname="c2"
colnum="2"
colwidth="1*"/>
<colspec
colname="c3"
colnum="3"
colwidth="1*"/>
<thead>
<row>
<entry>Identifier Type</entry>
<entry>Rules for Naming</entry>
<entry>Example</entry>
</row>
</thead>
<tbody>
<row>
<entry>
<para>Global Variable (not requiring run time evaluation)</para>
</entry>
<entry>
<para>Should be uppercase with hyphens separating each word</para>
</entry>
<entry>
<programlisting>$FAILURE-CODE
$LIST-OF-URIS</programlisting>
</entry>
</row>
<row>
<entry>
<para>Global Variable (requiring run time evaluation)</para>
</entry>
<entry>
<para>Should be lowercase with hyphens separating each word</para>
</entry>
<entry>
<programlisting>$country-list</programlisting>
</entry>
</row>
<row>
<entry>
<para>Local Variable</para>
</entry>
<entry>
<para>Should be mixed camel case with a lower case first letter</para>
</entry>
<entry>
<programlisting>$city
$intermediateResult</programlisting>
</entry>
</row>
<row>
<entry>
<para>Function Name</para>
</entry>
<entry>
<para>Should be verbs in lower case, with hyphens separating each word</para>
</entry>
<entry>
<programlisting>produce-report()
calculate-result()</programlisting>
</entry>
</row>
<row>
<entry>
<para>Namespace Prefix</para>
</entry>
<entry>
<para>Should be lower case alpha characters and less than 8 characters in length.</para>
</entry>
<entry>
<programlisting>display
lib</programlisting>
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</section>
<section>
<title>RESTful Services</title>
<para><emphasis
role="bold">OpenAPI Specification</emphasis> (formerly Swagger Specification) is an API description format for REST APIs. An OpenAPI file allows you to describe your entire API, including:</para>
<itemizedlist>
<listitem>
<para>Available endpoints (<code>/users</code>) and operations on each endpoint (<code>GET /users</code>, <code>POST /users</code>, etc.)</para>
</listitem>
<listitem>
<para>Operation parameters Input and output for each operation</para>
</listitem>
<listitem>
<para>Authentication methods</para>
</listitem>
<listitem>
<para>Contact information, license, terms of use and other information.</para>
</listitem>
</itemizedlist>
<para>API specifications can be written in YAML or JSON. The format is easy to learn and readable to both humans and machines. With xqDoc, it is possible to generate the JSON format of the specification. Extracting the OpenAPI JSON file (format defined as specification 3.0.2 <link
xlink:href="https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md"></link>) from the extracted xqDoc of the codebase will keep the REST API descriptions current to the existing codebase. </para>
<note>
<para>This will save time and money by not requiring a separate task to update the API description and risk it becoming out of date.</para>
</note>
<section>
<title>RestXQ</title>
<para>Adam Retter introduced the idea of creating RESTful Services in XQuery with Annotations at XML Prague 2012 and then with Christian Grün managed to flesh out the <link
xlink:href="http://exquery.github.io/exquery/exquery-restxq-specification/restxq-1.0-specification.html">RESTXQ Specification</link>.</para>
<para>RestXQ supports the following HTTP Methods:<code>GET, HEAD, POST, PUT, DELETE, OPTIONS</code> and <code>PATCH</code></para>
<section>
<title>OpenAPI Mapping</title>
<para>Find all xqDoc functions <emphasis>xqdoc:function</emphasis> that contain the <emphasis><emphasis
role="bold">%rest:path</emphasis></emphasis> RestXQ annotation. The functions are grouped by the identical path annotation.</para>
<para></para>
<section>
<title>Parameters</title>
<para></para>
<note>
<para>Discussion point! Should there be custom tags or function annotations defining the schema and showing examples for the parameterrs and the return type?</para>
</note>
<para></para>
<table
frame="none"
rowsep="1"
colsep="1">
<title>Data type mapping</title>
<tgroup
cols="2"
align="left">
<colspec
colname="c1"
colnum="1"
colwidth="1*"/>
<colspec
colname="c2"
colnum="2"
colwidth="1*"/>
<thead>
<row>