Database Reference
In-Depth Information
Require:
s = query (fragment) to be translated with all information gathered in s.let, s.for, s.where,
and s.return
1:
String qs = new String(”
<
result
>
”)
2:
for all
variableName v, document d
∈
s.let
do
3:
qs += ”let $” + v + ”:=doc(” + d + ”)
4:
end for
5:
order all c's in s.for according to their hierarchical structure in the source document utilising
the information captured in the supermodel
6:
for all
element c
∈
ordered s.for starting from top
do
7:
if
c instance of root element
then
8:
qs += ” for $” + c.name + ” in $” + v + ”/” + c.name
9:
else if
s.c instance of complex element
then
10:
qs += ” for $” + c.name + ” in $” + p.name + ”/” + c.name
11:
where p = parent complex element of c
12:
end if
13:
end for
14:
if
!s.where.isEmpty()
then
15:
qs += ” where ”
16:
for all
predicate p in s.where
do
17:
if
pofkind
simple element l1
op
simple element l2
then
18:
qs += ”$” + c1.name + ”/” + l1.name + op + ”$” + c2.name + ”/” + l2.name
19:
where c1 and c2 are the corresponding parent complex elements of simple elements l1
and l2, respectively
20:
then
21:
qs += ”$” + c.name + ”/” + l.name + op + constant
22:
where c is the corresponding parent complex element of simple element l
23:
end if
24:
if
s.where.hasNext()
then
25:
qs += ” AND ”
26:
end if
27:
end for
28:
end if
29:
qs += ”return
<
tuple
>
”
30:
for all
simple element l in s.return
do
31:
qs += ”
<
” + c.name + ”.” + l.name + ”
>
”
32:
qs += ” fn:data($” + c.name + ”/” + l.name + ”)”
33:
qs += ”
</
” + c.name + ”.” + l.name + ”
>
”
34:
where c is the corresponding parent complex element of simple element l
35:
end for
36:
qs += ”
<
/tuple
>
”
37:
qs +=”
<
/result
>
”
38:
return
queryString
else if
pofkind
simple element l
op
constant
Fig. 5.
toXQueryString(s)
XQuery Q
1
:
<result>
let $s := doc("...")
for $o in $s/country
where $o/population > 5000000
return
<tuple>
<o.name>{fn:data($o/name)}</o.name>
<o.code>{fn:data($o/code)}</o.code>
<o.capital>{fn:data($o/capital)}</o.capital>
</tuple>
</result>
XQuery Q
2
:
for $o in $s/country
for $c in $o/capital_city/city
where $o/code = $c/country
and $c/name = “Manchester”
for $o in $s/country
for $c in $o/other_city/city
where $o/code = $c/country
and $c/name = “Manchester”
XQuery Q
3
:
for $o in $s/country
for $c in $o/capital_city/city
where $o/code = $c/country
and $c/capital = $c/name
for $o in $s/country
for $c in $o/other_city/city
where $o/code = $c/country
and $c/capital = $c/name
Fig. 6.
(Partial) XQueries corresponding to
Q
1
,
Q
2
and
Q
3