-
Notifications
You must be signed in to change notification settings - Fork 0
/
Unit 16-Lab-Exercices.xqy
92 lines (69 loc) · 2.52 KB
/
Unit 16-Lab-Exercices.xqy
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
(:
(: create some sample docs :)
if (xdmp:database-name(xdmp:database()) = "Documents")
then
(
xdmp:document-insert("/testA.xml", <doc><node>hello</node></doc>),
xdmp:document-insert("/testB.xml", <doc><node>goodbye</node></doc>)
)
else
"please select the documents database"
xquery version "1.0-ml";
if (xdmp:database-name(xdmp:database()) = "Documents")
then
(
for $doc in /doc
return
xdmp:node-replace($doc/node, <node>123</node>)
)
else
"please select the documents database"
xdmp:document-insert("song1.xml",
<top-song>
<title>Money for Nothing</title>
<artist>Dire Straits</artist>
<weeks>
<week>1985-09-21</week> <week>1985-09-28</week> <week>1985-10-05</week>
</weeks>
<album>Brothers in Arms</album>
</top-song>)
xdmp:node-replace(fn:doc("song1.xml")/top-song/title,
<title>Trouble for Nothing</title>)
xdmp:node-replace(fn:doc("song1.xml")/top-song/title,
<title>Trouble for Nothing</title>)
fn:doc("/songs/Jay-Z-and-Alicia-Keys+Empire-State-of-Mind.xml")
declare namespace ts="http://marklogic.com/MLU/top-songs";
xdmp:node-insert-child(
fn:doc("/songs/Jay-Z-and-Alicia-Keys+Empire-State-of-Mind.xml")//ts:genres,
<ts:genre>rap</ts:genre>)
declare namespace ts="http://marklogic.com/MLU/top-songs";
xdmp:node-insert-before(
fn:doc("/songs/Jay-Z-and-Alicia-Keys+Empire-State-of-Mind.xml")//ts:genres/ts:genre[. eq "Hip hop"],
<ts:genre>pop</ts:genre>)
(: create a document :)
xdmp:document-insert("/example.xml",
<a/>);
(: insert a child of a :)
xdmp:node-insert-child(doc("/example.xml")/a,
<b>bbb</b>);
(: insert an attribute as child of a :)
xdmp:node-insert-child(doc("/example.xml")/a,
attribute b { "bbb" });
(: look at the new document :)
fn:doc("/example.xml")
declare namespace ts="http://marklogic.com/MLU/top-songs";
fn:(fn:doc("/songs/Stevie-Wonder+I-Just-Called-to-Say-I-Love-You.xml")//ts:weeks/ts:week)
declare namespace ts="http://marklogic.com/MLU/top-songs";
let $weeks := fn:doc("/songs/Stevie-Wonder+I-Just-Called-to-Say-I-Love-You.xml")//ts:weeks
let $count := fn:count($weeks/ts:week)
return xdmp:node-insert-child($weeks, attribute count {$count})
declare namespace ts="http://marklogic.com/MLU/top-songs";
for $weeks in //ts:weeks
let $count := fn:count($weeks/ts:week)
return
if($weeks/@count)
then xdmp:node-replace($weeks/@count, attribute count {$count})
else xdmp:node-insert-child($weeks, attribute count {$count})
:)
declare namespace ts="http://marklogic.com/MLU/top-songs";
doc("/songs/Stevie-Wonder+I-Just-Called-to-Say-I-Love-You.xml")//ts:weeks