forked from MadhushaPrasad/Oracle-SQL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml_database.sql
354 lines (261 loc) · 8.42 KB
/
xml_database.sql
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
-- start the document with a declation,srrounded by
-- <?xml version="1.0" encoding="UTF-8"?>.this is the first line of the document.
-- tags are case sensitive
-- xml database have two types
-- Xml doc
-- Xml Schema
-- xml schema language check our document correct or NOT
-- it can be used to validate xml document
-- types of xml schema
-- DTD
-- XML Schema
-- Relax NG
------------------------------------------------------------------------
-- insertion of xml data and Xml data type
CREATE TABLE adminUsers(
id int NOT NULL PRIMARY KEY,
xDoc Xml NOT NULL
);
INSERT INTO adminUsers VALUES(5,
'<catalog>
<product dept="WN">
<number>555</number>
<name language="en">Flece Pullover</name>
</product>
<doc>
<title>hello</title>
</doc>
</catalog>');
SELECT * FROM adminUsers;
INSERT INTO adminUsers VALUES(3,
'
<doc>
<doc id="123">
<section>
<section num="1">
<title>XML Schema</title>
</section>
<section num="2">
<title>XML Schema</title>
</section>
<section num="3">
<title>XML Schema</title>
</section>
</section>
</doc>
</doc>
'
);
-------------------------------------------------------------------
-- path expression
-- Simple path descriptors are sequences of
-- tags separated by slashes (/).
-- If the descriptor begins with /, then the path
-- starts at the root and has those tags, in order.
-- If the descriptor begins with //, then the path
-- can start anywhere.
-- Value of a Path Descriptor
-- Each path descriptor, applied to a
-- document, has a value that is a sequence of
-- elements.
-- An element is an atomic value or a node.
-- A node is matching tags and everything in
-- between.
-- Example 01: /BARS/BAR/PRICE
<BARS>
<BAR name = “JoesBar”>
<PRICE theBeer = “Bud”>2.50</PRICE>
<PRICE theBeer = “Miller”>3.00</PRICE>
</BAR>
<BEER name = “Bud” soldBy = “JoesBar SuesBar/>
</BARS>
-- /BARS/BAR/PRICE describes the
-- set with these two PRICE elements
-- as well as the PRICE elements for
-- any other bars.
-- Example 02: //PRICE
<BARS>
<BAR name = “JoesBar”>
<PRICE theBeer = “Bud”>2.50</PRICE>
<PRICE theBeer = “Miller”>3.00</PRICE>
</BAR>
<BEER name = “Bud” soldBy = “JoesBar SuesBar …”/>
</BARS>
-- //PRICE describes the PRICE
-- Elements to appear within
-- the document.
-- Wild Card *
-- A star (*) in place of a tag represents any one
-- tag.
-- Example: /*/*/PRICE represents all price
-- objects at the third level of nesting.
-------------------------------------------------------------------------
-- Example 01: /BARS/*
<BARS>
<BAR name = “JoesBar”>
<PRICE theBeer = “Bud”>2.50</PRICE>
<PRICE theBeer = “Miller”>3.00</PRICE>
</BAR>
<BEER name = “Bud” soldBy = “JoesBar SuesBar”/>
</BARS>
-- /BARS/* captures all BAR
-- and BEER elements, such
-- as these.
--------------------------------------------------------------------
--
-- In XPath, we refer to attributes by prepending ' @ ' to their
-- Attributes of a tag may appear in paths as if
-- they were nested within that tag.
-- Example: /BARS/*/@name
<BARS>
<BAR name = “JoesBar”>
<PRICE theBeer = “Bud”>2.50</PRICE>
<PRICE theBeer = “Miller”>3.00</PRICE>
</BAR>
<BEER name = “Bud” soldBy = “JoesBar SuesBar”/>
</BARS>
-- /BARS/*/@name selects all
-- name attributes of immediate
-- subelements of the BARS element.
-------------------------------------------------------------------------------------------------
-- Axes
-- In general, path expressions allow us to
-- start at the root and execute steps to find a
-- sequence of nodes at each step.
-- At each step, we may follow any one of
-- several axes
/child::BAR[@name=“JoesBar"]
-- Axis -> child
-- Node test -> BAR
-- Predicate -> [@name=“JoesBar"]
-- 1.The axis (Optional)
-- -Direction to navigate
-- 2.The node test
-- -The node of interest by name
-- 3.Predicate
-- -The criteria used to filter nodes
-- /BARS/BEER is really shorter way for
-- /BARS/child::BEER
-- @ is really shorthand for the attribute:: axis.
-- Thus, /BARS/BEER[@name = “Bud” ] is
-- shorthand for
-- /BARS/BEER[attribute::name = “Bud”]
-- More Axes
-- Some other useful axes are:
-- parent:: = parent(s) of the current node(s).
-- descendant or self:: = the current node(s)
-- and all descendants.
-- Note: // is really shorthand for this axis.
-- ancestor::, ancestor or self, etc.
-- the default axis is child:: go to all the
-- children of the current set of nodes.
------------------------------------------------------------------------------------------------
-- Predicates
-- A condition inside […] may follow a tag.
-- If so, then only paths that have that tag and
-- also satisfy the condition are included in the
-- result of a path expression.
-- Example: /using Query() method
-- simple path expression
SELECT id,xDoc.query('/catalog/product/'); -- -> gives all products
SELECT id,xDoc.query('//product'); -- -> go to the root and give all products since down of root products are there
-- get second data of SELECT BARS inside the bars
SELECT id,xDoc.query('/BARS/BAR[2]');
-------------------------------------------------------------------------------------------------------
-- Example: Predicates and Returned
-- Elements
-- Using number in a predicate does not mean that number
-- elements are returned:
-- all price elements whose grater than 2.50
SELECT id,xDoc.query('/BARS/BAR/PRICE[.>2.50]');
-- all price elements whose less than 2.50
SELECT id,xDoc.query('/BARS/BAR/PRICE[.< 2.50]');
SELECT id,xDoc.query('/BARS/BAR[. lt 2.00]') FROM adminUsers;
-- A period (".") is used to indicate the context item itself
/*same code in the xql Database*/
SELECT id,xDoc.query('/*/doc[@id="123"]') FROM adminUsers;
SELECT id,xDoc.query('/*/child::doc[attribute::id="123"]') FROM adminUsers;
/*same code in the xql Database*/
SELECT id,xDoc.query('//doc[@id="123"]') FROM adminUsers;
SELECT id,xDoc.query('descendant-or-self::doc[attribute::id="123"]') FROM adminUsers;
-------------------------------------------------------------------------------------------------------
-- XQuery
-- XQuery extends XPath to a query language that has
-- power similar to SQL.
-- Variables are identified by a name preceded by a $
for $prod in
(doc("cat.xml")//product)
return $prod/number
-- Literal values can be expressed as:
-- STRING (in single oe double quotes)
-- - doc("cat.xml")//product/@dept = "WMN"
-- NUMERIC (no quotes)
-- - doc("cat.xml")//product/@price = 10
-- - doc("cat.xml")//item/@quntity > 1;
------------------------------------------------------------------
-- Comments
-- Xquery comments
-- - Delimited by (: and :)
-- - Anywhere insignificant whitespace is allowed
-- - Do not appear in the results
-- Xml comments
-- - May appear in the results
-- -XML-like syntax ( <!-- This element -->)
------------------------------------------------------------------------
-- Clauses of a FLWOR Expression
-- -- for clause
-- iteratively binds the $prod varible to each item returned by a
-- path expression
-- -- let clause
-- binds a variable to a value
-- -- where clause
-- filters the items returned by the for clause
-- -- return clause
-- specifies the value to be returned for each item
for $prod in
(doc("cat.xml")//product)
let $prodDept := $prod/@dept
Where $prodDept = "ACC" or $prodDept = "WMN"
return $prod/name
SELECT xDoc.query(
'for $prod in //product
let $a:= $prod//number
return $a
'
) FROM adminUsers WHERE id=1;
SELECT xDoc.query(
'for $prod in //BARS
let $a:=$prod//PRICE
where $a > 2.50
return $a
'
) FROM adminUsers;
SELECT xDoc.query(
'for $prod in //BARS
let $a:=$prod//PRICE
where $a > 2.50
return (<item>{$a}</item>)
'
) FROM adminUsers;
SELECT xDoc.query(
'for $prod in //BARS
let $a:=$prod//PRICE
where $a > 2.50
return $a
'
) FROM adminUsers;
SELECT xDoc.query(
'for $prod in //BARS
let $a:=$prod//PRICE
where $a > 2.50
return (<item>{data($a)}</item>)
'
) FROM adminUsers;
SELECT xDoc.query(
'for $prod in //BARS
let $a:=$prod//PRICE
return if ($a > 2.50)
then <book>{data($a)}</book>
else <paper>{data($a)}</paper>
'
) FROM adminUsers;