File: xquery

package info (click to toggle)
ruby-rouge 4.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,836 kB
  • sloc: ruby: 38,168; sed: 2,071; perl: 152; makefile: 8
file content (248 lines) | stat: -rw-r--r-- 8,238 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
(:
    The following samples have been compiled from examples from the XQuery language specification.
    The following notice is to comply with the W3C Software and Document Notice and License:

    This software or document includes material copied from or derived from "XQuery 3.1: An XML Query
    Language", https://www.w3.org/TR/xquery-31/. © 2017 W3C® (MIT, ERCIM, Keio, Beihang).

    This software or document includes material copied from or derived from "XML Path Language (XPath)
    3.1" https://www.w3.org/TR/xpath-31/.
    Copyright © 2017 W3C® (MIT, ERCIM, Keio, Beihang).

    (:
        W3C Software and Document Notice and License

        This work is being provided by the copyright holders under the following
        license.

        License
        By obtaining and/or copying this work, you (the licensee) agree that you have
        read, understood, and will comply with the following terms and conditions.

        Permission to copy, modify, and distribute this work, with or without
        modification, for any purpose and without fee or royalty is hereby granted,
        provided that you include the following on ALL copies of the work or portions
        thereof, including modifications:

            1. The full text of this NOTICE in a location viewable to users of the
               redistributed or derivative work.
            2. Any pre-existing intellectual property disclaimers, notices, or terms and
               conditions. If none exist, the W3C Software and Document Short Notice
               should be included.
            3. Notice of any changes or modifications, through a copyright statement on
               the new code or document such as "This software or document includes
               material copied from or derived from [title and URI of the W3C document].
               Copyright © [YEAR] W3C® (MIT, ERCIM, Keio, Beihang)."

        Disclaimers
        THIS WORK IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE NO REPRESENTATIONS OR
        WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO, WARRANTIES OF
        MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THE
        SOFTWARE OR DOCUMENT WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS,
        TRADEMARKS OR OTHER RIGHTS.

        COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL OR
        CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE OR DOCUMENT.
        The name and trademarks of copyright holders may NOT be used in advertising or
        publicity pertaining to the work without specific, written prior permission.

        Title to copyright in this work will at all times remain with copyright holders.
    :)
:)

(: 1. Prologs :)
xquery version "3.1" encoding "utf-8";
module namespace gis = "http://example.org/gis-functions";
declare boundary-space preserve;
declare default collation "http://example.org/languages/Icelandic";
declare base-uri "http://example.org";
declare decimal-format local:de decimal-separator="," grouping-separator=".";
declare decimal-format local:en decimal-separator="." grouping-separator=",";
import schema namespace soap="http://www.w3.org/2003/05/soap-envelope" at "http://www.w3.org/2003/05/soap-envelope/";
declare option exq:strip-comments "true";

(: 2. Node constructors :)
<example size="As big as {$hat/@size}">
    <!-- XML comment -->
    <p> Here is a query. </p>
    <eg>{{ $b/title }}</eg>
    <p> Here is the result of the query. </p>
    <eg>{ $b/title }</eg>
    <p> Here is a another example </p>
    <eg> Hello&#x20;{"world"}</eg>
    <fact>I saw <howmany>{5 + 3}</howmany> cats.</fact>
</example>


element book {
   attribute isbn {"isbn-0060229357" },
   element title { "Harold and the Purple Crayon"},
   element author {
      element first { "Crockett" },
      element last {"Johnson" }
   }
}

let $e := <length units="inches">{5}</length>
return element {fn:node-name($e)} {$e/@*, 2 * fn:data($e)}

let $target := "audio-output",
    $content := "beep"
return processing-instruction {$target} {$content}


(: 3. String constructors :)
declare function local:prize-message($a) as xs:string {
    ``[Hello `{$a?name}`
    You have just won `{$a?value}` dollars!
    `{
       if ($a?in_ca)
       then ``[Well, `{$a?taxed_value}` dollars, after taxes.]``
       else ""
    }`]``
};

(: 4. FLWOR :)
for $x at $i in $inputvalues
where $i mod 100 = 0
return $x

for tumbling window $w in (2, 4, 6, 8, 10)
    start $s at $spos previous $sprev next $snext when true()
    end $e at $epos previous $eprev next $enext when true()

let $x := 64000
for $c in //customer
let $d := $c/department
where $c/salary > $x
group by $d
return
 <department name="{$d}">
  Number of employees earning more than ${distinct-values($x)} is {count($c)}
 </department>

(: 5. Switch :)
switch ($animal)
   case "Cow" return "Moo"
   case "Cat" return "Meow"
   case "Duck" return "Quack"
   default return "What's that odd noise?"

(: 6. Try/catch :)
try {
    $x cast as xs:integer
}
catch err:FORG0001 | err:XPTY0004 {
    0
}

(: 7. Type expressions: instance of, typeswitch, cast, castable, ... :)
5 instance of xs:decimal

typeswitch($customer/billing-address)
   case $a as element(*, USAddress)
     return $a/state
   case $a as element(*, CanadaAddress)
     return $a/province
   default
     return "unknown"

(: 8. Validate :)
validate strict {
    <x>12</x>
}

(: 9. Pragma :)
declare namespace exq = "http://example.org/XQueryImplementation";
for $x in
    (# exq:distinct //city by @country #)
    { //city[not(@country = preceding::city/@country)] }
return f:show-city($x)

(: 10. Declarations and annotations :)
declare %eg:volatile variable $time as xs:time external;
declare context item as item() external;
declare
   %java:method("java.lang.StrictMath.copySign")
   function smath:copySign($magnitude, $sign)
   external;


(:
   The XQuery specification states:
      > XQuery 3.1 is an extension of XPath 3.1. In general, any expression that
      > is syntactically valid and executes successfully in both XPath 3.1 and
      > XQuery 3.1 will return the same result in both languages.

   The following XPath examples have been compiled from the XPath language specification.
:)

(: XPATH 2.0 :)
(:   1. Arithmetic :)
($x div $y) + xs:decimal($z) |
(1 to 100)[. mod 5 eq 0] |
xs:decimal($floatvalue * 0.2E-5)

(:   2. Sequences :)
(10, (1, 2), (), (3, 4))
(1, 2) = (2, 3) and (2, 3) = (3, 4) and (1, 2) = (3, 4) and (1, 2) != (2, 3) |
(salary, bonus)  |
($price, $price) |

(:   3. Path selection :)
child::chapter[2] |
descendant::toy[attribute::color = "red"] |
child::employee[secretary][assistant] |
/books/book[isbn="1558604820"] is /books/book[call="QA76.9 C3845"] |

(:   4. Functions :)
fn:error(xs:QName("app:err057"), "Unexpected value", fn:string($v)) |

(:   5. Control flow :)
if ($x castable as hatsize)
   then $x cast as hatsize
   else if ($x castable as IQ)
       then $x cast as IQ
       else $x cast as xs:string |
$N[if (@x castable as xs:date)
   then xs:date(@x) gt xs:date("2000-01-01")
   else false()] |

(:   6. Looping :)
for $a in fn:distinct-values(book/author) return (book/author[. = $a][1], book[author = $a]/title) |
every $part in /parts/part satisfies $part/@discounted |
some $emp in /emps/employee satisfies ($emp/bonus > 0.25 * $emp/salary) |

(:   7. Type casts :)
$myaddress treat as element(*, USAddress) |
(fn:root(self::node()) treat as document-node())/

(:   8. Functions and let :)
let $f := function($a) { starts-with($a, "E") }
    return local:filter(("Ethel", "Enid", "Gertrude"), $f) |

collection()/(let $a := . return function() { $a }) |

let $x := doc('a.xml')/*,
    $y := $x//*,
    $z := f(3, ?)
return $y[@value gt $x/@min] |

(:   9. Maps :)
let $m := map {
  "Monday" : true(),
  "Wednesday" : true(),
  "Friday" : true(),
  "Saturday" : false(),
  "Sunday" : false()
},
$days := ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
return fn:filter($days, $m) |

(:   10. Arrays :)
let a := array { "licorice", "ginger" }(1),
    b := [ 1, 2, 5, 7 ](4)
return a |

(:   11. Arrow operator :)
$string => upper-case() => normalize-unicode() => tokenize("\s+") |