9. XML Query Languages III – XQuery
Transcript of 9. XML Query Languages III – XQuery
XML Databases9 XML Query Languages III ndash
Silke EcksteinAndreas KupferInstitut fuumlr InformationssystemeTechnische Universitaumlt Braunschweighttpwwwifiscstu-bsde
9 XML Query Languages III ndashXQuery
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 2
bull XQuery
ndash XQuery is a truly declarative language specifically designed for the purpose of querying XML data
ndash As such XML assumes the role that SQL occupies in the context of relational databases
ndash XQuery exhibits properties known from database (DB) languages as
91 Introduction
ndash XQuery exhibits properties known from database (DB) languages as well as from (functional) programming (PL) languages
ndash The language is designed and formally specified by the W3C XQuery Working Group (httpwwww3orgXMLXQuery)
bull The first working draft documents date back to February 2001 TheXQueryspecification has become a W3C Recommendation in January 2007
bull Members of the working group include Dana FlorescuDB Ioana ManolescuDB Phil WadlerPL Mary FernandezDB+PL Don ChamberlinDB (the father of SQL) Jeacuterocircme SimeonDB Michael RysDB and many others
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 3[Gru08]
bull 12 Programming Language 12 Query Languagendash XQuery is a hybrid exhibiting features commonly found in programming as well as database query languages
bull Programming language features
explicit iteration and variable bindings (
91 Introduction
ndash explicit iteration and variable bindings (forinletin)
ndash recursive user-defined functions
ndash regular expressions strong [static] typing
ndash ordered sequences (much like lists or arrays)
bull Database query language features
ndash filtering
ndash grouping joins expressed via nested for loops
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 4[Gru08]
bull History of XQuery
91 Introduction
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 5[Gru08]
bull XQuery-Preliminariesndash Remember XPath is part of XQuery (as a sublanguage)ndash Some constructs that have not previously been discussed yet are not within the core of our focus on XQuery include
bull Comparisons any XQuery expression evaluates to a
92 Preliminaries
bull Comparisons any XQuery expression evaluates to a sequence of items Consequently many XQuery concepts are prepared to accept sequences (as opposed to single items)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 6[Gru08]
General comparisonsThe general comparison withᶿe1 e2
yields true() if any of the items in the sequences e12 compare
true (existential semantics)
= = lt lt= gt=gtᶿ isin
92 Preliminaries
bull Comparisons General comparison examples
(123) gt (245) rArrrArrrArrrArr true()
(123) = 1 rArrrArrrArrrArr true()
() = 0 rArrrArrrArrrArr false()
2 lt= 1 rArrrArrrArrrArr false()
(123) = 3 rArrrArrrArrrArr true()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 7[Gru08]
ndash The six value comparison operators eq ne lt le ge gtcompare single items by value (atomization)
(123) = 3 rArrrArrrArrrArr true()
(12) = (12) rArrrArrrArrrArr true()
not((12) = (12)) rArrrArrrArrrArr false()
Value comparisons
2 gt 10 rArrrArrrArrrArr true()
ltxgt42ltxgt eq ltygt42ltygt rArrrArrrArrrArr true()
(01) eq 0 rArrrArrrArrrArr (type error)
bull More on comparisons
ndash Note The existential semantics of the general comparison operators may lead to unexpected behavior
92 Preliminaries
a) For an item-by-item comparison use deep-equal()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 8[Gru08]
Surprises
(123) = (13) rArrrArrrArrrArr true()a
(21) = 1 rArrrArrrArrrArr true() or (impl dependent)
bull Node comparisons
92 Preliminaries
based on identity and document order
e1 is e2 nodes e12 identical
e1 ltlt e2 node e1 before e2
e1 gtgt e2 node e1 after e2
ndash Node comparison examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 9[Gru08]
ltxgt42ltxgt eq ltxgt42ltxgt rArrrArrrArrrArr true()
ltxgt42ltxgt is ltxgt42ltxgt rArrrArrrArrrArr false()
root(e1) is root(e2) rArrrArrrArrrArr nodes e12 in same tree
let $a = ltxgtltygtltxgt
return $a ltlt $ayrArrrArrrArrrArr true()
bull Working with sequencesndash XQuery comes with an extensive library of builtinfunctions to perform common computations over sequences
bull Common sequence operations
92 Preliminaries
Function Example
count count((042)) rArrrArrrArrrArr 3
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 10[Gru08]
count count((042)) rArrrArrrArrrArr 3
max max((042)) rArrrArrrArrrArr 4
subsequence subsequence((1357)23) rArrrArrrArrrArr (357)
empty empty((042)) rArrrArrrArrrArr false()
exists exists((042)) rArrrArrrArrrArr true()
distinct-values distinct-values((4424)) rArrrArrrArrrArr (42)
to (1 to 10)[ mod 2 eq 1] rArrrArrrArrrArr (13579)
bull Arithmeticsndash Only a few words on arithmetics ndash XQuery meets the common expectation here Points to note1 Infix operators + - lowastlowastlowastlowast div idiv (integer division)2 operators first atomize their operands then perform
promotion to a common numeric type
92 Preliminaries
promotion to a common numeric type3 if at least one operand is () the result is ()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 11[Gru08]
Examples and pitfalls
ltxgt1ltxgt + 41 rArrrArrrArrrArr 420
() 42 rArrrArrrArrrArr ()
(12) - (23) rArrrArrrArrrArr (type error)
x-42 rArrrArrrArrrArr childx-42 (use x˽-˽42)
xy rArrrArrrArrrArr childxchildy (use x div y)
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)ndash Foor loop
ndash Variable bindings
9 XQuery
ndash Variable bindings
ndash Where clause
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 12
bull XQuery Iteration FLWORsndash Remember that XPath steps perform implicit iteration in cse evaluation of e is iterated with bound to each item in cs in turn
ndash XPath subexpressions aside iteration in XQuery is explicit via the FLWOR (flower) construct
93 Iteration (FLWORs)
explicit via the FLWOR (flower) constructbull The versatile FLWOR is used to express
ndash nested iteration
ndash joins between sequences (of nodes)
ndash groupings
ndash orderings beyond document order etc
bull In a sense FLWOR assumes the role of the SELECT-FROM-WHERE block in SQL
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 13[Gru08]
bull FLWOR Iteration via forin
ndash Explicit iteration
93 Iteration (FLWORs)
Explicit iteration is expressed using the forin construct a
for $v [at $p] in e1
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 14[Gru08]
for $v [at $p] in e1 return e2
If e1 evaluates to the sequence (x1xn) the loop body e2 is
evaluated n times with variable $v bound to each xi [and $pbound to i ] in order The results of these evaluations are concatenated to form a single sequence
a the construct at $p is optional
93 Iteration (FLWORs)
Iteration examples
for $x in (321)
return ($xlowastlowastlowastlowast)rArrrArrrArrrArr (3lowastlowastlowastlowast2lowastlowastlowastlowast1lowastlowastlowastlowast)
for $x in (321)
return $xlowastlowastlowastlowastrArrrArrrArrrArr (321lowastlowastlowastlowast)
for $x in (321)
return for $y in (ab) rArrrArrrArrrArr(3a3b
2a2b
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 15[Gru08]
return for $y in (ab)
return ($x$y)
rArrrArrrArrrArr 2a2b
1a1b)
FLWOR Abbreviations
for $v1 in e1return
for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1$v2 in e2
return e3
bull FLWOR Iteration via forin
93 Iteration (FLWORs)
Purpose of this query Qmax( for $i in csdescendant-or-self[not()]
return count($iancestor) )
A sample cs ldquoAnnotatedrdquo sample cs
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 16[Gru08]
A sample csa
b c
d
e g h
f
ldquoAnnotatedrdquo sample csa
b
e
c
d
g h
f
1
3 3 3
Answer
bull FLWOR Iteration via forin ndash Return every other item in sequence
93 Iteration (FLWORs)
These queries both return the items at odd positions in the inputsequence e
for $i in (1 to count(e))[ mod 2 eq 1]
bull Remember ebv(0) = false()ebv effective boolean value
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 17[Gru08]
for $i in (1 to count(e))[ mod 2 eq 1]
return e[$i]
for $i at $p in e
return if ($p mod 2)
then e[$p]
else ()
bull FLWOR Variable Binding via let =
ndash Note that in the examples on the last slide expression e is re-evaluated count(e) 2 times although e is constant in the loop
93 Iteration (FLWORs)
Variable bindingsThe result of evaluating an expression e may be bound to a variable $v
bull for and let clauses may be freely intermixed
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 18[Gru08]
Variable bindingsThe result of evaluating an expression e1 may be bound to a variable $vvia let
let $v = e1return e2
evaluates e2 with free occurrences of $v replaced by e1
bull FLWOR Variable Binding via letbull bull bull =
93 Iteration (FLWORs)
Iteration vs variable binding
for $x in (321)
return ($x)
rArrrArrrArrrArr (321
)
let $x = (321) rArrrArrrArrrArr (321)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 19[Gru08]
let $x = (321)
return ($x)
rArrrArrrArrrArr (321)
Every other item revisited (flip back two slides)The following hoists the constant e out of the loop body
let $seq = ereturn for $i at $p in $seq
return if ($p mod 2)
then $seq[$p]
else ()
bull Adding a where clause
ndash Inside loop bodies the idiom if (p) then e else ()
is so common that FLWOR comes with a SQL-like where clause to address this
93 Iteration (FLWORs)
A where clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 20[Gru08]
A where clauseIf ebv(p) evaluates to false() under the current variable bindings the
current iteration does not contribute to the result
for $v in e1where p
return e2
equiv
for $v in e1return if (p)
then e2else ()
bull Explicit vs implicit iterationndash XPath implicit iteration
ndash Equivalent nested FLWOR blocks
93 Iteration (FLWORs)
a[b = foo]c[2]d[e = 42]
for $a in a
ndash NB Unlike the XPath step operator for does not change the context item
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 21[Gru08]
for $a in a
where $ab = foo
return for $c at $p in $ac
where $p = 2
return for $d in $cd
where $de = 42
return $d
bull FLWOR Reorder iteration result via order byndash In a FLWOR block for $v in e1 return e2 the order of e1determines the order of the resulting sequence
93 Iteration (FLWORs)
Reordering via order by
In the FLWOR block
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 22[Gru08]
In the FLWOR block
the value (atomization) of e3 determines the order in which the bindingsof $v are used to evaluate e2
for $v in e1order by e3 [ascendingdescending][empty greatestleast]return e2
bull FLWOR Reordering examples
93 Iteration (FLWORs)
An order by no-op reordering by sequence order
for $x at $p in (53142)
order by $p
return $x
rArrrArrrArrrArr (53142)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 23[Gru08]
All bound variables in scope in order by
for $x at $p in (53142)
order by $p + $x
return $x
rArrrArrrArrrArr (13524)
Reordering as in SQLs ORDER BY
for $x at $p in (53142)
order by $x
return $x
rArrrArrrArrrArr (12345)
bull FLWOR Reordering examplesndash Value-based reordering of an XPath step result
93 Iteration (FLWORs)
This query reorders the result of the XPath location step descendantb
based on (string) value Which result is to be expectedlet $a = ltagt
ltb id=0gt42ltbgt
ltb id=1gt5ltbgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 24[Gru08]
ltb id=1gt5ltbgt
ltb id=2gt
ltb id=3gt3ltbgt
ltb id=4gt1ltbgt
ltagt
for $b in $adescendantb
order by $btext() empty greatest
return $bid
Answer
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 25
bull FLWOR semantics tuple spacendash In the W3C XQuery specification the interaction of the five clauses of a FLWOR (for-let-where-order by-return) block is formally explained by means of a tuple space
94 FLOWR Semantics
means of a tuple spacebull Size of tuple space equiv number of iterations performed by FLWOR block
bull The fields of the tuples represent for each iteration
1 forlet variable bindings
2 the outcome of the where clause
3 the value of the reordering criterion and
4 the value returned by the return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 26[Gru08]
bull FLWOR semantics tuple space (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y where order by return
10 1 100 false 4 (110)
1 Complete tuple space
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 27[Gru08]
return ($p$x) 10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (2)2 Filtering where clause ($y lt= 42)
94 FLOWR Semantics
$x $p $y where order by return
10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 28[Gru08]
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (3)3 Reordering order by clause
94 FLOWR Semantics
$x $p $y where order by return
1 10 1 true -5 (101)
2 9 4 true -4 (92)
3 8 9 true -3 (83)
4 To emit the final result scan the tuple space in the order specified by the order by column and concatenate the return column entries
(1019283746556)
ndash Observation some values have been computed but never used
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 29[Gru08]
3 8 9 true -3 (83)
4 7 16 true -2 (74)
5 6 25 true -1 (65)
6 5 36 true 0 (56)
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 2
bull XQuery
ndash XQuery is a truly declarative language specifically designed for the purpose of querying XML data
ndash As such XML assumes the role that SQL occupies in the context of relational databases
ndash XQuery exhibits properties known from database (DB) languages as
91 Introduction
ndash XQuery exhibits properties known from database (DB) languages as well as from (functional) programming (PL) languages
ndash The language is designed and formally specified by the W3C XQuery Working Group (httpwwww3orgXMLXQuery)
bull The first working draft documents date back to February 2001 TheXQueryspecification has become a W3C Recommendation in January 2007
bull Members of the working group include Dana FlorescuDB Ioana ManolescuDB Phil WadlerPL Mary FernandezDB+PL Don ChamberlinDB (the father of SQL) Jeacuterocircme SimeonDB Michael RysDB and many others
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 3[Gru08]
bull 12 Programming Language 12 Query Languagendash XQuery is a hybrid exhibiting features commonly found in programming as well as database query languages
bull Programming language features
explicit iteration and variable bindings (
91 Introduction
ndash explicit iteration and variable bindings (forinletin)
ndash recursive user-defined functions
ndash regular expressions strong [static] typing
ndash ordered sequences (much like lists or arrays)
bull Database query language features
ndash filtering
ndash grouping joins expressed via nested for loops
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 4[Gru08]
bull History of XQuery
91 Introduction
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 5[Gru08]
bull XQuery-Preliminariesndash Remember XPath is part of XQuery (as a sublanguage)ndash Some constructs that have not previously been discussed yet are not within the core of our focus on XQuery include
bull Comparisons any XQuery expression evaluates to a
92 Preliminaries
bull Comparisons any XQuery expression evaluates to a sequence of items Consequently many XQuery concepts are prepared to accept sequences (as opposed to single items)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 6[Gru08]
General comparisonsThe general comparison withᶿe1 e2
yields true() if any of the items in the sequences e12 compare
true (existential semantics)
= = lt lt= gt=gtᶿ isin
92 Preliminaries
bull Comparisons General comparison examples
(123) gt (245) rArrrArrrArrrArr true()
(123) = 1 rArrrArrrArrrArr true()
() = 0 rArrrArrrArrrArr false()
2 lt= 1 rArrrArrrArrrArr false()
(123) = 3 rArrrArrrArrrArr true()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 7[Gru08]
ndash The six value comparison operators eq ne lt le ge gtcompare single items by value (atomization)
(123) = 3 rArrrArrrArrrArr true()
(12) = (12) rArrrArrrArrrArr true()
not((12) = (12)) rArrrArrrArrrArr false()
Value comparisons
2 gt 10 rArrrArrrArrrArr true()
ltxgt42ltxgt eq ltygt42ltygt rArrrArrrArrrArr true()
(01) eq 0 rArrrArrrArrrArr (type error)
bull More on comparisons
ndash Note The existential semantics of the general comparison operators may lead to unexpected behavior
92 Preliminaries
a) For an item-by-item comparison use deep-equal()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 8[Gru08]
Surprises
(123) = (13) rArrrArrrArrrArr true()a
(21) = 1 rArrrArrrArrrArr true() or (impl dependent)
bull Node comparisons
92 Preliminaries
based on identity and document order
e1 is e2 nodes e12 identical
e1 ltlt e2 node e1 before e2
e1 gtgt e2 node e1 after e2
ndash Node comparison examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 9[Gru08]
ltxgt42ltxgt eq ltxgt42ltxgt rArrrArrrArrrArr true()
ltxgt42ltxgt is ltxgt42ltxgt rArrrArrrArrrArr false()
root(e1) is root(e2) rArrrArrrArrrArr nodes e12 in same tree
let $a = ltxgtltygtltxgt
return $a ltlt $ayrArrrArrrArrrArr true()
bull Working with sequencesndash XQuery comes with an extensive library of builtinfunctions to perform common computations over sequences
bull Common sequence operations
92 Preliminaries
Function Example
count count((042)) rArrrArrrArrrArr 3
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 10[Gru08]
count count((042)) rArrrArrrArrrArr 3
max max((042)) rArrrArrrArrrArr 4
subsequence subsequence((1357)23) rArrrArrrArrrArr (357)
empty empty((042)) rArrrArrrArrrArr false()
exists exists((042)) rArrrArrrArrrArr true()
distinct-values distinct-values((4424)) rArrrArrrArrrArr (42)
to (1 to 10)[ mod 2 eq 1] rArrrArrrArrrArr (13579)
bull Arithmeticsndash Only a few words on arithmetics ndash XQuery meets the common expectation here Points to note1 Infix operators + - lowastlowastlowastlowast div idiv (integer division)2 operators first atomize their operands then perform
promotion to a common numeric type
92 Preliminaries
promotion to a common numeric type3 if at least one operand is () the result is ()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 11[Gru08]
Examples and pitfalls
ltxgt1ltxgt + 41 rArrrArrrArrrArr 420
() 42 rArrrArrrArrrArr ()
(12) - (23) rArrrArrrArrrArr (type error)
x-42 rArrrArrrArrrArr childx-42 (use x˽-˽42)
xy rArrrArrrArrrArr childxchildy (use x div y)
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)ndash Foor loop
ndash Variable bindings
9 XQuery
ndash Variable bindings
ndash Where clause
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 12
bull XQuery Iteration FLWORsndash Remember that XPath steps perform implicit iteration in cse evaluation of e is iterated with bound to each item in cs in turn
ndash XPath subexpressions aside iteration in XQuery is explicit via the FLWOR (flower) construct
93 Iteration (FLWORs)
explicit via the FLWOR (flower) constructbull The versatile FLWOR is used to express
ndash nested iteration
ndash joins between sequences (of nodes)
ndash groupings
ndash orderings beyond document order etc
bull In a sense FLWOR assumes the role of the SELECT-FROM-WHERE block in SQL
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 13[Gru08]
bull FLWOR Iteration via forin
ndash Explicit iteration
93 Iteration (FLWORs)
Explicit iteration is expressed using the forin construct a
for $v [at $p] in e1
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 14[Gru08]
for $v [at $p] in e1 return e2
If e1 evaluates to the sequence (x1xn) the loop body e2 is
evaluated n times with variable $v bound to each xi [and $pbound to i ] in order The results of these evaluations are concatenated to form a single sequence
a the construct at $p is optional
93 Iteration (FLWORs)
Iteration examples
for $x in (321)
return ($xlowastlowastlowastlowast)rArrrArrrArrrArr (3lowastlowastlowastlowast2lowastlowastlowastlowast1lowastlowastlowastlowast)
for $x in (321)
return $xlowastlowastlowastlowastrArrrArrrArrrArr (321lowastlowastlowastlowast)
for $x in (321)
return for $y in (ab) rArrrArrrArrrArr(3a3b
2a2b
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 15[Gru08]
return for $y in (ab)
return ($x$y)
rArrrArrrArrrArr 2a2b
1a1b)
FLWOR Abbreviations
for $v1 in e1return
for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1$v2 in e2
return e3
bull FLWOR Iteration via forin
93 Iteration (FLWORs)
Purpose of this query Qmax( for $i in csdescendant-or-self[not()]
return count($iancestor) )
A sample cs ldquoAnnotatedrdquo sample cs
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 16[Gru08]
A sample csa
b c
d
e g h
f
ldquoAnnotatedrdquo sample csa
b
e
c
d
g h
f
1
3 3 3
Answer
bull FLWOR Iteration via forin ndash Return every other item in sequence
93 Iteration (FLWORs)
These queries both return the items at odd positions in the inputsequence e
for $i in (1 to count(e))[ mod 2 eq 1]
bull Remember ebv(0) = false()ebv effective boolean value
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 17[Gru08]
for $i in (1 to count(e))[ mod 2 eq 1]
return e[$i]
for $i at $p in e
return if ($p mod 2)
then e[$p]
else ()
bull FLWOR Variable Binding via let =
ndash Note that in the examples on the last slide expression e is re-evaluated count(e) 2 times although e is constant in the loop
93 Iteration (FLWORs)
Variable bindingsThe result of evaluating an expression e may be bound to a variable $v
bull for and let clauses may be freely intermixed
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 18[Gru08]
Variable bindingsThe result of evaluating an expression e1 may be bound to a variable $vvia let
let $v = e1return e2
evaluates e2 with free occurrences of $v replaced by e1
bull FLWOR Variable Binding via letbull bull bull =
93 Iteration (FLWORs)
Iteration vs variable binding
for $x in (321)
return ($x)
rArrrArrrArrrArr (321
)
let $x = (321) rArrrArrrArrrArr (321)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 19[Gru08]
let $x = (321)
return ($x)
rArrrArrrArrrArr (321)
Every other item revisited (flip back two slides)The following hoists the constant e out of the loop body
let $seq = ereturn for $i at $p in $seq
return if ($p mod 2)
then $seq[$p]
else ()
bull Adding a where clause
ndash Inside loop bodies the idiom if (p) then e else ()
is so common that FLWOR comes with a SQL-like where clause to address this
93 Iteration (FLWORs)
A where clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 20[Gru08]
A where clauseIf ebv(p) evaluates to false() under the current variable bindings the
current iteration does not contribute to the result
for $v in e1where p
return e2
equiv
for $v in e1return if (p)
then e2else ()
bull Explicit vs implicit iterationndash XPath implicit iteration
ndash Equivalent nested FLWOR blocks
93 Iteration (FLWORs)
a[b = foo]c[2]d[e = 42]
for $a in a
ndash NB Unlike the XPath step operator for does not change the context item
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 21[Gru08]
for $a in a
where $ab = foo
return for $c at $p in $ac
where $p = 2
return for $d in $cd
where $de = 42
return $d
bull FLWOR Reorder iteration result via order byndash In a FLWOR block for $v in e1 return e2 the order of e1determines the order of the resulting sequence
93 Iteration (FLWORs)
Reordering via order by
In the FLWOR block
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 22[Gru08]
In the FLWOR block
the value (atomization) of e3 determines the order in which the bindingsof $v are used to evaluate e2
for $v in e1order by e3 [ascendingdescending][empty greatestleast]return e2
bull FLWOR Reordering examples
93 Iteration (FLWORs)
An order by no-op reordering by sequence order
for $x at $p in (53142)
order by $p
return $x
rArrrArrrArrrArr (53142)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 23[Gru08]
All bound variables in scope in order by
for $x at $p in (53142)
order by $p + $x
return $x
rArrrArrrArrrArr (13524)
Reordering as in SQLs ORDER BY
for $x at $p in (53142)
order by $x
return $x
rArrrArrrArrrArr (12345)
bull FLWOR Reordering examplesndash Value-based reordering of an XPath step result
93 Iteration (FLWORs)
This query reorders the result of the XPath location step descendantb
based on (string) value Which result is to be expectedlet $a = ltagt
ltb id=0gt42ltbgt
ltb id=1gt5ltbgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 24[Gru08]
ltb id=1gt5ltbgt
ltb id=2gt
ltb id=3gt3ltbgt
ltb id=4gt1ltbgt
ltagt
for $b in $adescendantb
order by $btext() empty greatest
return $bid
Answer
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 25
bull FLWOR semantics tuple spacendash In the W3C XQuery specification the interaction of the five clauses of a FLWOR (for-let-where-order by-return) block is formally explained by means of a tuple space
94 FLOWR Semantics
means of a tuple spacebull Size of tuple space equiv number of iterations performed by FLWOR block
bull The fields of the tuples represent for each iteration
1 forlet variable bindings
2 the outcome of the where clause
3 the value of the reordering criterion and
4 the value returned by the return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 26[Gru08]
bull FLWOR semantics tuple space (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y where order by return
10 1 100 false 4 (110)
1 Complete tuple space
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 27[Gru08]
return ($p$x) 10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (2)2 Filtering where clause ($y lt= 42)
94 FLOWR Semantics
$x $p $y where order by return
10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 28[Gru08]
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (3)3 Reordering order by clause
94 FLOWR Semantics
$x $p $y where order by return
1 10 1 true -5 (101)
2 9 4 true -4 (92)
3 8 9 true -3 (83)
4 To emit the final result scan the tuple space in the order specified by the order by column and concatenate the return column entries
(1019283746556)
ndash Observation some values have been computed but never used
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 29[Gru08]
3 8 9 true -3 (83)
4 7 16 true -2 (74)
5 6 25 true -1 (65)
6 5 36 true 0 (56)
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull XQuery
ndash XQuery is a truly declarative language specifically designed for the purpose of querying XML data
ndash As such XML assumes the role that SQL occupies in the context of relational databases
ndash XQuery exhibits properties known from database (DB) languages as
91 Introduction
ndash XQuery exhibits properties known from database (DB) languages as well as from (functional) programming (PL) languages
ndash The language is designed and formally specified by the W3C XQuery Working Group (httpwwww3orgXMLXQuery)
bull The first working draft documents date back to February 2001 TheXQueryspecification has become a W3C Recommendation in January 2007
bull Members of the working group include Dana FlorescuDB Ioana ManolescuDB Phil WadlerPL Mary FernandezDB+PL Don ChamberlinDB (the father of SQL) Jeacuterocircme SimeonDB Michael RysDB and many others
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 3[Gru08]
bull 12 Programming Language 12 Query Languagendash XQuery is a hybrid exhibiting features commonly found in programming as well as database query languages
bull Programming language features
explicit iteration and variable bindings (
91 Introduction
ndash explicit iteration and variable bindings (forinletin)
ndash recursive user-defined functions
ndash regular expressions strong [static] typing
ndash ordered sequences (much like lists or arrays)
bull Database query language features
ndash filtering
ndash grouping joins expressed via nested for loops
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 4[Gru08]
bull History of XQuery
91 Introduction
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 5[Gru08]
bull XQuery-Preliminariesndash Remember XPath is part of XQuery (as a sublanguage)ndash Some constructs that have not previously been discussed yet are not within the core of our focus on XQuery include
bull Comparisons any XQuery expression evaluates to a
92 Preliminaries
bull Comparisons any XQuery expression evaluates to a sequence of items Consequently many XQuery concepts are prepared to accept sequences (as opposed to single items)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 6[Gru08]
General comparisonsThe general comparison withᶿe1 e2
yields true() if any of the items in the sequences e12 compare
true (existential semantics)
= = lt lt= gt=gtᶿ isin
92 Preliminaries
bull Comparisons General comparison examples
(123) gt (245) rArrrArrrArrrArr true()
(123) = 1 rArrrArrrArrrArr true()
() = 0 rArrrArrrArrrArr false()
2 lt= 1 rArrrArrrArrrArr false()
(123) = 3 rArrrArrrArrrArr true()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 7[Gru08]
ndash The six value comparison operators eq ne lt le ge gtcompare single items by value (atomization)
(123) = 3 rArrrArrrArrrArr true()
(12) = (12) rArrrArrrArrrArr true()
not((12) = (12)) rArrrArrrArrrArr false()
Value comparisons
2 gt 10 rArrrArrrArrrArr true()
ltxgt42ltxgt eq ltygt42ltygt rArrrArrrArrrArr true()
(01) eq 0 rArrrArrrArrrArr (type error)
bull More on comparisons
ndash Note The existential semantics of the general comparison operators may lead to unexpected behavior
92 Preliminaries
a) For an item-by-item comparison use deep-equal()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 8[Gru08]
Surprises
(123) = (13) rArrrArrrArrrArr true()a
(21) = 1 rArrrArrrArrrArr true() or (impl dependent)
bull Node comparisons
92 Preliminaries
based on identity and document order
e1 is e2 nodes e12 identical
e1 ltlt e2 node e1 before e2
e1 gtgt e2 node e1 after e2
ndash Node comparison examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 9[Gru08]
ltxgt42ltxgt eq ltxgt42ltxgt rArrrArrrArrrArr true()
ltxgt42ltxgt is ltxgt42ltxgt rArrrArrrArrrArr false()
root(e1) is root(e2) rArrrArrrArrrArr nodes e12 in same tree
let $a = ltxgtltygtltxgt
return $a ltlt $ayrArrrArrrArrrArr true()
bull Working with sequencesndash XQuery comes with an extensive library of builtinfunctions to perform common computations over sequences
bull Common sequence operations
92 Preliminaries
Function Example
count count((042)) rArrrArrrArrrArr 3
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 10[Gru08]
count count((042)) rArrrArrrArrrArr 3
max max((042)) rArrrArrrArrrArr 4
subsequence subsequence((1357)23) rArrrArrrArrrArr (357)
empty empty((042)) rArrrArrrArrrArr false()
exists exists((042)) rArrrArrrArrrArr true()
distinct-values distinct-values((4424)) rArrrArrrArrrArr (42)
to (1 to 10)[ mod 2 eq 1] rArrrArrrArrrArr (13579)
bull Arithmeticsndash Only a few words on arithmetics ndash XQuery meets the common expectation here Points to note1 Infix operators + - lowastlowastlowastlowast div idiv (integer division)2 operators first atomize their operands then perform
promotion to a common numeric type
92 Preliminaries
promotion to a common numeric type3 if at least one operand is () the result is ()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 11[Gru08]
Examples and pitfalls
ltxgt1ltxgt + 41 rArrrArrrArrrArr 420
() 42 rArrrArrrArrrArr ()
(12) - (23) rArrrArrrArrrArr (type error)
x-42 rArrrArrrArrrArr childx-42 (use x˽-˽42)
xy rArrrArrrArrrArr childxchildy (use x div y)
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)ndash Foor loop
ndash Variable bindings
9 XQuery
ndash Variable bindings
ndash Where clause
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 12
bull XQuery Iteration FLWORsndash Remember that XPath steps perform implicit iteration in cse evaluation of e is iterated with bound to each item in cs in turn
ndash XPath subexpressions aside iteration in XQuery is explicit via the FLWOR (flower) construct
93 Iteration (FLWORs)
explicit via the FLWOR (flower) constructbull The versatile FLWOR is used to express
ndash nested iteration
ndash joins between sequences (of nodes)
ndash groupings
ndash orderings beyond document order etc
bull In a sense FLWOR assumes the role of the SELECT-FROM-WHERE block in SQL
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 13[Gru08]
bull FLWOR Iteration via forin
ndash Explicit iteration
93 Iteration (FLWORs)
Explicit iteration is expressed using the forin construct a
for $v [at $p] in e1
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 14[Gru08]
for $v [at $p] in e1 return e2
If e1 evaluates to the sequence (x1xn) the loop body e2 is
evaluated n times with variable $v bound to each xi [and $pbound to i ] in order The results of these evaluations are concatenated to form a single sequence
a the construct at $p is optional
93 Iteration (FLWORs)
Iteration examples
for $x in (321)
return ($xlowastlowastlowastlowast)rArrrArrrArrrArr (3lowastlowastlowastlowast2lowastlowastlowastlowast1lowastlowastlowastlowast)
for $x in (321)
return $xlowastlowastlowastlowastrArrrArrrArrrArr (321lowastlowastlowastlowast)
for $x in (321)
return for $y in (ab) rArrrArrrArrrArr(3a3b
2a2b
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 15[Gru08]
return for $y in (ab)
return ($x$y)
rArrrArrrArrrArr 2a2b
1a1b)
FLWOR Abbreviations
for $v1 in e1return
for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1$v2 in e2
return e3
bull FLWOR Iteration via forin
93 Iteration (FLWORs)
Purpose of this query Qmax( for $i in csdescendant-or-self[not()]
return count($iancestor) )
A sample cs ldquoAnnotatedrdquo sample cs
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 16[Gru08]
A sample csa
b c
d
e g h
f
ldquoAnnotatedrdquo sample csa
b
e
c
d
g h
f
1
3 3 3
Answer
bull FLWOR Iteration via forin ndash Return every other item in sequence
93 Iteration (FLWORs)
These queries both return the items at odd positions in the inputsequence e
for $i in (1 to count(e))[ mod 2 eq 1]
bull Remember ebv(0) = false()ebv effective boolean value
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 17[Gru08]
for $i in (1 to count(e))[ mod 2 eq 1]
return e[$i]
for $i at $p in e
return if ($p mod 2)
then e[$p]
else ()
bull FLWOR Variable Binding via let =
ndash Note that in the examples on the last slide expression e is re-evaluated count(e) 2 times although e is constant in the loop
93 Iteration (FLWORs)
Variable bindingsThe result of evaluating an expression e may be bound to a variable $v
bull for and let clauses may be freely intermixed
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 18[Gru08]
Variable bindingsThe result of evaluating an expression e1 may be bound to a variable $vvia let
let $v = e1return e2
evaluates e2 with free occurrences of $v replaced by e1
bull FLWOR Variable Binding via letbull bull bull =
93 Iteration (FLWORs)
Iteration vs variable binding
for $x in (321)
return ($x)
rArrrArrrArrrArr (321
)
let $x = (321) rArrrArrrArrrArr (321)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 19[Gru08]
let $x = (321)
return ($x)
rArrrArrrArrrArr (321)
Every other item revisited (flip back two slides)The following hoists the constant e out of the loop body
let $seq = ereturn for $i at $p in $seq
return if ($p mod 2)
then $seq[$p]
else ()
bull Adding a where clause
ndash Inside loop bodies the idiom if (p) then e else ()
is so common that FLWOR comes with a SQL-like where clause to address this
93 Iteration (FLWORs)
A where clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 20[Gru08]
A where clauseIf ebv(p) evaluates to false() under the current variable bindings the
current iteration does not contribute to the result
for $v in e1where p
return e2
equiv
for $v in e1return if (p)
then e2else ()
bull Explicit vs implicit iterationndash XPath implicit iteration
ndash Equivalent nested FLWOR blocks
93 Iteration (FLWORs)
a[b = foo]c[2]d[e = 42]
for $a in a
ndash NB Unlike the XPath step operator for does not change the context item
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 21[Gru08]
for $a in a
where $ab = foo
return for $c at $p in $ac
where $p = 2
return for $d in $cd
where $de = 42
return $d
bull FLWOR Reorder iteration result via order byndash In a FLWOR block for $v in e1 return e2 the order of e1determines the order of the resulting sequence
93 Iteration (FLWORs)
Reordering via order by
In the FLWOR block
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 22[Gru08]
In the FLWOR block
the value (atomization) of e3 determines the order in which the bindingsof $v are used to evaluate e2
for $v in e1order by e3 [ascendingdescending][empty greatestleast]return e2
bull FLWOR Reordering examples
93 Iteration (FLWORs)
An order by no-op reordering by sequence order
for $x at $p in (53142)
order by $p
return $x
rArrrArrrArrrArr (53142)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 23[Gru08]
All bound variables in scope in order by
for $x at $p in (53142)
order by $p + $x
return $x
rArrrArrrArrrArr (13524)
Reordering as in SQLs ORDER BY
for $x at $p in (53142)
order by $x
return $x
rArrrArrrArrrArr (12345)
bull FLWOR Reordering examplesndash Value-based reordering of an XPath step result
93 Iteration (FLWORs)
This query reorders the result of the XPath location step descendantb
based on (string) value Which result is to be expectedlet $a = ltagt
ltb id=0gt42ltbgt
ltb id=1gt5ltbgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 24[Gru08]
ltb id=1gt5ltbgt
ltb id=2gt
ltb id=3gt3ltbgt
ltb id=4gt1ltbgt
ltagt
for $b in $adescendantb
order by $btext() empty greatest
return $bid
Answer
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 25
bull FLWOR semantics tuple spacendash In the W3C XQuery specification the interaction of the five clauses of a FLWOR (for-let-where-order by-return) block is formally explained by means of a tuple space
94 FLOWR Semantics
means of a tuple spacebull Size of tuple space equiv number of iterations performed by FLWOR block
bull The fields of the tuples represent for each iteration
1 forlet variable bindings
2 the outcome of the where clause
3 the value of the reordering criterion and
4 the value returned by the return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 26[Gru08]
bull FLWOR semantics tuple space (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y where order by return
10 1 100 false 4 (110)
1 Complete tuple space
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 27[Gru08]
return ($p$x) 10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (2)2 Filtering where clause ($y lt= 42)
94 FLOWR Semantics
$x $p $y where order by return
10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 28[Gru08]
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (3)3 Reordering order by clause
94 FLOWR Semantics
$x $p $y where order by return
1 10 1 true -5 (101)
2 9 4 true -4 (92)
3 8 9 true -3 (83)
4 To emit the final result scan the tuple space in the order specified by the order by column and concatenate the return column entries
(1019283746556)
ndash Observation some values have been computed but never used
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 29[Gru08]
3 8 9 true -3 (83)
4 7 16 true -2 (74)
5 6 25 true -1 (65)
6 5 36 true 0 (56)
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull 12 Programming Language 12 Query Languagendash XQuery is a hybrid exhibiting features commonly found in programming as well as database query languages
bull Programming language features
explicit iteration and variable bindings (
91 Introduction
ndash explicit iteration and variable bindings (forinletin)
ndash recursive user-defined functions
ndash regular expressions strong [static] typing
ndash ordered sequences (much like lists or arrays)
bull Database query language features
ndash filtering
ndash grouping joins expressed via nested for loops
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 4[Gru08]
bull History of XQuery
91 Introduction
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 5[Gru08]
bull XQuery-Preliminariesndash Remember XPath is part of XQuery (as a sublanguage)ndash Some constructs that have not previously been discussed yet are not within the core of our focus on XQuery include
bull Comparisons any XQuery expression evaluates to a
92 Preliminaries
bull Comparisons any XQuery expression evaluates to a sequence of items Consequently many XQuery concepts are prepared to accept sequences (as opposed to single items)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 6[Gru08]
General comparisonsThe general comparison withᶿe1 e2
yields true() if any of the items in the sequences e12 compare
true (existential semantics)
= = lt lt= gt=gtᶿ isin
92 Preliminaries
bull Comparisons General comparison examples
(123) gt (245) rArrrArrrArrrArr true()
(123) = 1 rArrrArrrArrrArr true()
() = 0 rArrrArrrArrrArr false()
2 lt= 1 rArrrArrrArrrArr false()
(123) = 3 rArrrArrrArrrArr true()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 7[Gru08]
ndash The six value comparison operators eq ne lt le ge gtcompare single items by value (atomization)
(123) = 3 rArrrArrrArrrArr true()
(12) = (12) rArrrArrrArrrArr true()
not((12) = (12)) rArrrArrrArrrArr false()
Value comparisons
2 gt 10 rArrrArrrArrrArr true()
ltxgt42ltxgt eq ltygt42ltygt rArrrArrrArrrArr true()
(01) eq 0 rArrrArrrArrrArr (type error)
bull More on comparisons
ndash Note The existential semantics of the general comparison operators may lead to unexpected behavior
92 Preliminaries
a) For an item-by-item comparison use deep-equal()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 8[Gru08]
Surprises
(123) = (13) rArrrArrrArrrArr true()a
(21) = 1 rArrrArrrArrrArr true() or (impl dependent)
bull Node comparisons
92 Preliminaries
based on identity and document order
e1 is e2 nodes e12 identical
e1 ltlt e2 node e1 before e2
e1 gtgt e2 node e1 after e2
ndash Node comparison examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 9[Gru08]
ltxgt42ltxgt eq ltxgt42ltxgt rArrrArrrArrrArr true()
ltxgt42ltxgt is ltxgt42ltxgt rArrrArrrArrrArr false()
root(e1) is root(e2) rArrrArrrArrrArr nodes e12 in same tree
let $a = ltxgtltygtltxgt
return $a ltlt $ayrArrrArrrArrrArr true()
bull Working with sequencesndash XQuery comes with an extensive library of builtinfunctions to perform common computations over sequences
bull Common sequence operations
92 Preliminaries
Function Example
count count((042)) rArrrArrrArrrArr 3
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 10[Gru08]
count count((042)) rArrrArrrArrrArr 3
max max((042)) rArrrArrrArrrArr 4
subsequence subsequence((1357)23) rArrrArrrArrrArr (357)
empty empty((042)) rArrrArrrArrrArr false()
exists exists((042)) rArrrArrrArrrArr true()
distinct-values distinct-values((4424)) rArrrArrrArrrArr (42)
to (1 to 10)[ mod 2 eq 1] rArrrArrrArrrArr (13579)
bull Arithmeticsndash Only a few words on arithmetics ndash XQuery meets the common expectation here Points to note1 Infix operators + - lowastlowastlowastlowast div idiv (integer division)2 operators first atomize their operands then perform
promotion to a common numeric type
92 Preliminaries
promotion to a common numeric type3 if at least one operand is () the result is ()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 11[Gru08]
Examples and pitfalls
ltxgt1ltxgt + 41 rArrrArrrArrrArr 420
() 42 rArrrArrrArrrArr ()
(12) - (23) rArrrArrrArrrArr (type error)
x-42 rArrrArrrArrrArr childx-42 (use x˽-˽42)
xy rArrrArrrArrrArr childxchildy (use x div y)
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)ndash Foor loop
ndash Variable bindings
9 XQuery
ndash Variable bindings
ndash Where clause
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 12
bull XQuery Iteration FLWORsndash Remember that XPath steps perform implicit iteration in cse evaluation of e is iterated with bound to each item in cs in turn
ndash XPath subexpressions aside iteration in XQuery is explicit via the FLWOR (flower) construct
93 Iteration (FLWORs)
explicit via the FLWOR (flower) constructbull The versatile FLWOR is used to express
ndash nested iteration
ndash joins between sequences (of nodes)
ndash groupings
ndash orderings beyond document order etc
bull In a sense FLWOR assumes the role of the SELECT-FROM-WHERE block in SQL
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 13[Gru08]
bull FLWOR Iteration via forin
ndash Explicit iteration
93 Iteration (FLWORs)
Explicit iteration is expressed using the forin construct a
for $v [at $p] in e1
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 14[Gru08]
for $v [at $p] in e1 return e2
If e1 evaluates to the sequence (x1xn) the loop body e2 is
evaluated n times with variable $v bound to each xi [and $pbound to i ] in order The results of these evaluations are concatenated to form a single sequence
a the construct at $p is optional
93 Iteration (FLWORs)
Iteration examples
for $x in (321)
return ($xlowastlowastlowastlowast)rArrrArrrArrrArr (3lowastlowastlowastlowast2lowastlowastlowastlowast1lowastlowastlowastlowast)
for $x in (321)
return $xlowastlowastlowastlowastrArrrArrrArrrArr (321lowastlowastlowastlowast)
for $x in (321)
return for $y in (ab) rArrrArrrArrrArr(3a3b
2a2b
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 15[Gru08]
return for $y in (ab)
return ($x$y)
rArrrArrrArrrArr 2a2b
1a1b)
FLWOR Abbreviations
for $v1 in e1return
for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1$v2 in e2
return e3
bull FLWOR Iteration via forin
93 Iteration (FLWORs)
Purpose of this query Qmax( for $i in csdescendant-or-self[not()]
return count($iancestor) )
A sample cs ldquoAnnotatedrdquo sample cs
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 16[Gru08]
A sample csa
b c
d
e g h
f
ldquoAnnotatedrdquo sample csa
b
e
c
d
g h
f
1
3 3 3
Answer
bull FLWOR Iteration via forin ndash Return every other item in sequence
93 Iteration (FLWORs)
These queries both return the items at odd positions in the inputsequence e
for $i in (1 to count(e))[ mod 2 eq 1]
bull Remember ebv(0) = false()ebv effective boolean value
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 17[Gru08]
for $i in (1 to count(e))[ mod 2 eq 1]
return e[$i]
for $i at $p in e
return if ($p mod 2)
then e[$p]
else ()
bull FLWOR Variable Binding via let =
ndash Note that in the examples on the last slide expression e is re-evaluated count(e) 2 times although e is constant in the loop
93 Iteration (FLWORs)
Variable bindingsThe result of evaluating an expression e may be bound to a variable $v
bull for and let clauses may be freely intermixed
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 18[Gru08]
Variable bindingsThe result of evaluating an expression e1 may be bound to a variable $vvia let
let $v = e1return e2
evaluates e2 with free occurrences of $v replaced by e1
bull FLWOR Variable Binding via letbull bull bull =
93 Iteration (FLWORs)
Iteration vs variable binding
for $x in (321)
return ($x)
rArrrArrrArrrArr (321
)
let $x = (321) rArrrArrrArrrArr (321)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 19[Gru08]
let $x = (321)
return ($x)
rArrrArrrArrrArr (321)
Every other item revisited (flip back two slides)The following hoists the constant e out of the loop body
let $seq = ereturn for $i at $p in $seq
return if ($p mod 2)
then $seq[$p]
else ()
bull Adding a where clause
ndash Inside loop bodies the idiom if (p) then e else ()
is so common that FLWOR comes with a SQL-like where clause to address this
93 Iteration (FLWORs)
A where clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 20[Gru08]
A where clauseIf ebv(p) evaluates to false() under the current variable bindings the
current iteration does not contribute to the result
for $v in e1where p
return e2
equiv
for $v in e1return if (p)
then e2else ()
bull Explicit vs implicit iterationndash XPath implicit iteration
ndash Equivalent nested FLWOR blocks
93 Iteration (FLWORs)
a[b = foo]c[2]d[e = 42]
for $a in a
ndash NB Unlike the XPath step operator for does not change the context item
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 21[Gru08]
for $a in a
where $ab = foo
return for $c at $p in $ac
where $p = 2
return for $d in $cd
where $de = 42
return $d
bull FLWOR Reorder iteration result via order byndash In a FLWOR block for $v in e1 return e2 the order of e1determines the order of the resulting sequence
93 Iteration (FLWORs)
Reordering via order by
In the FLWOR block
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 22[Gru08]
In the FLWOR block
the value (atomization) of e3 determines the order in which the bindingsof $v are used to evaluate e2
for $v in e1order by e3 [ascendingdescending][empty greatestleast]return e2
bull FLWOR Reordering examples
93 Iteration (FLWORs)
An order by no-op reordering by sequence order
for $x at $p in (53142)
order by $p
return $x
rArrrArrrArrrArr (53142)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 23[Gru08]
All bound variables in scope in order by
for $x at $p in (53142)
order by $p + $x
return $x
rArrrArrrArrrArr (13524)
Reordering as in SQLs ORDER BY
for $x at $p in (53142)
order by $x
return $x
rArrrArrrArrrArr (12345)
bull FLWOR Reordering examplesndash Value-based reordering of an XPath step result
93 Iteration (FLWORs)
This query reorders the result of the XPath location step descendantb
based on (string) value Which result is to be expectedlet $a = ltagt
ltb id=0gt42ltbgt
ltb id=1gt5ltbgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 24[Gru08]
ltb id=1gt5ltbgt
ltb id=2gt
ltb id=3gt3ltbgt
ltb id=4gt1ltbgt
ltagt
for $b in $adescendantb
order by $btext() empty greatest
return $bid
Answer
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 25
bull FLWOR semantics tuple spacendash In the W3C XQuery specification the interaction of the five clauses of a FLWOR (for-let-where-order by-return) block is formally explained by means of a tuple space
94 FLOWR Semantics
means of a tuple spacebull Size of tuple space equiv number of iterations performed by FLWOR block
bull The fields of the tuples represent for each iteration
1 forlet variable bindings
2 the outcome of the where clause
3 the value of the reordering criterion and
4 the value returned by the return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 26[Gru08]
bull FLWOR semantics tuple space (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y where order by return
10 1 100 false 4 (110)
1 Complete tuple space
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 27[Gru08]
return ($p$x) 10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (2)2 Filtering where clause ($y lt= 42)
94 FLOWR Semantics
$x $p $y where order by return
10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 28[Gru08]
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (3)3 Reordering order by clause
94 FLOWR Semantics
$x $p $y where order by return
1 10 1 true -5 (101)
2 9 4 true -4 (92)
3 8 9 true -3 (83)
4 To emit the final result scan the tuple space in the order specified by the order by column and concatenate the return column entries
(1019283746556)
ndash Observation some values have been computed but never used
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 29[Gru08]
3 8 9 true -3 (83)
4 7 16 true -2 (74)
5 6 25 true -1 (65)
6 5 36 true 0 (56)
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull History of XQuery
91 Introduction
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 5[Gru08]
bull XQuery-Preliminariesndash Remember XPath is part of XQuery (as a sublanguage)ndash Some constructs that have not previously been discussed yet are not within the core of our focus on XQuery include
bull Comparisons any XQuery expression evaluates to a
92 Preliminaries
bull Comparisons any XQuery expression evaluates to a sequence of items Consequently many XQuery concepts are prepared to accept sequences (as opposed to single items)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 6[Gru08]
General comparisonsThe general comparison withᶿe1 e2
yields true() if any of the items in the sequences e12 compare
true (existential semantics)
= = lt lt= gt=gtᶿ isin
92 Preliminaries
bull Comparisons General comparison examples
(123) gt (245) rArrrArrrArrrArr true()
(123) = 1 rArrrArrrArrrArr true()
() = 0 rArrrArrrArrrArr false()
2 lt= 1 rArrrArrrArrrArr false()
(123) = 3 rArrrArrrArrrArr true()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 7[Gru08]
ndash The six value comparison operators eq ne lt le ge gtcompare single items by value (atomization)
(123) = 3 rArrrArrrArrrArr true()
(12) = (12) rArrrArrrArrrArr true()
not((12) = (12)) rArrrArrrArrrArr false()
Value comparisons
2 gt 10 rArrrArrrArrrArr true()
ltxgt42ltxgt eq ltygt42ltygt rArrrArrrArrrArr true()
(01) eq 0 rArrrArrrArrrArr (type error)
bull More on comparisons
ndash Note The existential semantics of the general comparison operators may lead to unexpected behavior
92 Preliminaries
a) For an item-by-item comparison use deep-equal()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 8[Gru08]
Surprises
(123) = (13) rArrrArrrArrrArr true()a
(21) = 1 rArrrArrrArrrArr true() or (impl dependent)
bull Node comparisons
92 Preliminaries
based on identity and document order
e1 is e2 nodes e12 identical
e1 ltlt e2 node e1 before e2
e1 gtgt e2 node e1 after e2
ndash Node comparison examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 9[Gru08]
ltxgt42ltxgt eq ltxgt42ltxgt rArrrArrrArrrArr true()
ltxgt42ltxgt is ltxgt42ltxgt rArrrArrrArrrArr false()
root(e1) is root(e2) rArrrArrrArrrArr nodes e12 in same tree
let $a = ltxgtltygtltxgt
return $a ltlt $ayrArrrArrrArrrArr true()
bull Working with sequencesndash XQuery comes with an extensive library of builtinfunctions to perform common computations over sequences
bull Common sequence operations
92 Preliminaries
Function Example
count count((042)) rArrrArrrArrrArr 3
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 10[Gru08]
count count((042)) rArrrArrrArrrArr 3
max max((042)) rArrrArrrArrrArr 4
subsequence subsequence((1357)23) rArrrArrrArrrArr (357)
empty empty((042)) rArrrArrrArrrArr false()
exists exists((042)) rArrrArrrArrrArr true()
distinct-values distinct-values((4424)) rArrrArrrArrrArr (42)
to (1 to 10)[ mod 2 eq 1] rArrrArrrArrrArr (13579)
bull Arithmeticsndash Only a few words on arithmetics ndash XQuery meets the common expectation here Points to note1 Infix operators + - lowastlowastlowastlowast div idiv (integer division)2 operators first atomize their operands then perform
promotion to a common numeric type
92 Preliminaries
promotion to a common numeric type3 if at least one operand is () the result is ()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 11[Gru08]
Examples and pitfalls
ltxgt1ltxgt + 41 rArrrArrrArrrArr 420
() 42 rArrrArrrArrrArr ()
(12) - (23) rArrrArrrArrrArr (type error)
x-42 rArrrArrrArrrArr childx-42 (use x˽-˽42)
xy rArrrArrrArrrArr childxchildy (use x div y)
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)ndash Foor loop
ndash Variable bindings
9 XQuery
ndash Variable bindings
ndash Where clause
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 12
bull XQuery Iteration FLWORsndash Remember that XPath steps perform implicit iteration in cse evaluation of e is iterated with bound to each item in cs in turn
ndash XPath subexpressions aside iteration in XQuery is explicit via the FLWOR (flower) construct
93 Iteration (FLWORs)
explicit via the FLWOR (flower) constructbull The versatile FLWOR is used to express
ndash nested iteration
ndash joins between sequences (of nodes)
ndash groupings
ndash orderings beyond document order etc
bull In a sense FLWOR assumes the role of the SELECT-FROM-WHERE block in SQL
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 13[Gru08]
bull FLWOR Iteration via forin
ndash Explicit iteration
93 Iteration (FLWORs)
Explicit iteration is expressed using the forin construct a
for $v [at $p] in e1
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 14[Gru08]
for $v [at $p] in e1 return e2
If e1 evaluates to the sequence (x1xn) the loop body e2 is
evaluated n times with variable $v bound to each xi [and $pbound to i ] in order The results of these evaluations are concatenated to form a single sequence
a the construct at $p is optional
93 Iteration (FLWORs)
Iteration examples
for $x in (321)
return ($xlowastlowastlowastlowast)rArrrArrrArrrArr (3lowastlowastlowastlowast2lowastlowastlowastlowast1lowastlowastlowastlowast)
for $x in (321)
return $xlowastlowastlowastlowastrArrrArrrArrrArr (321lowastlowastlowastlowast)
for $x in (321)
return for $y in (ab) rArrrArrrArrrArr(3a3b
2a2b
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 15[Gru08]
return for $y in (ab)
return ($x$y)
rArrrArrrArrrArr 2a2b
1a1b)
FLWOR Abbreviations
for $v1 in e1return
for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1$v2 in e2
return e3
bull FLWOR Iteration via forin
93 Iteration (FLWORs)
Purpose of this query Qmax( for $i in csdescendant-or-self[not()]
return count($iancestor) )
A sample cs ldquoAnnotatedrdquo sample cs
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 16[Gru08]
A sample csa
b c
d
e g h
f
ldquoAnnotatedrdquo sample csa
b
e
c
d
g h
f
1
3 3 3
Answer
bull FLWOR Iteration via forin ndash Return every other item in sequence
93 Iteration (FLWORs)
These queries both return the items at odd positions in the inputsequence e
for $i in (1 to count(e))[ mod 2 eq 1]
bull Remember ebv(0) = false()ebv effective boolean value
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 17[Gru08]
for $i in (1 to count(e))[ mod 2 eq 1]
return e[$i]
for $i at $p in e
return if ($p mod 2)
then e[$p]
else ()
bull FLWOR Variable Binding via let =
ndash Note that in the examples on the last slide expression e is re-evaluated count(e) 2 times although e is constant in the loop
93 Iteration (FLWORs)
Variable bindingsThe result of evaluating an expression e may be bound to a variable $v
bull for and let clauses may be freely intermixed
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 18[Gru08]
Variable bindingsThe result of evaluating an expression e1 may be bound to a variable $vvia let
let $v = e1return e2
evaluates e2 with free occurrences of $v replaced by e1
bull FLWOR Variable Binding via letbull bull bull =
93 Iteration (FLWORs)
Iteration vs variable binding
for $x in (321)
return ($x)
rArrrArrrArrrArr (321
)
let $x = (321) rArrrArrrArrrArr (321)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 19[Gru08]
let $x = (321)
return ($x)
rArrrArrrArrrArr (321)
Every other item revisited (flip back two slides)The following hoists the constant e out of the loop body
let $seq = ereturn for $i at $p in $seq
return if ($p mod 2)
then $seq[$p]
else ()
bull Adding a where clause
ndash Inside loop bodies the idiom if (p) then e else ()
is so common that FLWOR comes with a SQL-like where clause to address this
93 Iteration (FLWORs)
A where clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 20[Gru08]
A where clauseIf ebv(p) evaluates to false() under the current variable bindings the
current iteration does not contribute to the result
for $v in e1where p
return e2
equiv
for $v in e1return if (p)
then e2else ()
bull Explicit vs implicit iterationndash XPath implicit iteration
ndash Equivalent nested FLWOR blocks
93 Iteration (FLWORs)
a[b = foo]c[2]d[e = 42]
for $a in a
ndash NB Unlike the XPath step operator for does not change the context item
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 21[Gru08]
for $a in a
where $ab = foo
return for $c at $p in $ac
where $p = 2
return for $d in $cd
where $de = 42
return $d
bull FLWOR Reorder iteration result via order byndash In a FLWOR block for $v in e1 return e2 the order of e1determines the order of the resulting sequence
93 Iteration (FLWORs)
Reordering via order by
In the FLWOR block
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 22[Gru08]
In the FLWOR block
the value (atomization) of e3 determines the order in which the bindingsof $v are used to evaluate e2
for $v in e1order by e3 [ascendingdescending][empty greatestleast]return e2
bull FLWOR Reordering examples
93 Iteration (FLWORs)
An order by no-op reordering by sequence order
for $x at $p in (53142)
order by $p
return $x
rArrrArrrArrrArr (53142)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 23[Gru08]
All bound variables in scope in order by
for $x at $p in (53142)
order by $p + $x
return $x
rArrrArrrArrrArr (13524)
Reordering as in SQLs ORDER BY
for $x at $p in (53142)
order by $x
return $x
rArrrArrrArrrArr (12345)
bull FLWOR Reordering examplesndash Value-based reordering of an XPath step result
93 Iteration (FLWORs)
This query reorders the result of the XPath location step descendantb
based on (string) value Which result is to be expectedlet $a = ltagt
ltb id=0gt42ltbgt
ltb id=1gt5ltbgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 24[Gru08]
ltb id=1gt5ltbgt
ltb id=2gt
ltb id=3gt3ltbgt
ltb id=4gt1ltbgt
ltagt
for $b in $adescendantb
order by $btext() empty greatest
return $bid
Answer
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 25
bull FLWOR semantics tuple spacendash In the W3C XQuery specification the interaction of the five clauses of a FLWOR (for-let-where-order by-return) block is formally explained by means of a tuple space
94 FLOWR Semantics
means of a tuple spacebull Size of tuple space equiv number of iterations performed by FLWOR block
bull The fields of the tuples represent for each iteration
1 forlet variable bindings
2 the outcome of the where clause
3 the value of the reordering criterion and
4 the value returned by the return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 26[Gru08]
bull FLWOR semantics tuple space (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y where order by return
10 1 100 false 4 (110)
1 Complete tuple space
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 27[Gru08]
return ($p$x) 10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (2)2 Filtering where clause ($y lt= 42)
94 FLOWR Semantics
$x $p $y where order by return
10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 28[Gru08]
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (3)3 Reordering order by clause
94 FLOWR Semantics
$x $p $y where order by return
1 10 1 true -5 (101)
2 9 4 true -4 (92)
3 8 9 true -3 (83)
4 To emit the final result scan the tuple space in the order specified by the order by column and concatenate the return column entries
(1019283746556)
ndash Observation some values have been computed but never used
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 29[Gru08]
3 8 9 true -3 (83)
4 7 16 true -2 (74)
5 6 25 true -1 (65)
6 5 36 true 0 (56)
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull XQuery-Preliminariesndash Remember XPath is part of XQuery (as a sublanguage)ndash Some constructs that have not previously been discussed yet are not within the core of our focus on XQuery include
bull Comparisons any XQuery expression evaluates to a
92 Preliminaries
bull Comparisons any XQuery expression evaluates to a sequence of items Consequently many XQuery concepts are prepared to accept sequences (as opposed to single items)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 6[Gru08]
General comparisonsThe general comparison withᶿe1 e2
yields true() if any of the items in the sequences e12 compare
true (existential semantics)
= = lt lt= gt=gtᶿ isin
92 Preliminaries
bull Comparisons General comparison examples
(123) gt (245) rArrrArrrArrrArr true()
(123) = 1 rArrrArrrArrrArr true()
() = 0 rArrrArrrArrrArr false()
2 lt= 1 rArrrArrrArrrArr false()
(123) = 3 rArrrArrrArrrArr true()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 7[Gru08]
ndash The six value comparison operators eq ne lt le ge gtcompare single items by value (atomization)
(123) = 3 rArrrArrrArrrArr true()
(12) = (12) rArrrArrrArrrArr true()
not((12) = (12)) rArrrArrrArrrArr false()
Value comparisons
2 gt 10 rArrrArrrArrrArr true()
ltxgt42ltxgt eq ltygt42ltygt rArrrArrrArrrArr true()
(01) eq 0 rArrrArrrArrrArr (type error)
bull More on comparisons
ndash Note The existential semantics of the general comparison operators may lead to unexpected behavior
92 Preliminaries
a) For an item-by-item comparison use deep-equal()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 8[Gru08]
Surprises
(123) = (13) rArrrArrrArrrArr true()a
(21) = 1 rArrrArrrArrrArr true() or (impl dependent)
bull Node comparisons
92 Preliminaries
based on identity and document order
e1 is e2 nodes e12 identical
e1 ltlt e2 node e1 before e2
e1 gtgt e2 node e1 after e2
ndash Node comparison examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 9[Gru08]
ltxgt42ltxgt eq ltxgt42ltxgt rArrrArrrArrrArr true()
ltxgt42ltxgt is ltxgt42ltxgt rArrrArrrArrrArr false()
root(e1) is root(e2) rArrrArrrArrrArr nodes e12 in same tree
let $a = ltxgtltygtltxgt
return $a ltlt $ayrArrrArrrArrrArr true()
bull Working with sequencesndash XQuery comes with an extensive library of builtinfunctions to perform common computations over sequences
bull Common sequence operations
92 Preliminaries
Function Example
count count((042)) rArrrArrrArrrArr 3
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 10[Gru08]
count count((042)) rArrrArrrArrrArr 3
max max((042)) rArrrArrrArrrArr 4
subsequence subsequence((1357)23) rArrrArrrArrrArr (357)
empty empty((042)) rArrrArrrArrrArr false()
exists exists((042)) rArrrArrrArrrArr true()
distinct-values distinct-values((4424)) rArrrArrrArrrArr (42)
to (1 to 10)[ mod 2 eq 1] rArrrArrrArrrArr (13579)
bull Arithmeticsndash Only a few words on arithmetics ndash XQuery meets the common expectation here Points to note1 Infix operators + - lowastlowastlowastlowast div idiv (integer division)2 operators first atomize their operands then perform
promotion to a common numeric type
92 Preliminaries
promotion to a common numeric type3 if at least one operand is () the result is ()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 11[Gru08]
Examples and pitfalls
ltxgt1ltxgt + 41 rArrrArrrArrrArr 420
() 42 rArrrArrrArrrArr ()
(12) - (23) rArrrArrrArrrArr (type error)
x-42 rArrrArrrArrrArr childx-42 (use x˽-˽42)
xy rArrrArrrArrrArr childxchildy (use x div y)
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)ndash Foor loop
ndash Variable bindings
9 XQuery
ndash Variable bindings
ndash Where clause
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 12
bull XQuery Iteration FLWORsndash Remember that XPath steps perform implicit iteration in cse evaluation of e is iterated with bound to each item in cs in turn
ndash XPath subexpressions aside iteration in XQuery is explicit via the FLWOR (flower) construct
93 Iteration (FLWORs)
explicit via the FLWOR (flower) constructbull The versatile FLWOR is used to express
ndash nested iteration
ndash joins between sequences (of nodes)
ndash groupings
ndash orderings beyond document order etc
bull In a sense FLWOR assumes the role of the SELECT-FROM-WHERE block in SQL
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 13[Gru08]
bull FLWOR Iteration via forin
ndash Explicit iteration
93 Iteration (FLWORs)
Explicit iteration is expressed using the forin construct a
for $v [at $p] in e1
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 14[Gru08]
for $v [at $p] in e1 return e2
If e1 evaluates to the sequence (x1xn) the loop body e2 is
evaluated n times with variable $v bound to each xi [and $pbound to i ] in order The results of these evaluations are concatenated to form a single sequence
a the construct at $p is optional
93 Iteration (FLWORs)
Iteration examples
for $x in (321)
return ($xlowastlowastlowastlowast)rArrrArrrArrrArr (3lowastlowastlowastlowast2lowastlowastlowastlowast1lowastlowastlowastlowast)
for $x in (321)
return $xlowastlowastlowastlowastrArrrArrrArrrArr (321lowastlowastlowastlowast)
for $x in (321)
return for $y in (ab) rArrrArrrArrrArr(3a3b
2a2b
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 15[Gru08]
return for $y in (ab)
return ($x$y)
rArrrArrrArrrArr 2a2b
1a1b)
FLWOR Abbreviations
for $v1 in e1return
for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1$v2 in e2
return e3
bull FLWOR Iteration via forin
93 Iteration (FLWORs)
Purpose of this query Qmax( for $i in csdescendant-or-self[not()]
return count($iancestor) )
A sample cs ldquoAnnotatedrdquo sample cs
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 16[Gru08]
A sample csa
b c
d
e g h
f
ldquoAnnotatedrdquo sample csa
b
e
c
d
g h
f
1
3 3 3
Answer
bull FLWOR Iteration via forin ndash Return every other item in sequence
93 Iteration (FLWORs)
These queries both return the items at odd positions in the inputsequence e
for $i in (1 to count(e))[ mod 2 eq 1]
bull Remember ebv(0) = false()ebv effective boolean value
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 17[Gru08]
for $i in (1 to count(e))[ mod 2 eq 1]
return e[$i]
for $i at $p in e
return if ($p mod 2)
then e[$p]
else ()
bull FLWOR Variable Binding via let =
ndash Note that in the examples on the last slide expression e is re-evaluated count(e) 2 times although e is constant in the loop
93 Iteration (FLWORs)
Variable bindingsThe result of evaluating an expression e may be bound to a variable $v
bull for and let clauses may be freely intermixed
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 18[Gru08]
Variable bindingsThe result of evaluating an expression e1 may be bound to a variable $vvia let
let $v = e1return e2
evaluates e2 with free occurrences of $v replaced by e1
bull FLWOR Variable Binding via letbull bull bull =
93 Iteration (FLWORs)
Iteration vs variable binding
for $x in (321)
return ($x)
rArrrArrrArrrArr (321
)
let $x = (321) rArrrArrrArrrArr (321)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 19[Gru08]
let $x = (321)
return ($x)
rArrrArrrArrrArr (321)
Every other item revisited (flip back two slides)The following hoists the constant e out of the loop body
let $seq = ereturn for $i at $p in $seq
return if ($p mod 2)
then $seq[$p]
else ()
bull Adding a where clause
ndash Inside loop bodies the idiom if (p) then e else ()
is so common that FLWOR comes with a SQL-like where clause to address this
93 Iteration (FLWORs)
A where clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 20[Gru08]
A where clauseIf ebv(p) evaluates to false() under the current variable bindings the
current iteration does not contribute to the result
for $v in e1where p
return e2
equiv
for $v in e1return if (p)
then e2else ()
bull Explicit vs implicit iterationndash XPath implicit iteration
ndash Equivalent nested FLWOR blocks
93 Iteration (FLWORs)
a[b = foo]c[2]d[e = 42]
for $a in a
ndash NB Unlike the XPath step operator for does not change the context item
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 21[Gru08]
for $a in a
where $ab = foo
return for $c at $p in $ac
where $p = 2
return for $d in $cd
where $de = 42
return $d
bull FLWOR Reorder iteration result via order byndash In a FLWOR block for $v in e1 return e2 the order of e1determines the order of the resulting sequence
93 Iteration (FLWORs)
Reordering via order by
In the FLWOR block
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 22[Gru08]
In the FLWOR block
the value (atomization) of e3 determines the order in which the bindingsof $v are used to evaluate e2
for $v in e1order by e3 [ascendingdescending][empty greatestleast]return e2
bull FLWOR Reordering examples
93 Iteration (FLWORs)
An order by no-op reordering by sequence order
for $x at $p in (53142)
order by $p
return $x
rArrrArrrArrrArr (53142)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 23[Gru08]
All bound variables in scope in order by
for $x at $p in (53142)
order by $p + $x
return $x
rArrrArrrArrrArr (13524)
Reordering as in SQLs ORDER BY
for $x at $p in (53142)
order by $x
return $x
rArrrArrrArrrArr (12345)
bull FLWOR Reordering examplesndash Value-based reordering of an XPath step result
93 Iteration (FLWORs)
This query reorders the result of the XPath location step descendantb
based on (string) value Which result is to be expectedlet $a = ltagt
ltb id=0gt42ltbgt
ltb id=1gt5ltbgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 24[Gru08]
ltb id=1gt5ltbgt
ltb id=2gt
ltb id=3gt3ltbgt
ltb id=4gt1ltbgt
ltagt
for $b in $adescendantb
order by $btext() empty greatest
return $bid
Answer
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 25
bull FLWOR semantics tuple spacendash In the W3C XQuery specification the interaction of the five clauses of a FLWOR (for-let-where-order by-return) block is formally explained by means of a tuple space
94 FLOWR Semantics
means of a tuple spacebull Size of tuple space equiv number of iterations performed by FLWOR block
bull The fields of the tuples represent for each iteration
1 forlet variable bindings
2 the outcome of the where clause
3 the value of the reordering criterion and
4 the value returned by the return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 26[Gru08]
bull FLWOR semantics tuple space (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y where order by return
10 1 100 false 4 (110)
1 Complete tuple space
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 27[Gru08]
return ($p$x) 10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (2)2 Filtering where clause ($y lt= 42)
94 FLOWR Semantics
$x $p $y where order by return
10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 28[Gru08]
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (3)3 Reordering order by clause
94 FLOWR Semantics
$x $p $y where order by return
1 10 1 true -5 (101)
2 9 4 true -4 (92)
3 8 9 true -3 (83)
4 To emit the final result scan the tuple space in the order specified by the order by column and concatenate the return column entries
(1019283746556)
ndash Observation some values have been computed but never used
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 29[Gru08]
3 8 9 true -3 (83)
4 7 16 true -2 (74)
5 6 25 true -1 (65)
6 5 36 true 0 (56)
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
92 Preliminaries
bull Comparisons General comparison examples
(123) gt (245) rArrrArrrArrrArr true()
(123) = 1 rArrrArrrArrrArr true()
() = 0 rArrrArrrArrrArr false()
2 lt= 1 rArrrArrrArrrArr false()
(123) = 3 rArrrArrrArrrArr true()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 7[Gru08]
ndash The six value comparison operators eq ne lt le ge gtcompare single items by value (atomization)
(123) = 3 rArrrArrrArrrArr true()
(12) = (12) rArrrArrrArrrArr true()
not((12) = (12)) rArrrArrrArrrArr false()
Value comparisons
2 gt 10 rArrrArrrArrrArr true()
ltxgt42ltxgt eq ltygt42ltygt rArrrArrrArrrArr true()
(01) eq 0 rArrrArrrArrrArr (type error)
bull More on comparisons
ndash Note The existential semantics of the general comparison operators may lead to unexpected behavior
92 Preliminaries
a) For an item-by-item comparison use deep-equal()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 8[Gru08]
Surprises
(123) = (13) rArrrArrrArrrArr true()a
(21) = 1 rArrrArrrArrrArr true() or (impl dependent)
bull Node comparisons
92 Preliminaries
based on identity and document order
e1 is e2 nodes e12 identical
e1 ltlt e2 node e1 before e2
e1 gtgt e2 node e1 after e2
ndash Node comparison examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 9[Gru08]
ltxgt42ltxgt eq ltxgt42ltxgt rArrrArrrArrrArr true()
ltxgt42ltxgt is ltxgt42ltxgt rArrrArrrArrrArr false()
root(e1) is root(e2) rArrrArrrArrrArr nodes e12 in same tree
let $a = ltxgtltygtltxgt
return $a ltlt $ayrArrrArrrArrrArr true()
bull Working with sequencesndash XQuery comes with an extensive library of builtinfunctions to perform common computations over sequences
bull Common sequence operations
92 Preliminaries
Function Example
count count((042)) rArrrArrrArrrArr 3
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 10[Gru08]
count count((042)) rArrrArrrArrrArr 3
max max((042)) rArrrArrrArrrArr 4
subsequence subsequence((1357)23) rArrrArrrArrrArr (357)
empty empty((042)) rArrrArrrArrrArr false()
exists exists((042)) rArrrArrrArrrArr true()
distinct-values distinct-values((4424)) rArrrArrrArrrArr (42)
to (1 to 10)[ mod 2 eq 1] rArrrArrrArrrArr (13579)
bull Arithmeticsndash Only a few words on arithmetics ndash XQuery meets the common expectation here Points to note1 Infix operators + - lowastlowastlowastlowast div idiv (integer division)2 operators first atomize their operands then perform
promotion to a common numeric type
92 Preliminaries
promotion to a common numeric type3 if at least one operand is () the result is ()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 11[Gru08]
Examples and pitfalls
ltxgt1ltxgt + 41 rArrrArrrArrrArr 420
() 42 rArrrArrrArrrArr ()
(12) - (23) rArrrArrrArrrArr (type error)
x-42 rArrrArrrArrrArr childx-42 (use x˽-˽42)
xy rArrrArrrArrrArr childxchildy (use x div y)
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)ndash Foor loop
ndash Variable bindings
9 XQuery
ndash Variable bindings
ndash Where clause
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 12
bull XQuery Iteration FLWORsndash Remember that XPath steps perform implicit iteration in cse evaluation of e is iterated with bound to each item in cs in turn
ndash XPath subexpressions aside iteration in XQuery is explicit via the FLWOR (flower) construct
93 Iteration (FLWORs)
explicit via the FLWOR (flower) constructbull The versatile FLWOR is used to express
ndash nested iteration
ndash joins between sequences (of nodes)
ndash groupings
ndash orderings beyond document order etc
bull In a sense FLWOR assumes the role of the SELECT-FROM-WHERE block in SQL
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 13[Gru08]
bull FLWOR Iteration via forin
ndash Explicit iteration
93 Iteration (FLWORs)
Explicit iteration is expressed using the forin construct a
for $v [at $p] in e1
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 14[Gru08]
for $v [at $p] in e1 return e2
If e1 evaluates to the sequence (x1xn) the loop body e2 is
evaluated n times with variable $v bound to each xi [and $pbound to i ] in order The results of these evaluations are concatenated to form a single sequence
a the construct at $p is optional
93 Iteration (FLWORs)
Iteration examples
for $x in (321)
return ($xlowastlowastlowastlowast)rArrrArrrArrrArr (3lowastlowastlowastlowast2lowastlowastlowastlowast1lowastlowastlowastlowast)
for $x in (321)
return $xlowastlowastlowastlowastrArrrArrrArrrArr (321lowastlowastlowastlowast)
for $x in (321)
return for $y in (ab) rArrrArrrArrrArr(3a3b
2a2b
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 15[Gru08]
return for $y in (ab)
return ($x$y)
rArrrArrrArrrArr 2a2b
1a1b)
FLWOR Abbreviations
for $v1 in e1return
for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1$v2 in e2
return e3
bull FLWOR Iteration via forin
93 Iteration (FLWORs)
Purpose of this query Qmax( for $i in csdescendant-or-self[not()]
return count($iancestor) )
A sample cs ldquoAnnotatedrdquo sample cs
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 16[Gru08]
A sample csa
b c
d
e g h
f
ldquoAnnotatedrdquo sample csa
b
e
c
d
g h
f
1
3 3 3
Answer
bull FLWOR Iteration via forin ndash Return every other item in sequence
93 Iteration (FLWORs)
These queries both return the items at odd positions in the inputsequence e
for $i in (1 to count(e))[ mod 2 eq 1]
bull Remember ebv(0) = false()ebv effective boolean value
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 17[Gru08]
for $i in (1 to count(e))[ mod 2 eq 1]
return e[$i]
for $i at $p in e
return if ($p mod 2)
then e[$p]
else ()
bull FLWOR Variable Binding via let =
ndash Note that in the examples on the last slide expression e is re-evaluated count(e) 2 times although e is constant in the loop
93 Iteration (FLWORs)
Variable bindingsThe result of evaluating an expression e may be bound to a variable $v
bull for and let clauses may be freely intermixed
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 18[Gru08]
Variable bindingsThe result of evaluating an expression e1 may be bound to a variable $vvia let
let $v = e1return e2
evaluates e2 with free occurrences of $v replaced by e1
bull FLWOR Variable Binding via letbull bull bull =
93 Iteration (FLWORs)
Iteration vs variable binding
for $x in (321)
return ($x)
rArrrArrrArrrArr (321
)
let $x = (321) rArrrArrrArrrArr (321)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 19[Gru08]
let $x = (321)
return ($x)
rArrrArrrArrrArr (321)
Every other item revisited (flip back two slides)The following hoists the constant e out of the loop body
let $seq = ereturn for $i at $p in $seq
return if ($p mod 2)
then $seq[$p]
else ()
bull Adding a where clause
ndash Inside loop bodies the idiom if (p) then e else ()
is so common that FLWOR comes with a SQL-like where clause to address this
93 Iteration (FLWORs)
A where clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 20[Gru08]
A where clauseIf ebv(p) evaluates to false() under the current variable bindings the
current iteration does not contribute to the result
for $v in e1where p
return e2
equiv
for $v in e1return if (p)
then e2else ()
bull Explicit vs implicit iterationndash XPath implicit iteration
ndash Equivalent nested FLWOR blocks
93 Iteration (FLWORs)
a[b = foo]c[2]d[e = 42]
for $a in a
ndash NB Unlike the XPath step operator for does not change the context item
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 21[Gru08]
for $a in a
where $ab = foo
return for $c at $p in $ac
where $p = 2
return for $d in $cd
where $de = 42
return $d
bull FLWOR Reorder iteration result via order byndash In a FLWOR block for $v in e1 return e2 the order of e1determines the order of the resulting sequence
93 Iteration (FLWORs)
Reordering via order by
In the FLWOR block
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 22[Gru08]
In the FLWOR block
the value (atomization) of e3 determines the order in which the bindingsof $v are used to evaluate e2
for $v in e1order by e3 [ascendingdescending][empty greatestleast]return e2
bull FLWOR Reordering examples
93 Iteration (FLWORs)
An order by no-op reordering by sequence order
for $x at $p in (53142)
order by $p
return $x
rArrrArrrArrrArr (53142)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 23[Gru08]
All bound variables in scope in order by
for $x at $p in (53142)
order by $p + $x
return $x
rArrrArrrArrrArr (13524)
Reordering as in SQLs ORDER BY
for $x at $p in (53142)
order by $x
return $x
rArrrArrrArrrArr (12345)
bull FLWOR Reordering examplesndash Value-based reordering of an XPath step result
93 Iteration (FLWORs)
This query reorders the result of the XPath location step descendantb
based on (string) value Which result is to be expectedlet $a = ltagt
ltb id=0gt42ltbgt
ltb id=1gt5ltbgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 24[Gru08]
ltb id=1gt5ltbgt
ltb id=2gt
ltb id=3gt3ltbgt
ltb id=4gt1ltbgt
ltagt
for $b in $adescendantb
order by $btext() empty greatest
return $bid
Answer
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 25
bull FLWOR semantics tuple spacendash In the W3C XQuery specification the interaction of the five clauses of a FLWOR (for-let-where-order by-return) block is formally explained by means of a tuple space
94 FLOWR Semantics
means of a tuple spacebull Size of tuple space equiv number of iterations performed by FLWOR block
bull The fields of the tuples represent for each iteration
1 forlet variable bindings
2 the outcome of the where clause
3 the value of the reordering criterion and
4 the value returned by the return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 26[Gru08]
bull FLWOR semantics tuple space (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y where order by return
10 1 100 false 4 (110)
1 Complete tuple space
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 27[Gru08]
return ($p$x) 10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (2)2 Filtering where clause ($y lt= 42)
94 FLOWR Semantics
$x $p $y where order by return
10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 28[Gru08]
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (3)3 Reordering order by clause
94 FLOWR Semantics
$x $p $y where order by return
1 10 1 true -5 (101)
2 9 4 true -4 (92)
3 8 9 true -3 (83)
4 To emit the final result scan the tuple space in the order specified by the order by column and concatenate the return column entries
(1019283746556)
ndash Observation some values have been computed but never used
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 29[Gru08]
3 8 9 true -3 (83)
4 7 16 true -2 (74)
5 6 25 true -1 (65)
6 5 36 true 0 (56)
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull More on comparisons
ndash Note The existential semantics of the general comparison operators may lead to unexpected behavior
92 Preliminaries
a) For an item-by-item comparison use deep-equal()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 8[Gru08]
Surprises
(123) = (13) rArrrArrrArrrArr true()a
(21) = 1 rArrrArrrArrrArr true() or (impl dependent)
bull Node comparisons
92 Preliminaries
based on identity and document order
e1 is e2 nodes e12 identical
e1 ltlt e2 node e1 before e2
e1 gtgt e2 node e1 after e2
ndash Node comparison examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 9[Gru08]
ltxgt42ltxgt eq ltxgt42ltxgt rArrrArrrArrrArr true()
ltxgt42ltxgt is ltxgt42ltxgt rArrrArrrArrrArr false()
root(e1) is root(e2) rArrrArrrArrrArr nodes e12 in same tree
let $a = ltxgtltygtltxgt
return $a ltlt $ayrArrrArrrArrrArr true()
bull Working with sequencesndash XQuery comes with an extensive library of builtinfunctions to perform common computations over sequences
bull Common sequence operations
92 Preliminaries
Function Example
count count((042)) rArrrArrrArrrArr 3
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 10[Gru08]
count count((042)) rArrrArrrArrrArr 3
max max((042)) rArrrArrrArrrArr 4
subsequence subsequence((1357)23) rArrrArrrArrrArr (357)
empty empty((042)) rArrrArrrArrrArr false()
exists exists((042)) rArrrArrrArrrArr true()
distinct-values distinct-values((4424)) rArrrArrrArrrArr (42)
to (1 to 10)[ mod 2 eq 1] rArrrArrrArrrArr (13579)
bull Arithmeticsndash Only a few words on arithmetics ndash XQuery meets the common expectation here Points to note1 Infix operators + - lowastlowastlowastlowast div idiv (integer division)2 operators first atomize their operands then perform
promotion to a common numeric type
92 Preliminaries
promotion to a common numeric type3 if at least one operand is () the result is ()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 11[Gru08]
Examples and pitfalls
ltxgt1ltxgt + 41 rArrrArrrArrrArr 420
() 42 rArrrArrrArrrArr ()
(12) - (23) rArrrArrrArrrArr (type error)
x-42 rArrrArrrArrrArr childx-42 (use x˽-˽42)
xy rArrrArrrArrrArr childxchildy (use x div y)
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)ndash Foor loop
ndash Variable bindings
9 XQuery
ndash Variable bindings
ndash Where clause
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 12
bull XQuery Iteration FLWORsndash Remember that XPath steps perform implicit iteration in cse evaluation of e is iterated with bound to each item in cs in turn
ndash XPath subexpressions aside iteration in XQuery is explicit via the FLWOR (flower) construct
93 Iteration (FLWORs)
explicit via the FLWOR (flower) constructbull The versatile FLWOR is used to express
ndash nested iteration
ndash joins between sequences (of nodes)
ndash groupings
ndash orderings beyond document order etc
bull In a sense FLWOR assumes the role of the SELECT-FROM-WHERE block in SQL
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 13[Gru08]
bull FLWOR Iteration via forin
ndash Explicit iteration
93 Iteration (FLWORs)
Explicit iteration is expressed using the forin construct a
for $v [at $p] in e1
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 14[Gru08]
for $v [at $p] in e1 return e2
If e1 evaluates to the sequence (x1xn) the loop body e2 is
evaluated n times with variable $v bound to each xi [and $pbound to i ] in order The results of these evaluations are concatenated to form a single sequence
a the construct at $p is optional
93 Iteration (FLWORs)
Iteration examples
for $x in (321)
return ($xlowastlowastlowastlowast)rArrrArrrArrrArr (3lowastlowastlowastlowast2lowastlowastlowastlowast1lowastlowastlowastlowast)
for $x in (321)
return $xlowastlowastlowastlowastrArrrArrrArrrArr (321lowastlowastlowastlowast)
for $x in (321)
return for $y in (ab) rArrrArrrArrrArr(3a3b
2a2b
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 15[Gru08]
return for $y in (ab)
return ($x$y)
rArrrArrrArrrArr 2a2b
1a1b)
FLWOR Abbreviations
for $v1 in e1return
for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1$v2 in e2
return e3
bull FLWOR Iteration via forin
93 Iteration (FLWORs)
Purpose of this query Qmax( for $i in csdescendant-or-self[not()]
return count($iancestor) )
A sample cs ldquoAnnotatedrdquo sample cs
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 16[Gru08]
A sample csa
b c
d
e g h
f
ldquoAnnotatedrdquo sample csa
b
e
c
d
g h
f
1
3 3 3
Answer
bull FLWOR Iteration via forin ndash Return every other item in sequence
93 Iteration (FLWORs)
These queries both return the items at odd positions in the inputsequence e
for $i in (1 to count(e))[ mod 2 eq 1]
bull Remember ebv(0) = false()ebv effective boolean value
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 17[Gru08]
for $i in (1 to count(e))[ mod 2 eq 1]
return e[$i]
for $i at $p in e
return if ($p mod 2)
then e[$p]
else ()
bull FLWOR Variable Binding via let =
ndash Note that in the examples on the last slide expression e is re-evaluated count(e) 2 times although e is constant in the loop
93 Iteration (FLWORs)
Variable bindingsThe result of evaluating an expression e may be bound to a variable $v
bull for and let clauses may be freely intermixed
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 18[Gru08]
Variable bindingsThe result of evaluating an expression e1 may be bound to a variable $vvia let
let $v = e1return e2
evaluates e2 with free occurrences of $v replaced by e1
bull FLWOR Variable Binding via letbull bull bull =
93 Iteration (FLWORs)
Iteration vs variable binding
for $x in (321)
return ($x)
rArrrArrrArrrArr (321
)
let $x = (321) rArrrArrrArrrArr (321)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 19[Gru08]
let $x = (321)
return ($x)
rArrrArrrArrrArr (321)
Every other item revisited (flip back two slides)The following hoists the constant e out of the loop body
let $seq = ereturn for $i at $p in $seq
return if ($p mod 2)
then $seq[$p]
else ()
bull Adding a where clause
ndash Inside loop bodies the idiom if (p) then e else ()
is so common that FLWOR comes with a SQL-like where clause to address this
93 Iteration (FLWORs)
A where clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 20[Gru08]
A where clauseIf ebv(p) evaluates to false() under the current variable bindings the
current iteration does not contribute to the result
for $v in e1where p
return e2
equiv
for $v in e1return if (p)
then e2else ()
bull Explicit vs implicit iterationndash XPath implicit iteration
ndash Equivalent nested FLWOR blocks
93 Iteration (FLWORs)
a[b = foo]c[2]d[e = 42]
for $a in a
ndash NB Unlike the XPath step operator for does not change the context item
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 21[Gru08]
for $a in a
where $ab = foo
return for $c at $p in $ac
where $p = 2
return for $d in $cd
where $de = 42
return $d
bull FLWOR Reorder iteration result via order byndash In a FLWOR block for $v in e1 return e2 the order of e1determines the order of the resulting sequence
93 Iteration (FLWORs)
Reordering via order by
In the FLWOR block
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 22[Gru08]
In the FLWOR block
the value (atomization) of e3 determines the order in which the bindingsof $v are used to evaluate e2
for $v in e1order by e3 [ascendingdescending][empty greatestleast]return e2
bull FLWOR Reordering examples
93 Iteration (FLWORs)
An order by no-op reordering by sequence order
for $x at $p in (53142)
order by $p
return $x
rArrrArrrArrrArr (53142)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 23[Gru08]
All bound variables in scope in order by
for $x at $p in (53142)
order by $p + $x
return $x
rArrrArrrArrrArr (13524)
Reordering as in SQLs ORDER BY
for $x at $p in (53142)
order by $x
return $x
rArrrArrrArrrArr (12345)
bull FLWOR Reordering examplesndash Value-based reordering of an XPath step result
93 Iteration (FLWORs)
This query reorders the result of the XPath location step descendantb
based on (string) value Which result is to be expectedlet $a = ltagt
ltb id=0gt42ltbgt
ltb id=1gt5ltbgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 24[Gru08]
ltb id=1gt5ltbgt
ltb id=2gt
ltb id=3gt3ltbgt
ltb id=4gt1ltbgt
ltagt
for $b in $adescendantb
order by $btext() empty greatest
return $bid
Answer
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 25
bull FLWOR semantics tuple spacendash In the W3C XQuery specification the interaction of the five clauses of a FLWOR (for-let-where-order by-return) block is formally explained by means of a tuple space
94 FLOWR Semantics
means of a tuple spacebull Size of tuple space equiv number of iterations performed by FLWOR block
bull The fields of the tuples represent for each iteration
1 forlet variable bindings
2 the outcome of the where clause
3 the value of the reordering criterion and
4 the value returned by the return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 26[Gru08]
bull FLWOR semantics tuple space (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y where order by return
10 1 100 false 4 (110)
1 Complete tuple space
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 27[Gru08]
return ($p$x) 10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (2)2 Filtering where clause ($y lt= 42)
94 FLOWR Semantics
$x $p $y where order by return
10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 28[Gru08]
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (3)3 Reordering order by clause
94 FLOWR Semantics
$x $p $y where order by return
1 10 1 true -5 (101)
2 9 4 true -4 (92)
3 8 9 true -3 (83)
4 To emit the final result scan the tuple space in the order specified by the order by column and concatenate the return column entries
(1019283746556)
ndash Observation some values have been computed but never used
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 29[Gru08]
3 8 9 true -3 (83)
4 7 16 true -2 (74)
5 6 25 true -1 (65)
6 5 36 true 0 (56)
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull Node comparisons
92 Preliminaries
based on identity and document order
e1 is e2 nodes e12 identical
e1 ltlt e2 node e1 before e2
e1 gtgt e2 node e1 after e2
ndash Node comparison examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 9[Gru08]
ltxgt42ltxgt eq ltxgt42ltxgt rArrrArrrArrrArr true()
ltxgt42ltxgt is ltxgt42ltxgt rArrrArrrArrrArr false()
root(e1) is root(e2) rArrrArrrArrrArr nodes e12 in same tree
let $a = ltxgtltygtltxgt
return $a ltlt $ayrArrrArrrArrrArr true()
bull Working with sequencesndash XQuery comes with an extensive library of builtinfunctions to perform common computations over sequences
bull Common sequence operations
92 Preliminaries
Function Example
count count((042)) rArrrArrrArrrArr 3
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 10[Gru08]
count count((042)) rArrrArrrArrrArr 3
max max((042)) rArrrArrrArrrArr 4
subsequence subsequence((1357)23) rArrrArrrArrrArr (357)
empty empty((042)) rArrrArrrArrrArr false()
exists exists((042)) rArrrArrrArrrArr true()
distinct-values distinct-values((4424)) rArrrArrrArrrArr (42)
to (1 to 10)[ mod 2 eq 1] rArrrArrrArrrArr (13579)
bull Arithmeticsndash Only a few words on arithmetics ndash XQuery meets the common expectation here Points to note1 Infix operators + - lowastlowastlowastlowast div idiv (integer division)2 operators first atomize their operands then perform
promotion to a common numeric type
92 Preliminaries
promotion to a common numeric type3 if at least one operand is () the result is ()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 11[Gru08]
Examples and pitfalls
ltxgt1ltxgt + 41 rArrrArrrArrrArr 420
() 42 rArrrArrrArrrArr ()
(12) - (23) rArrrArrrArrrArr (type error)
x-42 rArrrArrrArrrArr childx-42 (use x˽-˽42)
xy rArrrArrrArrrArr childxchildy (use x div y)
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)ndash Foor loop
ndash Variable bindings
9 XQuery
ndash Variable bindings
ndash Where clause
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 12
bull XQuery Iteration FLWORsndash Remember that XPath steps perform implicit iteration in cse evaluation of e is iterated with bound to each item in cs in turn
ndash XPath subexpressions aside iteration in XQuery is explicit via the FLWOR (flower) construct
93 Iteration (FLWORs)
explicit via the FLWOR (flower) constructbull The versatile FLWOR is used to express
ndash nested iteration
ndash joins between sequences (of nodes)
ndash groupings
ndash orderings beyond document order etc
bull In a sense FLWOR assumes the role of the SELECT-FROM-WHERE block in SQL
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 13[Gru08]
bull FLWOR Iteration via forin
ndash Explicit iteration
93 Iteration (FLWORs)
Explicit iteration is expressed using the forin construct a
for $v [at $p] in e1
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 14[Gru08]
for $v [at $p] in e1 return e2
If e1 evaluates to the sequence (x1xn) the loop body e2 is
evaluated n times with variable $v bound to each xi [and $pbound to i ] in order The results of these evaluations are concatenated to form a single sequence
a the construct at $p is optional
93 Iteration (FLWORs)
Iteration examples
for $x in (321)
return ($xlowastlowastlowastlowast)rArrrArrrArrrArr (3lowastlowastlowastlowast2lowastlowastlowastlowast1lowastlowastlowastlowast)
for $x in (321)
return $xlowastlowastlowastlowastrArrrArrrArrrArr (321lowastlowastlowastlowast)
for $x in (321)
return for $y in (ab) rArrrArrrArrrArr(3a3b
2a2b
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 15[Gru08]
return for $y in (ab)
return ($x$y)
rArrrArrrArrrArr 2a2b
1a1b)
FLWOR Abbreviations
for $v1 in e1return
for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1$v2 in e2
return e3
bull FLWOR Iteration via forin
93 Iteration (FLWORs)
Purpose of this query Qmax( for $i in csdescendant-or-self[not()]
return count($iancestor) )
A sample cs ldquoAnnotatedrdquo sample cs
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 16[Gru08]
A sample csa
b c
d
e g h
f
ldquoAnnotatedrdquo sample csa
b
e
c
d
g h
f
1
3 3 3
Answer
bull FLWOR Iteration via forin ndash Return every other item in sequence
93 Iteration (FLWORs)
These queries both return the items at odd positions in the inputsequence e
for $i in (1 to count(e))[ mod 2 eq 1]
bull Remember ebv(0) = false()ebv effective boolean value
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 17[Gru08]
for $i in (1 to count(e))[ mod 2 eq 1]
return e[$i]
for $i at $p in e
return if ($p mod 2)
then e[$p]
else ()
bull FLWOR Variable Binding via let =
ndash Note that in the examples on the last slide expression e is re-evaluated count(e) 2 times although e is constant in the loop
93 Iteration (FLWORs)
Variable bindingsThe result of evaluating an expression e may be bound to a variable $v
bull for and let clauses may be freely intermixed
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 18[Gru08]
Variable bindingsThe result of evaluating an expression e1 may be bound to a variable $vvia let
let $v = e1return e2
evaluates e2 with free occurrences of $v replaced by e1
bull FLWOR Variable Binding via letbull bull bull =
93 Iteration (FLWORs)
Iteration vs variable binding
for $x in (321)
return ($x)
rArrrArrrArrrArr (321
)
let $x = (321) rArrrArrrArrrArr (321)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 19[Gru08]
let $x = (321)
return ($x)
rArrrArrrArrrArr (321)
Every other item revisited (flip back two slides)The following hoists the constant e out of the loop body
let $seq = ereturn for $i at $p in $seq
return if ($p mod 2)
then $seq[$p]
else ()
bull Adding a where clause
ndash Inside loop bodies the idiom if (p) then e else ()
is so common that FLWOR comes with a SQL-like where clause to address this
93 Iteration (FLWORs)
A where clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 20[Gru08]
A where clauseIf ebv(p) evaluates to false() under the current variable bindings the
current iteration does not contribute to the result
for $v in e1where p
return e2
equiv
for $v in e1return if (p)
then e2else ()
bull Explicit vs implicit iterationndash XPath implicit iteration
ndash Equivalent nested FLWOR blocks
93 Iteration (FLWORs)
a[b = foo]c[2]d[e = 42]
for $a in a
ndash NB Unlike the XPath step operator for does not change the context item
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 21[Gru08]
for $a in a
where $ab = foo
return for $c at $p in $ac
where $p = 2
return for $d in $cd
where $de = 42
return $d
bull FLWOR Reorder iteration result via order byndash In a FLWOR block for $v in e1 return e2 the order of e1determines the order of the resulting sequence
93 Iteration (FLWORs)
Reordering via order by
In the FLWOR block
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 22[Gru08]
In the FLWOR block
the value (atomization) of e3 determines the order in which the bindingsof $v are used to evaluate e2
for $v in e1order by e3 [ascendingdescending][empty greatestleast]return e2
bull FLWOR Reordering examples
93 Iteration (FLWORs)
An order by no-op reordering by sequence order
for $x at $p in (53142)
order by $p
return $x
rArrrArrrArrrArr (53142)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 23[Gru08]
All bound variables in scope in order by
for $x at $p in (53142)
order by $p + $x
return $x
rArrrArrrArrrArr (13524)
Reordering as in SQLs ORDER BY
for $x at $p in (53142)
order by $x
return $x
rArrrArrrArrrArr (12345)
bull FLWOR Reordering examplesndash Value-based reordering of an XPath step result
93 Iteration (FLWORs)
This query reorders the result of the XPath location step descendantb
based on (string) value Which result is to be expectedlet $a = ltagt
ltb id=0gt42ltbgt
ltb id=1gt5ltbgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 24[Gru08]
ltb id=1gt5ltbgt
ltb id=2gt
ltb id=3gt3ltbgt
ltb id=4gt1ltbgt
ltagt
for $b in $adescendantb
order by $btext() empty greatest
return $bid
Answer
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 25
bull FLWOR semantics tuple spacendash In the W3C XQuery specification the interaction of the five clauses of a FLWOR (for-let-where-order by-return) block is formally explained by means of a tuple space
94 FLOWR Semantics
means of a tuple spacebull Size of tuple space equiv number of iterations performed by FLWOR block
bull The fields of the tuples represent for each iteration
1 forlet variable bindings
2 the outcome of the where clause
3 the value of the reordering criterion and
4 the value returned by the return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 26[Gru08]
bull FLWOR semantics tuple space (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y where order by return
10 1 100 false 4 (110)
1 Complete tuple space
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 27[Gru08]
return ($p$x) 10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (2)2 Filtering where clause ($y lt= 42)
94 FLOWR Semantics
$x $p $y where order by return
10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 28[Gru08]
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (3)3 Reordering order by clause
94 FLOWR Semantics
$x $p $y where order by return
1 10 1 true -5 (101)
2 9 4 true -4 (92)
3 8 9 true -3 (83)
4 To emit the final result scan the tuple space in the order specified by the order by column and concatenate the return column entries
(1019283746556)
ndash Observation some values have been computed but never used
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 29[Gru08]
3 8 9 true -3 (83)
4 7 16 true -2 (74)
5 6 25 true -1 (65)
6 5 36 true 0 (56)
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull Working with sequencesndash XQuery comes with an extensive library of builtinfunctions to perform common computations over sequences
bull Common sequence operations
92 Preliminaries
Function Example
count count((042)) rArrrArrrArrrArr 3
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 10[Gru08]
count count((042)) rArrrArrrArrrArr 3
max max((042)) rArrrArrrArrrArr 4
subsequence subsequence((1357)23) rArrrArrrArrrArr (357)
empty empty((042)) rArrrArrrArrrArr false()
exists exists((042)) rArrrArrrArrrArr true()
distinct-values distinct-values((4424)) rArrrArrrArrrArr (42)
to (1 to 10)[ mod 2 eq 1] rArrrArrrArrrArr (13579)
bull Arithmeticsndash Only a few words on arithmetics ndash XQuery meets the common expectation here Points to note1 Infix operators + - lowastlowastlowastlowast div idiv (integer division)2 operators first atomize their operands then perform
promotion to a common numeric type
92 Preliminaries
promotion to a common numeric type3 if at least one operand is () the result is ()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 11[Gru08]
Examples and pitfalls
ltxgt1ltxgt + 41 rArrrArrrArrrArr 420
() 42 rArrrArrrArrrArr ()
(12) - (23) rArrrArrrArrrArr (type error)
x-42 rArrrArrrArrrArr childx-42 (use x˽-˽42)
xy rArrrArrrArrrArr childxchildy (use x div y)
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)ndash Foor loop
ndash Variable bindings
9 XQuery
ndash Variable bindings
ndash Where clause
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 12
bull XQuery Iteration FLWORsndash Remember that XPath steps perform implicit iteration in cse evaluation of e is iterated with bound to each item in cs in turn
ndash XPath subexpressions aside iteration in XQuery is explicit via the FLWOR (flower) construct
93 Iteration (FLWORs)
explicit via the FLWOR (flower) constructbull The versatile FLWOR is used to express
ndash nested iteration
ndash joins between sequences (of nodes)
ndash groupings
ndash orderings beyond document order etc
bull In a sense FLWOR assumes the role of the SELECT-FROM-WHERE block in SQL
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 13[Gru08]
bull FLWOR Iteration via forin
ndash Explicit iteration
93 Iteration (FLWORs)
Explicit iteration is expressed using the forin construct a
for $v [at $p] in e1
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 14[Gru08]
for $v [at $p] in e1 return e2
If e1 evaluates to the sequence (x1xn) the loop body e2 is
evaluated n times with variable $v bound to each xi [and $pbound to i ] in order The results of these evaluations are concatenated to form a single sequence
a the construct at $p is optional
93 Iteration (FLWORs)
Iteration examples
for $x in (321)
return ($xlowastlowastlowastlowast)rArrrArrrArrrArr (3lowastlowastlowastlowast2lowastlowastlowastlowast1lowastlowastlowastlowast)
for $x in (321)
return $xlowastlowastlowastlowastrArrrArrrArrrArr (321lowastlowastlowastlowast)
for $x in (321)
return for $y in (ab) rArrrArrrArrrArr(3a3b
2a2b
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 15[Gru08]
return for $y in (ab)
return ($x$y)
rArrrArrrArrrArr 2a2b
1a1b)
FLWOR Abbreviations
for $v1 in e1return
for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1$v2 in e2
return e3
bull FLWOR Iteration via forin
93 Iteration (FLWORs)
Purpose of this query Qmax( for $i in csdescendant-or-self[not()]
return count($iancestor) )
A sample cs ldquoAnnotatedrdquo sample cs
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 16[Gru08]
A sample csa
b c
d
e g h
f
ldquoAnnotatedrdquo sample csa
b
e
c
d
g h
f
1
3 3 3
Answer
bull FLWOR Iteration via forin ndash Return every other item in sequence
93 Iteration (FLWORs)
These queries both return the items at odd positions in the inputsequence e
for $i in (1 to count(e))[ mod 2 eq 1]
bull Remember ebv(0) = false()ebv effective boolean value
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 17[Gru08]
for $i in (1 to count(e))[ mod 2 eq 1]
return e[$i]
for $i at $p in e
return if ($p mod 2)
then e[$p]
else ()
bull FLWOR Variable Binding via let =
ndash Note that in the examples on the last slide expression e is re-evaluated count(e) 2 times although e is constant in the loop
93 Iteration (FLWORs)
Variable bindingsThe result of evaluating an expression e may be bound to a variable $v
bull for and let clauses may be freely intermixed
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 18[Gru08]
Variable bindingsThe result of evaluating an expression e1 may be bound to a variable $vvia let
let $v = e1return e2
evaluates e2 with free occurrences of $v replaced by e1
bull FLWOR Variable Binding via letbull bull bull =
93 Iteration (FLWORs)
Iteration vs variable binding
for $x in (321)
return ($x)
rArrrArrrArrrArr (321
)
let $x = (321) rArrrArrrArrrArr (321)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 19[Gru08]
let $x = (321)
return ($x)
rArrrArrrArrrArr (321)
Every other item revisited (flip back two slides)The following hoists the constant e out of the loop body
let $seq = ereturn for $i at $p in $seq
return if ($p mod 2)
then $seq[$p]
else ()
bull Adding a where clause
ndash Inside loop bodies the idiom if (p) then e else ()
is so common that FLWOR comes with a SQL-like where clause to address this
93 Iteration (FLWORs)
A where clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 20[Gru08]
A where clauseIf ebv(p) evaluates to false() under the current variable bindings the
current iteration does not contribute to the result
for $v in e1where p
return e2
equiv
for $v in e1return if (p)
then e2else ()
bull Explicit vs implicit iterationndash XPath implicit iteration
ndash Equivalent nested FLWOR blocks
93 Iteration (FLWORs)
a[b = foo]c[2]d[e = 42]
for $a in a
ndash NB Unlike the XPath step operator for does not change the context item
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 21[Gru08]
for $a in a
where $ab = foo
return for $c at $p in $ac
where $p = 2
return for $d in $cd
where $de = 42
return $d
bull FLWOR Reorder iteration result via order byndash In a FLWOR block for $v in e1 return e2 the order of e1determines the order of the resulting sequence
93 Iteration (FLWORs)
Reordering via order by
In the FLWOR block
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 22[Gru08]
In the FLWOR block
the value (atomization) of e3 determines the order in which the bindingsof $v are used to evaluate e2
for $v in e1order by e3 [ascendingdescending][empty greatestleast]return e2
bull FLWOR Reordering examples
93 Iteration (FLWORs)
An order by no-op reordering by sequence order
for $x at $p in (53142)
order by $p
return $x
rArrrArrrArrrArr (53142)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 23[Gru08]
All bound variables in scope in order by
for $x at $p in (53142)
order by $p + $x
return $x
rArrrArrrArrrArr (13524)
Reordering as in SQLs ORDER BY
for $x at $p in (53142)
order by $x
return $x
rArrrArrrArrrArr (12345)
bull FLWOR Reordering examplesndash Value-based reordering of an XPath step result
93 Iteration (FLWORs)
This query reorders the result of the XPath location step descendantb
based on (string) value Which result is to be expectedlet $a = ltagt
ltb id=0gt42ltbgt
ltb id=1gt5ltbgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 24[Gru08]
ltb id=1gt5ltbgt
ltb id=2gt
ltb id=3gt3ltbgt
ltb id=4gt1ltbgt
ltagt
for $b in $adescendantb
order by $btext() empty greatest
return $bid
Answer
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 25
bull FLWOR semantics tuple spacendash In the W3C XQuery specification the interaction of the five clauses of a FLWOR (for-let-where-order by-return) block is formally explained by means of a tuple space
94 FLOWR Semantics
means of a tuple spacebull Size of tuple space equiv number of iterations performed by FLWOR block
bull The fields of the tuples represent for each iteration
1 forlet variable bindings
2 the outcome of the where clause
3 the value of the reordering criterion and
4 the value returned by the return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 26[Gru08]
bull FLWOR semantics tuple space (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y where order by return
10 1 100 false 4 (110)
1 Complete tuple space
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 27[Gru08]
return ($p$x) 10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (2)2 Filtering where clause ($y lt= 42)
94 FLOWR Semantics
$x $p $y where order by return
10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 28[Gru08]
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (3)3 Reordering order by clause
94 FLOWR Semantics
$x $p $y where order by return
1 10 1 true -5 (101)
2 9 4 true -4 (92)
3 8 9 true -3 (83)
4 To emit the final result scan the tuple space in the order specified by the order by column and concatenate the return column entries
(1019283746556)
ndash Observation some values have been computed but never used
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 29[Gru08]
3 8 9 true -3 (83)
4 7 16 true -2 (74)
5 6 25 true -1 (65)
6 5 36 true 0 (56)
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull Arithmeticsndash Only a few words on arithmetics ndash XQuery meets the common expectation here Points to note1 Infix operators + - lowastlowastlowastlowast div idiv (integer division)2 operators first atomize their operands then perform
promotion to a common numeric type
92 Preliminaries
promotion to a common numeric type3 if at least one operand is () the result is ()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 11[Gru08]
Examples and pitfalls
ltxgt1ltxgt + 41 rArrrArrrArrrArr 420
() 42 rArrrArrrArrrArr ()
(12) - (23) rArrrArrrArrrArr (type error)
x-42 rArrrArrrArrrArr childx-42 (use x˽-˽42)
xy rArrrArrrArrrArr childxchildy (use x div y)
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)ndash Foor loop
ndash Variable bindings
9 XQuery
ndash Variable bindings
ndash Where clause
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 12
bull XQuery Iteration FLWORsndash Remember that XPath steps perform implicit iteration in cse evaluation of e is iterated with bound to each item in cs in turn
ndash XPath subexpressions aside iteration in XQuery is explicit via the FLWOR (flower) construct
93 Iteration (FLWORs)
explicit via the FLWOR (flower) constructbull The versatile FLWOR is used to express
ndash nested iteration
ndash joins between sequences (of nodes)
ndash groupings
ndash orderings beyond document order etc
bull In a sense FLWOR assumes the role of the SELECT-FROM-WHERE block in SQL
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 13[Gru08]
bull FLWOR Iteration via forin
ndash Explicit iteration
93 Iteration (FLWORs)
Explicit iteration is expressed using the forin construct a
for $v [at $p] in e1
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 14[Gru08]
for $v [at $p] in e1 return e2
If e1 evaluates to the sequence (x1xn) the loop body e2 is
evaluated n times with variable $v bound to each xi [and $pbound to i ] in order The results of these evaluations are concatenated to form a single sequence
a the construct at $p is optional
93 Iteration (FLWORs)
Iteration examples
for $x in (321)
return ($xlowastlowastlowastlowast)rArrrArrrArrrArr (3lowastlowastlowastlowast2lowastlowastlowastlowast1lowastlowastlowastlowast)
for $x in (321)
return $xlowastlowastlowastlowastrArrrArrrArrrArr (321lowastlowastlowastlowast)
for $x in (321)
return for $y in (ab) rArrrArrrArrrArr(3a3b
2a2b
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 15[Gru08]
return for $y in (ab)
return ($x$y)
rArrrArrrArrrArr 2a2b
1a1b)
FLWOR Abbreviations
for $v1 in e1return
for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1$v2 in e2
return e3
bull FLWOR Iteration via forin
93 Iteration (FLWORs)
Purpose of this query Qmax( for $i in csdescendant-or-self[not()]
return count($iancestor) )
A sample cs ldquoAnnotatedrdquo sample cs
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 16[Gru08]
A sample csa
b c
d
e g h
f
ldquoAnnotatedrdquo sample csa
b
e
c
d
g h
f
1
3 3 3
Answer
bull FLWOR Iteration via forin ndash Return every other item in sequence
93 Iteration (FLWORs)
These queries both return the items at odd positions in the inputsequence e
for $i in (1 to count(e))[ mod 2 eq 1]
bull Remember ebv(0) = false()ebv effective boolean value
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 17[Gru08]
for $i in (1 to count(e))[ mod 2 eq 1]
return e[$i]
for $i at $p in e
return if ($p mod 2)
then e[$p]
else ()
bull FLWOR Variable Binding via let =
ndash Note that in the examples on the last slide expression e is re-evaluated count(e) 2 times although e is constant in the loop
93 Iteration (FLWORs)
Variable bindingsThe result of evaluating an expression e may be bound to a variable $v
bull for and let clauses may be freely intermixed
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 18[Gru08]
Variable bindingsThe result of evaluating an expression e1 may be bound to a variable $vvia let
let $v = e1return e2
evaluates e2 with free occurrences of $v replaced by e1
bull FLWOR Variable Binding via letbull bull bull =
93 Iteration (FLWORs)
Iteration vs variable binding
for $x in (321)
return ($x)
rArrrArrrArrrArr (321
)
let $x = (321) rArrrArrrArrrArr (321)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 19[Gru08]
let $x = (321)
return ($x)
rArrrArrrArrrArr (321)
Every other item revisited (flip back two slides)The following hoists the constant e out of the loop body
let $seq = ereturn for $i at $p in $seq
return if ($p mod 2)
then $seq[$p]
else ()
bull Adding a where clause
ndash Inside loop bodies the idiom if (p) then e else ()
is so common that FLWOR comes with a SQL-like where clause to address this
93 Iteration (FLWORs)
A where clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 20[Gru08]
A where clauseIf ebv(p) evaluates to false() under the current variable bindings the
current iteration does not contribute to the result
for $v in e1where p
return e2
equiv
for $v in e1return if (p)
then e2else ()
bull Explicit vs implicit iterationndash XPath implicit iteration
ndash Equivalent nested FLWOR blocks
93 Iteration (FLWORs)
a[b = foo]c[2]d[e = 42]
for $a in a
ndash NB Unlike the XPath step operator for does not change the context item
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 21[Gru08]
for $a in a
where $ab = foo
return for $c at $p in $ac
where $p = 2
return for $d in $cd
where $de = 42
return $d
bull FLWOR Reorder iteration result via order byndash In a FLWOR block for $v in e1 return e2 the order of e1determines the order of the resulting sequence
93 Iteration (FLWORs)
Reordering via order by
In the FLWOR block
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 22[Gru08]
In the FLWOR block
the value (atomization) of e3 determines the order in which the bindingsof $v are used to evaluate e2
for $v in e1order by e3 [ascendingdescending][empty greatestleast]return e2
bull FLWOR Reordering examples
93 Iteration (FLWORs)
An order by no-op reordering by sequence order
for $x at $p in (53142)
order by $p
return $x
rArrrArrrArrrArr (53142)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 23[Gru08]
All bound variables in scope in order by
for $x at $p in (53142)
order by $p + $x
return $x
rArrrArrrArrrArr (13524)
Reordering as in SQLs ORDER BY
for $x at $p in (53142)
order by $x
return $x
rArrrArrrArrrArr (12345)
bull FLWOR Reordering examplesndash Value-based reordering of an XPath step result
93 Iteration (FLWORs)
This query reorders the result of the XPath location step descendantb
based on (string) value Which result is to be expectedlet $a = ltagt
ltb id=0gt42ltbgt
ltb id=1gt5ltbgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 24[Gru08]
ltb id=1gt5ltbgt
ltb id=2gt
ltb id=3gt3ltbgt
ltb id=4gt1ltbgt
ltagt
for $b in $adescendantb
order by $btext() empty greatest
return $bid
Answer
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 25
bull FLWOR semantics tuple spacendash In the W3C XQuery specification the interaction of the five clauses of a FLWOR (for-let-where-order by-return) block is formally explained by means of a tuple space
94 FLOWR Semantics
means of a tuple spacebull Size of tuple space equiv number of iterations performed by FLWOR block
bull The fields of the tuples represent for each iteration
1 forlet variable bindings
2 the outcome of the where clause
3 the value of the reordering criterion and
4 the value returned by the return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 26[Gru08]
bull FLWOR semantics tuple space (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y where order by return
10 1 100 false 4 (110)
1 Complete tuple space
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 27[Gru08]
return ($p$x) 10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (2)2 Filtering where clause ($y lt= 42)
94 FLOWR Semantics
$x $p $y where order by return
10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 28[Gru08]
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (3)3 Reordering order by clause
94 FLOWR Semantics
$x $p $y where order by return
1 10 1 true -5 (101)
2 9 4 true -4 (92)
3 8 9 true -3 (83)
4 To emit the final result scan the tuple space in the order specified by the order by column and concatenate the return column entries
(1019283746556)
ndash Observation some values have been computed but never used
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 29[Gru08]
3 8 9 true -3 (83)
4 7 16 true -2 (74)
5 6 25 true -1 (65)
6 5 36 true 0 (56)
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)ndash Foor loop
ndash Variable bindings
9 XQuery
ndash Variable bindings
ndash Where clause
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 12
bull XQuery Iteration FLWORsndash Remember that XPath steps perform implicit iteration in cse evaluation of e is iterated with bound to each item in cs in turn
ndash XPath subexpressions aside iteration in XQuery is explicit via the FLWOR (flower) construct
93 Iteration (FLWORs)
explicit via the FLWOR (flower) constructbull The versatile FLWOR is used to express
ndash nested iteration
ndash joins between sequences (of nodes)
ndash groupings
ndash orderings beyond document order etc
bull In a sense FLWOR assumes the role of the SELECT-FROM-WHERE block in SQL
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 13[Gru08]
bull FLWOR Iteration via forin
ndash Explicit iteration
93 Iteration (FLWORs)
Explicit iteration is expressed using the forin construct a
for $v [at $p] in e1
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 14[Gru08]
for $v [at $p] in e1 return e2
If e1 evaluates to the sequence (x1xn) the loop body e2 is
evaluated n times with variable $v bound to each xi [and $pbound to i ] in order The results of these evaluations are concatenated to form a single sequence
a the construct at $p is optional
93 Iteration (FLWORs)
Iteration examples
for $x in (321)
return ($xlowastlowastlowastlowast)rArrrArrrArrrArr (3lowastlowastlowastlowast2lowastlowastlowastlowast1lowastlowastlowastlowast)
for $x in (321)
return $xlowastlowastlowastlowastrArrrArrrArrrArr (321lowastlowastlowastlowast)
for $x in (321)
return for $y in (ab) rArrrArrrArrrArr(3a3b
2a2b
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 15[Gru08]
return for $y in (ab)
return ($x$y)
rArrrArrrArrrArr 2a2b
1a1b)
FLWOR Abbreviations
for $v1 in e1return
for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1$v2 in e2
return e3
bull FLWOR Iteration via forin
93 Iteration (FLWORs)
Purpose of this query Qmax( for $i in csdescendant-or-self[not()]
return count($iancestor) )
A sample cs ldquoAnnotatedrdquo sample cs
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 16[Gru08]
A sample csa
b c
d
e g h
f
ldquoAnnotatedrdquo sample csa
b
e
c
d
g h
f
1
3 3 3
Answer
bull FLWOR Iteration via forin ndash Return every other item in sequence
93 Iteration (FLWORs)
These queries both return the items at odd positions in the inputsequence e
for $i in (1 to count(e))[ mod 2 eq 1]
bull Remember ebv(0) = false()ebv effective boolean value
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 17[Gru08]
for $i in (1 to count(e))[ mod 2 eq 1]
return e[$i]
for $i at $p in e
return if ($p mod 2)
then e[$p]
else ()
bull FLWOR Variable Binding via let =
ndash Note that in the examples on the last slide expression e is re-evaluated count(e) 2 times although e is constant in the loop
93 Iteration (FLWORs)
Variable bindingsThe result of evaluating an expression e may be bound to a variable $v
bull for and let clauses may be freely intermixed
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 18[Gru08]
Variable bindingsThe result of evaluating an expression e1 may be bound to a variable $vvia let
let $v = e1return e2
evaluates e2 with free occurrences of $v replaced by e1
bull FLWOR Variable Binding via letbull bull bull =
93 Iteration (FLWORs)
Iteration vs variable binding
for $x in (321)
return ($x)
rArrrArrrArrrArr (321
)
let $x = (321) rArrrArrrArrrArr (321)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 19[Gru08]
let $x = (321)
return ($x)
rArrrArrrArrrArr (321)
Every other item revisited (flip back two slides)The following hoists the constant e out of the loop body
let $seq = ereturn for $i at $p in $seq
return if ($p mod 2)
then $seq[$p]
else ()
bull Adding a where clause
ndash Inside loop bodies the idiom if (p) then e else ()
is so common that FLWOR comes with a SQL-like where clause to address this
93 Iteration (FLWORs)
A where clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 20[Gru08]
A where clauseIf ebv(p) evaluates to false() under the current variable bindings the
current iteration does not contribute to the result
for $v in e1where p
return e2
equiv
for $v in e1return if (p)
then e2else ()
bull Explicit vs implicit iterationndash XPath implicit iteration
ndash Equivalent nested FLWOR blocks
93 Iteration (FLWORs)
a[b = foo]c[2]d[e = 42]
for $a in a
ndash NB Unlike the XPath step operator for does not change the context item
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 21[Gru08]
for $a in a
where $ab = foo
return for $c at $p in $ac
where $p = 2
return for $d in $cd
where $de = 42
return $d
bull FLWOR Reorder iteration result via order byndash In a FLWOR block for $v in e1 return e2 the order of e1determines the order of the resulting sequence
93 Iteration (FLWORs)
Reordering via order by
In the FLWOR block
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 22[Gru08]
In the FLWOR block
the value (atomization) of e3 determines the order in which the bindingsof $v are used to evaluate e2
for $v in e1order by e3 [ascendingdescending][empty greatestleast]return e2
bull FLWOR Reordering examples
93 Iteration (FLWORs)
An order by no-op reordering by sequence order
for $x at $p in (53142)
order by $p
return $x
rArrrArrrArrrArr (53142)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 23[Gru08]
All bound variables in scope in order by
for $x at $p in (53142)
order by $p + $x
return $x
rArrrArrrArrrArr (13524)
Reordering as in SQLs ORDER BY
for $x at $p in (53142)
order by $x
return $x
rArrrArrrArrrArr (12345)
bull FLWOR Reordering examplesndash Value-based reordering of an XPath step result
93 Iteration (FLWORs)
This query reorders the result of the XPath location step descendantb
based on (string) value Which result is to be expectedlet $a = ltagt
ltb id=0gt42ltbgt
ltb id=1gt5ltbgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 24[Gru08]
ltb id=1gt5ltbgt
ltb id=2gt
ltb id=3gt3ltbgt
ltb id=4gt1ltbgt
ltagt
for $b in $adescendantb
order by $btext() empty greatest
return $bid
Answer
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 25
bull FLWOR semantics tuple spacendash In the W3C XQuery specification the interaction of the five clauses of a FLWOR (for-let-where-order by-return) block is formally explained by means of a tuple space
94 FLOWR Semantics
means of a tuple spacebull Size of tuple space equiv number of iterations performed by FLWOR block
bull The fields of the tuples represent for each iteration
1 forlet variable bindings
2 the outcome of the where clause
3 the value of the reordering criterion and
4 the value returned by the return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 26[Gru08]
bull FLWOR semantics tuple space (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y where order by return
10 1 100 false 4 (110)
1 Complete tuple space
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 27[Gru08]
return ($p$x) 10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (2)2 Filtering where clause ($y lt= 42)
94 FLOWR Semantics
$x $p $y where order by return
10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 28[Gru08]
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (3)3 Reordering order by clause
94 FLOWR Semantics
$x $p $y where order by return
1 10 1 true -5 (101)
2 9 4 true -4 (92)
3 8 9 true -3 (83)
4 To emit the final result scan the tuple space in the order specified by the order by column and concatenate the return column entries
(1019283746556)
ndash Observation some values have been computed but never used
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 29[Gru08]
3 8 9 true -3 (83)
4 7 16 true -2 (74)
5 6 25 true -1 (65)
6 5 36 true 0 (56)
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull XQuery Iteration FLWORsndash Remember that XPath steps perform implicit iteration in cse evaluation of e is iterated with bound to each item in cs in turn
ndash XPath subexpressions aside iteration in XQuery is explicit via the FLWOR (flower) construct
93 Iteration (FLWORs)
explicit via the FLWOR (flower) constructbull The versatile FLWOR is used to express
ndash nested iteration
ndash joins between sequences (of nodes)
ndash groupings
ndash orderings beyond document order etc
bull In a sense FLWOR assumes the role of the SELECT-FROM-WHERE block in SQL
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 13[Gru08]
bull FLWOR Iteration via forin
ndash Explicit iteration
93 Iteration (FLWORs)
Explicit iteration is expressed using the forin construct a
for $v [at $p] in e1
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 14[Gru08]
for $v [at $p] in e1 return e2
If e1 evaluates to the sequence (x1xn) the loop body e2 is
evaluated n times with variable $v bound to each xi [and $pbound to i ] in order The results of these evaluations are concatenated to form a single sequence
a the construct at $p is optional
93 Iteration (FLWORs)
Iteration examples
for $x in (321)
return ($xlowastlowastlowastlowast)rArrrArrrArrrArr (3lowastlowastlowastlowast2lowastlowastlowastlowast1lowastlowastlowastlowast)
for $x in (321)
return $xlowastlowastlowastlowastrArrrArrrArrrArr (321lowastlowastlowastlowast)
for $x in (321)
return for $y in (ab) rArrrArrrArrrArr(3a3b
2a2b
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 15[Gru08]
return for $y in (ab)
return ($x$y)
rArrrArrrArrrArr 2a2b
1a1b)
FLWOR Abbreviations
for $v1 in e1return
for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1$v2 in e2
return e3
bull FLWOR Iteration via forin
93 Iteration (FLWORs)
Purpose of this query Qmax( for $i in csdescendant-or-self[not()]
return count($iancestor) )
A sample cs ldquoAnnotatedrdquo sample cs
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 16[Gru08]
A sample csa
b c
d
e g h
f
ldquoAnnotatedrdquo sample csa
b
e
c
d
g h
f
1
3 3 3
Answer
bull FLWOR Iteration via forin ndash Return every other item in sequence
93 Iteration (FLWORs)
These queries both return the items at odd positions in the inputsequence e
for $i in (1 to count(e))[ mod 2 eq 1]
bull Remember ebv(0) = false()ebv effective boolean value
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 17[Gru08]
for $i in (1 to count(e))[ mod 2 eq 1]
return e[$i]
for $i at $p in e
return if ($p mod 2)
then e[$p]
else ()
bull FLWOR Variable Binding via let =
ndash Note that in the examples on the last slide expression e is re-evaluated count(e) 2 times although e is constant in the loop
93 Iteration (FLWORs)
Variable bindingsThe result of evaluating an expression e may be bound to a variable $v
bull for and let clauses may be freely intermixed
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 18[Gru08]
Variable bindingsThe result of evaluating an expression e1 may be bound to a variable $vvia let
let $v = e1return e2
evaluates e2 with free occurrences of $v replaced by e1
bull FLWOR Variable Binding via letbull bull bull =
93 Iteration (FLWORs)
Iteration vs variable binding
for $x in (321)
return ($x)
rArrrArrrArrrArr (321
)
let $x = (321) rArrrArrrArrrArr (321)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 19[Gru08]
let $x = (321)
return ($x)
rArrrArrrArrrArr (321)
Every other item revisited (flip back two slides)The following hoists the constant e out of the loop body
let $seq = ereturn for $i at $p in $seq
return if ($p mod 2)
then $seq[$p]
else ()
bull Adding a where clause
ndash Inside loop bodies the idiom if (p) then e else ()
is so common that FLWOR comes with a SQL-like where clause to address this
93 Iteration (FLWORs)
A where clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 20[Gru08]
A where clauseIf ebv(p) evaluates to false() under the current variable bindings the
current iteration does not contribute to the result
for $v in e1where p
return e2
equiv
for $v in e1return if (p)
then e2else ()
bull Explicit vs implicit iterationndash XPath implicit iteration
ndash Equivalent nested FLWOR blocks
93 Iteration (FLWORs)
a[b = foo]c[2]d[e = 42]
for $a in a
ndash NB Unlike the XPath step operator for does not change the context item
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 21[Gru08]
for $a in a
where $ab = foo
return for $c at $p in $ac
where $p = 2
return for $d in $cd
where $de = 42
return $d
bull FLWOR Reorder iteration result via order byndash In a FLWOR block for $v in e1 return e2 the order of e1determines the order of the resulting sequence
93 Iteration (FLWORs)
Reordering via order by
In the FLWOR block
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 22[Gru08]
In the FLWOR block
the value (atomization) of e3 determines the order in which the bindingsof $v are used to evaluate e2
for $v in e1order by e3 [ascendingdescending][empty greatestleast]return e2
bull FLWOR Reordering examples
93 Iteration (FLWORs)
An order by no-op reordering by sequence order
for $x at $p in (53142)
order by $p
return $x
rArrrArrrArrrArr (53142)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 23[Gru08]
All bound variables in scope in order by
for $x at $p in (53142)
order by $p + $x
return $x
rArrrArrrArrrArr (13524)
Reordering as in SQLs ORDER BY
for $x at $p in (53142)
order by $x
return $x
rArrrArrrArrrArr (12345)
bull FLWOR Reordering examplesndash Value-based reordering of an XPath step result
93 Iteration (FLWORs)
This query reorders the result of the XPath location step descendantb
based on (string) value Which result is to be expectedlet $a = ltagt
ltb id=0gt42ltbgt
ltb id=1gt5ltbgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 24[Gru08]
ltb id=1gt5ltbgt
ltb id=2gt
ltb id=3gt3ltbgt
ltb id=4gt1ltbgt
ltagt
for $b in $adescendantb
order by $btext() empty greatest
return $bid
Answer
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 25
bull FLWOR semantics tuple spacendash In the W3C XQuery specification the interaction of the five clauses of a FLWOR (for-let-where-order by-return) block is formally explained by means of a tuple space
94 FLOWR Semantics
means of a tuple spacebull Size of tuple space equiv number of iterations performed by FLWOR block
bull The fields of the tuples represent for each iteration
1 forlet variable bindings
2 the outcome of the where clause
3 the value of the reordering criterion and
4 the value returned by the return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 26[Gru08]
bull FLWOR semantics tuple space (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y where order by return
10 1 100 false 4 (110)
1 Complete tuple space
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 27[Gru08]
return ($p$x) 10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (2)2 Filtering where clause ($y lt= 42)
94 FLOWR Semantics
$x $p $y where order by return
10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 28[Gru08]
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (3)3 Reordering order by clause
94 FLOWR Semantics
$x $p $y where order by return
1 10 1 true -5 (101)
2 9 4 true -4 (92)
3 8 9 true -3 (83)
4 To emit the final result scan the tuple space in the order specified by the order by column and concatenate the return column entries
(1019283746556)
ndash Observation some values have been computed but never used
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 29[Gru08]
3 8 9 true -3 (83)
4 7 16 true -2 (74)
5 6 25 true -1 (65)
6 5 36 true 0 (56)
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull FLWOR Iteration via forin
ndash Explicit iteration
93 Iteration (FLWORs)
Explicit iteration is expressed using the forin construct a
for $v [at $p] in e1
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 14[Gru08]
for $v [at $p] in e1 return e2
If e1 evaluates to the sequence (x1xn) the loop body e2 is
evaluated n times with variable $v bound to each xi [and $pbound to i ] in order The results of these evaluations are concatenated to form a single sequence
a the construct at $p is optional
93 Iteration (FLWORs)
Iteration examples
for $x in (321)
return ($xlowastlowastlowastlowast)rArrrArrrArrrArr (3lowastlowastlowastlowast2lowastlowastlowastlowast1lowastlowastlowastlowast)
for $x in (321)
return $xlowastlowastlowastlowastrArrrArrrArrrArr (321lowastlowastlowastlowast)
for $x in (321)
return for $y in (ab) rArrrArrrArrrArr(3a3b
2a2b
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 15[Gru08]
return for $y in (ab)
return ($x$y)
rArrrArrrArrrArr 2a2b
1a1b)
FLWOR Abbreviations
for $v1 in e1return
for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1$v2 in e2
return e3
bull FLWOR Iteration via forin
93 Iteration (FLWORs)
Purpose of this query Qmax( for $i in csdescendant-or-self[not()]
return count($iancestor) )
A sample cs ldquoAnnotatedrdquo sample cs
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 16[Gru08]
A sample csa
b c
d
e g h
f
ldquoAnnotatedrdquo sample csa
b
e
c
d
g h
f
1
3 3 3
Answer
bull FLWOR Iteration via forin ndash Return every other item in sequence
93 Iteration (FLWORs)
These queries both return the items at odd positions in the inputsequence e
for $i in (1 to count(e))[ mod 2 eq 1]
bull Remember ebv(0) = false()ebv effective boolean value
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 17[Gru08]
for $i in (1 to count(e))[ mod 2 eq 1]
return e[$i]
for $i at $p in e
return if ($p mod 2)
then e[$p]
else ()
bull FLWOR Variable Binding via let =
ndash Note that in the examples on the last slide expression e is re-evaluated count(e) 2 times although e is constant in the loop
93 Iteration (FLWORs)
Variable bindingsThe result of evaluating an expression e may be bound to a variable $v
bull for and let clauses may be freely intermixed
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 18[Gru08]
Variable bindingsThe result of evaluating an expression e1 may be bound to a variable $vvia let
let $v = e1return e2
evaluates e2 with free occurrences of $v replaced by e1
bull FLWOR Variable Binding via letbull bull bull =
93 Iteration (FLWORs)
Iteration vs variable binding
for $x in (321)
return ($x)
rArrrArrrArrrArr (321
)
let $x = (321) rArrrArrrArrrArr (321)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 19[Gru08]
let $x = (321)
return ($x)
rArrrArrrArrrArr (321)
Every other item revisited (flip back two slides)The following hoists the constant e out of the loop body
let $seq = ereturn for $i at $p in $seq
return if ($p mod 2)
then $seq[$p]
else ()
bull Adding a where clause
ndash Inside loop bodies the idiom if (p) then e else ()
is so common that FLWOR comes with a SQL-like where clause to address this
93 Iteration (FLWORs)
A where clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 20[Gru08]
A where clauseIf ebv(p) evaluates to false() under the current variable bindings the
current iteration does not contribute to the result
for $v in e1where p
return e2
equiv
for $v in e1return if (p)
then e2else ()
bull Explicit vs implicit iterationndash XPath implicit iteration
ndash Equivalent nested FLWOR blocks
93 Iteration (FLWORs)
a[b = foo]c[2]d[e = 42]
for $a in a
ndash NB Unlike the XPath step operator for does not change the context item
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 21[Gru08]
for $a in a
where $ab = foo
return for $c at $p in $ac
where $p = 2
return for $d in $cd
where $de = 42
return $d
bull FLWOR Reorder iteration result via order byndash In a FLWOR block for $v in e1 return e2 the order of e1determines the order of the resulting sequence
93 Iteration (FLWORs)
Reordering via order by
In the FLWOR block
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 22[Gru08]
In the FLWOR block
the value (atomization) of e3 determines the order in which the bindingsof $v are used to evaluate e2
for $v in e1order by e3 [ascendingdescending][empty greatestleast]return e2
bull FLWOR Reordering examples
93 Iteration (FLWORs)
An order by no-op reordering by sequence order
for $x at $p in (53142)
order by $p
return $x
rArrrArrrArrrArr (53142)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 23[Gru08]
All bound variables in scope in order by
for $x at $p in (53142)
order by $p + $x
return $x
rArrrArrrArrrArr (13524)
Reordering as in SQLs ORDER BY
for $x at $p in (53142)
order by $x
return $x
rArrrArrrArrrArr (12345)
bull FLWOR Reordering examplesndash Value-based reordering of an XPath step result
93 Iteration (FLWORs)
This query reorders the result of the XPath location step descendantb
based on (string) value Which result is to be expectedlet $a = ltagt
ltb id=0gt42ltbgt
ltb id=1gt5ltbgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 24[Gru08]
ltb id=1gt5ltbgt
ltb id=2gt
ltb id=3gt3ltbgt
ltb id=4gt1ltbgt
ltagt
for $b in $adescendantb
order by $btext() empty greatest
return $bid
Answer
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 25
bull FLWOR semantics tuple spacendash In the W3C XQuery specification the interaction of the five clauses of a FLWOR (for-let-where-order by-return) block is formally explained by means of a tuple space
94 FLOWR Semantics
means of a tuple spacebull Size of tuple space equiv number of iterations performed by FLWOR block
bull The fields of the tuples represent for each iteration
1 forlet variable bindings
2 the outcome of the where clause
3 the value of the reordering criterion and
4 the value returned by the return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 26[Gru08]
bull FLWOR semantics tuple space (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y where order by return
10 1 100 false 4 (110)
1 Complete tuple space
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 27[Gru08]
return ($p$x) 10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (2)2 Filtering where clause ($y lt= 42)
94 FLOWR Semantics
$x $p $y where order by return
10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 28[Gru08]
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (3)3 Reordering order by clause
94 FLOWR Semantics
$x $p $y where order by return
1 10 1 true -5 (101)
2 9 4 true -4 (92)
3 8 9 true -3 (83)
4 To emit the final result scan the tuple space in the order specified by the order by column and concatenate the return column entries
(1019283746556)
ndash Observation some values have been computed but never used
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 29[Gru08]
3 8 9 true -3 (83)
4 7 16 true -2 (74)
5 6 25 true -1 (65)
6 5 36 true 0 (56)
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
93 Iteration (FLWORs)
Iteration examples
for $x in (321)
return ($xlowastlowastlowastlowast)rArrrArrrArrrArr (3lowastlowastlowastlowast2lowastlowastlowastlowast1lowastlowastlowastlowast)
for $x in (321)
return $xlowastlowastlowastlowastrArrrArrrArrrArr (321lowastlowastlowastlowast)
for $x in (321)
return for $y in (ab) rArrrArrrArrrArr(3a3b
2a2b
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 15[Gru08]
return for $y in (ab)
return ($x$y)
rArrrArrrArrrArr 2a2b
1a1b)
FLWOR Abbreviations
for $v1 in e1return
for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1for $v2 in e2return e3
equivequivequivequiv
for $v1 in e1$v2 in e2
return e3
bull FLWOR Iteration via forin
93 Iteration (FLWORs)
Purpose of this query Qmax( for $i in csdescendant-or-self[not()]
return count($iancestor) )
A sample cs ldquoAnnotatedrdquo sample cs
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 16[Gru08]
A sample csa
b c
d
e g h
f
ldquoAnnotatedrdquo sample csa
b
e
c
d
g h
f
1
3 3 3
Answer
bull FLWOR Iteration via forin ndash Return every other item in sequence
93 Iteration (FLWORs)
These queries both return the items at odd positions in the inputsequence e
for $i in (1 to count(e))[ mod 2 eq 1]
bull Remember ebv(0) = false()ebv effective boolean value
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 17[Gru08]
for $i in (1 to count(e))[ mod 2 eq 1]
return e[$i]
for $i at $p in e
return if ($p mod 2)
then e[$p]
else ()
bull FLWOR Variable Binding via let =
ndash Note that in the examples on the last slide expression e is re-evaluated count(e) 2 times although e is constant in the loop
93 Iteration (FLWORs)
Variable bindingsThe result of evaluating an expression e may be bound to a variable $v
bull for and let clauses may be freely intermixed
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 18[Gru08]
Variable bindingsThe result of evaluating an expression e1 may be bound to a variable $vvia let
let $v = e1return e2
evaluates e2 with free occurrences of $v replaced by e1
bull FLWOR Variable Binding via letbull bull bull =
93 Iteration (FLWORs)
Iteration vs variable binding
for $x in (321)
return ($x)
rArrrArrrArrrArr (321
)
let $x = (321) rArrrArrrArrrArr (321)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 19[Gru08]
let $x = (321)
return ($x)
rArrrArrrArrrArr (321)
Every other item revisited (flip back two slides)The following hoists the constant e out of the loop body
let $seq = ereturn for $i at $p in $seq
return if ($p mod 2)
then $seq[$p]
else ()
bull Adding a where clause
ndash Inside loop bodies the idiom if (p) then e else ()
is so common that FLWOR comes with a SQL-like where clause to address this
93 Iteration (FLWORs)
A where clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 20[Gru08]
A where clauseIf ebv(p) evaluates to false() under the current variable bindings the
current iteration does not contribute to the result
for $v in e1where p
return e2
equiv
for $v in e1return if (p)
then e2else ()
bull Explicit vs implicit iterationndash XPath implicit iteration
ndash Equivalent nested FLWOR blocks
93 Iteration (FLWORs)
a[b = foo]c[2]d[e = 42]
for $a in a
ndash NB Unlike the XPath step operator for does not change the context item
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 21[Gru08]
for $a in a
where $ab = foo
return for $c at $p in $ac
where $p = 2
return for $d in $cd
where $de = 42
return $d
bull FLWOR Reorder iteration result via order byndash In a FLWOR block for $v in e1 return e2 the order of e1determines the order of the resulting sequence
93 Iteration (FLWORs)
Reordering via order by
In the FLWOR block
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 22[Gru08]
In the FLWOR block
the value (atomization) of e3 determines the order in which the bindingsof $v are used to evaluate e2
for $v in e1order by e3 [ascendingdescending][empty greatestleast]return e2
bull FLWOR Reordering examples
93 Iteration (FLWORs)
An order by no-op reordering by sequence order
for $x at $p in (53142)
order by $p
return $x
rArrrArrrArrrArr (53142)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 23[Gru08]
All bound variables in scope in order by
for $x at $p in (53142)
order by $p + $x
return $x
rArrrArrrArrrArr (13524)
Reordering as in SQLs ORDER BY
for $x at $p in (53142)
order by $x
return $x
rArrrArrrArrrArr (12345)
bull FLWOR Reordering examplesndash Value-based reordering of an XPath step result
93 Iteration (FLWORs)
This query reorders the result of the XPath location step descendantb
based on (string) value Which result is to be expectedlet $a = ltagt
ltb id=0gt42ltbgt
ltb id=1gt5ltbgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 24[Gru08]
ltb id=1gt5ltbgt
ltb id=2gt
ltb id=3gt3ltbgt
ltb id=4gt1ltbgt
ltagt
for $b in $adescendantb
order by $btext() empty greatest
return $bid
Answer
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 25
bull FLWOR semantics tuple spacendash In the W3C XQuery specification the interaction of the five clauses of a FLWOR (for-let-where-order by-return) block is formally explained by means of a tuple space
94 FLOWR Semantics
means of a tuple spacebull Size of tuple space equiv number of iterations performed by FLWOR block
bull The fields of the tuples represent for each iteration
1 forlet variable bindings
2 the outcome of the where clause
3 the value of the reordering criterion and
4 the value returned by the return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 26[Gru08]
bull FLWOR semantics tuple space (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y where order by return
10 1 100 false 4 (110)
1 Complete tuple space
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 27[Gru08]
return ($p$x) 10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (2)2 Filtering where clause ($y lt= 42)
94 FLOWR Semantics
$x $p $y where order by return
10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 28[Gru08]
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (3)3 Reordering order by clause
94 FLOWR Semantics
$x $p $y where order by return
1 10 1 true -5 (101)
2 9 4 true -4 (92)
3 8 9 true -3 (83)
4 To emit the final result scan the tuple space in the order specified by the order by column and concatenate the return column entries
(1019283746556)
ndash Observation some values have been computed but never used
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 29[Gru08]
3 8 9 true -3 (83)
4 7 16 true -2 (74)
5 6 25 true -1 (65)
6 5 36 true 0 (56)
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull FLWOR Iteration via forin
93 Iteration (FLWORs)
Purpose of this query Qmax( for $i in csdescendant-or-self[not()]
return count($iancestor) )
A sample cs ldquoAnnotatedrdquo sample cs
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 16[Gru08]
A sample csa
b c
d
e g h
f
ldquoAnnotatedrdquo sample csa
b
e
c
d
g h
f
1
3 3 3
Answer
bull FLWOR Iteration via forin ndash Return every other item in sequence
93 Iteration (FLWORs)
These queries both return the items at odd positions in the inputsequence e
for $i in (1 to count(e))[ mod 2 eq 1]
bull Remember ebv(0) = false()ebv effective boolean value
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 17[Gru08]
for $i in (1 to count(e))[ mod 2 eq 1]
return e[$i]
for $i at $p in e
return if ($p mod 2)
then e[$p]
else ()
bull FLWOR Variable Binding via let =
ndash Note that in the examples on the last slide expression e is re-evaluated count(e) 2 times although e is constant in the loop
93 Iteration (FLWORs)
Variable bindingsThe result of evaluating an expression e may be bound to a variable $v
bull for and let clauses may be freely intermixed
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 18[Gru08]
Variable bindingsThe result of evaluating an expression e1 may be bound to a variable $vvia let
let $v = e1return e2
evaluates e2 with free occurrences of $v replaced by e1
bull FLWOR Variable Binding via letbull bull bull =
93 Iteration (FLWORs)
Iteration vs variable binding
for $x in (321)
return ($x)
rArrrArrrArrrArr (321
)
let $x = (321) rArrrArrrArrrArr (321)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 19[Gru08]
let $x = (321)
return ($x)
rArrrArrrArrrArr (321)
Every other item revisited (flip back two slides)The following hoists the constant e out of the loop body
let $seq = ereturn for $i at $p in $seq
return if ($p mod 2)
then $seq[$p]
else ()
bull Adding a where clause
ndash Inside loop bodies the idiom if (p) then e else ()
is so common that FLWOR comes with a SQL-like where clause to address this
93 Iteration (FLWORs)
A where clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 20[Gru08]
A where clauseIf ebv(p) evaluates to false() under the current variable bindings the
current iteration does not contribute to the result
for $v in e1where p
return e2
equiv
for $v in e1return if (p)
then e2else ()
bull Explicit vs implicit iterationndash XPath implicit iteration
ndash Equivalent nested FLWOR blocks
93 Iteration (FLWORs)
a[b = foo]c[2]d[e = 42]
for $a in a
ndash NB Unlike the XPath step operator for does not change the context item
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 21[Gru08]
for $a in a
where $ab = foo
return for $c at $p in $ac
where $p = 2
return for $d in $cd
where $de = 42
return $d
bull FLWOR Reorder iteration result via order byndash In a FLWOR block for $v in e1 return e2 the order of e1determines the order of the resulting sequence
93 Iteration (FLWORs)
Reordering via order by
In the FLWOR block
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 22[Gru08]
In the FLWOR block
the value (atomization) of e3 determines the order in which the bindingsof $v are used to evaluate e2
for $v in e1order by e3 [ascendingdescending][empty greatestleast]return e2
bull FLWOR Reordering examples
93 Iteration (FLWORs)
An order by no-op reordering by sequence order
for $x at $p in (53142)
order by $p
return $x
rArrrArrrArrrArr (53142)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 23[Gru08]
All bound variables in scope in order by
for $x at $p in (53142)
order by $p + $x
return $x
rArrrArrrArrrArr (13524)
Reordering as in SQLs ORDER BY
for $x at $p in (53142)
order by $x
return $x
rArrrArrrArrrArr (12345)
bull FLWOR Reordering examplesndash Value-based reordering of an XPath step result
93 Iteration (FLWORs)
This query reorders the result of the XPath location step descendantb
based on (string) value Which result is to be expectedlet $a = ltagt
ltb id=0gt42ltbgt
ltb id=1gt5ltbgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 24[Gru08]
ltb id=1gt5ltbgt
ltb id=2gt
ltb id=3gt3ltbgt
ltb id=4gt1ltbgt
ltagt
for $b in $adescendantb
order by $btext() empty greatest
return $bid
Answer
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 25
bull FLWOR semantics tuple spacendash In the W3C XQuery specification the interaction of the five clauses of a FLWOR (for-let-where-order by-return) block is formally explained by means of a tuple space
94 FLOWR Semantics
means of a tuple spacebull Size of tuple space equiv number of iterations performed by FLWOR block
bull The fields of the tuples represent for each iteration
1 forlet variable bindings
2 the outcome of the where clause
3 the value of the reordering criterion and
4 the value returned by the return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 26[Gru08]
bull FLWOR semantics tuple space (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y where order by return
10 1 100 false 4 (110)
1 Complete tuple space
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 27[Gru08]
return ($p$x) 10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (2)2 Filtering where clause ($y lt= 42)
94 FLOWR Semantics
$x $p $y where order by return
10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 28[Gru08]
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (3)3 Reordering order by clause
94 FLOWR Semantics
$x $p $y where order by return
1 10 1 true -5 (101)
2 9 4 true -4 (92)
3 8 9 true -3 (83)
4 To emit the final result scan the tuple space in the order specified by the order by column and concatenate the return column entries
(1019283746556)
ndash Observation some values have been computed but never used
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 29[Gru08]
3 8 9 true -3 (83)
4 7 16 true -2 (74)
5 6 25 true -1 (65)
6 5 36 true 0 (56)
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull FLWOR Iteration via forin ndash Return every other item in sequence
93 Iteration (FLWORs)
These queries both return the items at odd positions in the inputsequence e
for $i in (1 to count(e))[ mod 2 eq 1]
bull Remember ebv(0) = false()ebv effective boolean value
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 17[Gru08]
for $i in (1 to count(e))[ mod 2 eq 1]
return e[$i]
for $i at $p in e
return if ($p mod 2)
then e[$p]
else ()
bull FLWOR Variable Binding via let =
ndash Note that in the examples on the last slide expression e is re-evaluated count(e) 2 times although e is constant in the loop
93 Iteration (FLWORs)
Variable bindingsThe result of evaluating an expression e may be bound to a variable $v
bull for and let clauses may be freely intermixed
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 18[Gru08]
Variable bindingsThe result of evaluating an expression e1 may be bound to a variable $vvia let
let $v = e1return e2
evaluates e2 with free occurrences of $v replaced by e1
bull FLWOR Variable Binding via letbull bull bull =
93 Iteration (FLWORs)
Iteration vs variable binding
for $x in (321)
return ($x)
rArrrArrrArrrArr (321
)
let $x = (321) rArrrArrrArrrArr (321)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 19[Gru08]
let $x = (321)
return ($x)
rArrrArrrArrrArr (321)
Every other item revisited (flip back two slides)The following hoists the constant e out of the loop body
let $seq = ereturn for $i at $p in $seq
return if ($p mod 2)
then $seq[$p]
else ()
bull Adding a where clause
ndash Inside loop bodies the idiom if (p) then e else ()
is so common that FLWOR comes with a SQL-like where clause to address this
93 Iteration (FLWORs)
A where clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 20[Gru08]
A where clauseIf ebv(p) evaluates to false() under the current variable bindings the
current iteration does not contribute to the result
for $v in e1where p
return e2
equiv
for $v in e1return if (p)
then e2else ()
bull Explicit vs implicit iterationndash XPath implicit iteration
ndash Equivalent nested FLWOR blocks
93 Iteration (FLWORs)
a[b = foo]c[2]d[e = 42]
for $a in a
ndash NB Unlike the XPath step operator for does not change the context item
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 21[Gru08]
for $a in a
where $ab = foo
return for $c at $p in $ac
where $p = 2
return for $d in $cd
where $de = 42
return $d
bull FLWOR Reorder iteration result via order byndash In a FLWOR block for $v in e1 return e2 the order of e1determines the order of the resulting sequence
93 Iteration (FLWORs)
Reordering via order by
In the FLWOR block
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 22[Gru08]
In the FLWOR block
the value (atomization) of e3 determines the order in which the bindingsof $v are used to evaluate e2
for $v in e1order by e3 [ascendingdescending][empty greatestleast]return e2
bull FLWOR Reordering examples
93 Iteration (FLWORs)
An order by no-op reordering by sequence order
for $x at $p in (53142)
order by $p
return $x
rArrrArrrArrrArr (53142)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 23[Gru08]
All bound variables in scope in order by
for $x at $p in (53142)
order by $p + $x
return $x
rArrrArrrArrrArr (13524)
Reordering as in SQLs ORDER BY
for $x at $p in (53142)
order by $x
return $x
rArrrArrrArrrArr (12345)
bull FLWOR Reordering examplesndash Value-based reordering of an XPath step result
93 Iteration (FLWORs)
This query reorders the result of the XPath location step descendantb
based on (string) value Which result is to be expectedlet $a = ltagt
ltb id=0gt42ltbgt
ltb id=1gt5ltbgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 24[Gru08]
ltb id=1gt5ltbgt
ltb id=2gt
ltb id=3gt3ltbgt
ltb id=4gt1ltbgt
ltagt
for $b in $adescendantb
order by $btext() empty greatest
return $bid
Answer
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 25
bull FLWOR semantics tuple spacendash In the W3C XQuery specification the interaction of the five clauses of a FLWOR (for-let-where-order by-return) block is formally explained by means of a tuple space
94 FLOWR Semantics
means of a tuple spacebull Size of tuple space equiv number of iterations performed by FLWOR block
bull The fields of the tuples represent for each iteration
1 forlet variable bindings
2 the outcome of the where clause
3 the value of the reordering criterion and
4 the value returned by the return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 26[Gru08]
bull FLWOR semantics tuple space (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y where order by return
10 1 100 false 4 (110)
1 Complete tuple space
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 27[Gru08]
return ($p$x) 10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (2)2 Filtering where clause ($y lt= 42)
94 FLOWR Semantics
$x $p $y where order by return
10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 28[Gru08]
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (3)3 Reordering order by clause
94 FLOWR Semantics
$x $p $y where order by return
1 10 1 true -5 (101)
2 9 4 true -4 (92)
3 8 9 true -3 (83)
4 To emit the final result scan the tuple space in the order specified by the order by column and concatenate the return column entries
(1019283746556)
ndash Observation some values have been computed but never used
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 29[Gru08]
3 8 9 true -3 (83)
4 7 16 true -2 (74)
5 6 25 true -1 (65)
6 5 36 true 0 (56)
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull FLWOR Variable Binding via let =
ndash Note that in the examples on the last slide expression e is re-evaluated count(e) 2 times although e is constant in the loop
93 Iteration (FLWORs)
Variable bindingsThe result of evaluating an expression e may be bound to a variable $v
bull for and let clauses may be freely intermixed
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 18[Gru08]
Variable bindingsThe result of evaluating an expression e1 may be bound to a variable $vvia let
let $v = e1return e2
evaluates e2 with free occurrences of $v replaced by e1
bull FLWOR Variable Binding via letbull bull bull =
93 Iteration (FLWORs)
Iteration vs variable binding
for $x in (321)
return ($x)
rArrrArrrArrrArr (321
)
let $x = (321) rArrrArrrArrrArr (321)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 19[Gru08]
let $x = (321)
return ($x)
rArrrArrrArrrArr (321)
Every other item revisited (flip back two slides)The following hoists the constant e out of the loop body
let $seq = ereturn for $i at $p in $seq
return if ($p mod 2)
then $seq[$p]
else ()
bull Adding a where clause
ndash Inside loop bodies the idiom if (p) then e else ()
is so common that FLWOR comes with a SQL-like where clause to address this
93 Iteration (FLWORs)
A where clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 20[Gru08]
A where clauseIf ebv(p) evaluates to false() under the current variable bindings the
current iteration does not contribute to the result
for $v in e1where p
return e2
equiv
for $v in e1return if (p)
then e2else ()
bull Explicit vs implicit iterationndash XPath implicit iteration
ndash Equivalent nested FLWOR blocks
93 Iteration (FLWORs)
a[b = foo]c[2]d[e = 42]
for $a in a
ndash NB Unlike the XPath step operator for does not change the context item
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 21[Gru08]
for $a in a
where $ab = foo
return for $c at $p in $ac
where $p = 2
return for $d in $cd
where $de = 42
return $d
bull FLWOR Reorder iteration result via order byndash In a FLWOR block for $v in e1 return e2 the order of e1determines the order of the resulting sequence
93 Iteration (FLWORs)
Reordering via order by
In the FLWOR block
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 22[Gru08]
In the FLWOR block
the value (atomization) of e3 determines the order in which the bindingsof $v are used to evaluate e2
for $v in e1order by e3 [ascendingdescending][empty greatestleast]return e2
bull FLWOR Reordering examples
93 Iteration (FLWORs)
An order by no-op reordering by sequence order
for $x at $p in (53142)
order by $p
return $x
rArrrArrrArrrArr (53142)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 23[Gru08]
All bound variables in scope in order by
for $x at $p in (53142)
order by $p + $x
return $x
rArrrArrrArrrArr (13524)
Reordering as in SQLs ORDER BY
for $x at $p in (53142)
order by $x
return $x
rArrrArrrArrrArr (12345)
bull FLWOR Reordering examplesndash Value-based reordering of an XPath step result
93 Iteration (FLWORs)
This query reorders the result of the XPath location step descendantb
based on (string) value Which result is to be expectedlet $a = ltagt
ltb id=0gt42ltbgt
ltb id=1gt5ltbgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 24[Gru08]
ltb id=1gt5ltbgt
ltb id=2gt
ltb id=3gt3ltbgt
ltb id=4gt1ltbgt
ltagt
for $b in $adescendantb
order by $btext() empty greatest
return $bid
Answer
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 25
bull FLWOR semantics tuple spacendash In the W3C XQuery specification the interaction of the five clauses of a FLWOR (for-let-where-order by-return) block is formally explained by means of a tuple space
94 FLOWR Semantics
means of a tuple spacebull Size of tuple space equiv number of iterations performed by FLWOR block
bull The fields of the tuples represent for each iteration
1 forlet variable bindings
2 the outcome of the where clause
3 the value of the reordering criterion and
4 the value returned by the return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 26[Gru08]
bull FLWOR semantics tuple space (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y where order by return
10 1 100 false 4 (110)
1 Complete tuple space
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 27[Gru08]
return ($p$x) 10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (2)2 Filtering where clause ($y lt= 42)
94 FLOWR Semantics
$x $p $y where order by return
10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 28[Gru08]
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (3)3 Reordering order by clause
94 FLOWR Semantics
$x $p $y where order by return
1 10 1 true -5 (101)
2 9 4 true -4 (92)
3 8 9 true -3 (83)
4 To emit the final result scan the tuple space in the order specified by the order by column and concatenate the return column entries
(1019283746556)
ndash Observation some values have been computed but never used
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 29[Gru08]
3 8 9 true -3 (83)
4 7 16 true -2 (74)
5 6 25 true -1 (65)
6 5 36 true 0 (56)
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull FLWOR Variable Binding via letbull bull bull =
93 Iteration (FLWORs)
Iteration vs variable binding
for $x in (321)
return ($x)
rArrrArrrArrrArr (321
)
let $x = (321) rArrrArrrArrrArr (321)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 19[Gru08]
let $x = (321)
return ($x)
rArrrArrrArrrArr (321)
Every other item revisited (flip back two slides)The following hoists the constant e out of the loop body
let $seq = ereturn for $i at $p in $seq
return if ($p mod 2)
then $seq[$p]
else ()
bull Adding a where clause
ndash Inside loop bodies the idiom if (p) then e else ()
is so common that FLWOR comes with a SQL-like where clause to address this
93 Iteration (FLWORs)
A where clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 20[Gru08]
A where clauseIf ebv(p) evaluates to false() under the current variable bindings the
current iteration does not contribute to the result
for $v in e1where p
return e2
equiv
for $v in e1return if (p)
then e2else ()
bull Explicit vs implicit iterationndash XPath implicit iteration
ndash Equivalent nested FLWOR blocks
93 Iteration (FLWORs)
a[b = foo]c[2]d[e = 42]
for $a in a
ndash NB Unlike the XPath step operator for does not change the context item
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 21[Gru08]
for $a in a
where $ab = foo
return for $c at $p in $ac
where $p = 2
return for $d in $cd
where $de = 42
return $d
bull FLWOR Reorder iteration result via order byndash In a FLWOR block for $v in e1 return e2 the order of e1determines the order of the resulting sequence
93 Iteration (FLWORs)
Reordering via order by
In the FLWOR block
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 22[Gru08]
In the FLWOR block
the value (atomization) of e3 determines the order in which the bindingsof $v are used to evaluate e2
for $v in e1order by e3 [ascendingdescending][empty greatestleast]return e2
bull FLWOR Reordering examples
93 Iteration (FLWORs)
An order by no-op reordering by sequence order
for $x at $p in (53142)
order by $p
return $x
rArrrArrrArrrArr (53142)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 23[Gru08]
All bound variables in scope in order by
for $x at $p in (53142)
order by $p + $x
return $x
rArrrArrrArrrArr (13524)
Reordering as in SQLs ORDER BY
for $x at $p in (53142)
order by $x
return $x
rArrrArrrArrrArr (12345)
bull FLWOR Reordering examplesndash Value-based reordering of an XPath step result
93 Iteration (FLWORs)
This query reorders the result of the XPath location step descendantb
based on (string) value Which result is to be expectedlet $a = ltagt
ltb id=0gt42ltbgt
ltb id=1gt5ltbgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 24[Gru08]
ltb id=1gt5ltbgt
ltb id=2gt
ltb id=3gt3ltbgt
ltb id=4gt1ltbgt
ltagt
for $b in $adescendantb
order by $btext() empty greatest
return $bid
Answer
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 25
bull FLWOR semantics tuple spacendash In the W3C XQuery specification the interaction of the five clauses of a FLWOR (for-let-where-order by-return) block is formally explained by means of a tuple space
94 FLOWR Semantics
means of a tuple spacebull Size of tuple space equiv number of iterations performed by FLWOR block
bull The fields of the tuples represent for each iteration
1 forlet variable bindings
2 the outcome of the where clause
3 the value of the reordering criterion and
4 the value returned by the return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 26[Gru08]
bull FLWOR semantics tuple space (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y where order by return
10 1 100 false 4 (110)
1 Complete tuple space
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 27[Gru08]
return ($p$x) 10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (2)2 Filtering where clause ($y lt= 42)
94 FLOWR Semantics
$x $p $y where order by return
10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 28[Gru08]
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (3)3 Reordering order by clause
94 FLOWR Semantics
$x $p $y where order by return
1 10 1 true -5 (101)
2 9 4 true -4 (92)
3 8 9 true -3 (83)
4 To emit the final result scan the tuple space in the order specified by the order by column and concatenate the return column entries
(1019283746556)
ndash Observation some values have been computed but never used
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 29[Gru08]
3 8 9 true -3 (83)
4 7 16 true -2 (74)
5 6 25 true -1 (65)
6 5 36 true 0 (56)
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull Adding a where clause
ndash Inside loop bodies the idiom if (p) then e else ()
is so common that FLWOR comes with a SQL-like where clause to address this
93 Iteration (FLWORs)
A where clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 20[Gru08]
A where clauseIf ebv(p) evaluates to false() under the current variable bindings the
current iteration does not contribute to the result
for $v in e1where p
return e2
equiv
for $v in e1return if (p)
then e2else ()
bull Explicit vs implicit iterationndash XPath implicit iteration
ndash Equivalent nested FLWOR blocks
93 Iteration (FLWORs)
a[b = foo]c[2]d[e = 42]
for $a in a
ndash NB Unlike the XPath step operator for does not change the context item
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 21[Gru08]
for $a in a
where $ab = foo
return for $c at $p in $ac
where $p = 2
return for $d in $cd
where $de = 42
return $d
bull FLWOR Reorder iteration result via order byndash In a FLWOR block for $v in e1 return e2 the order of e1determines the order of the resulting sequence
93 Iteration (FLWORs)
Reordering via order by
In the FLWOR block
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 22[Gru08]
In the FLWOR block
the value (atomization) of e3 determines the order in which the bindingsof $v are used to evaluate e2
for $v in e1order by e3 [ascendingdescending][empty greatestleast]return e2
bull FLWOR Reordering examples
93 Iteration (FLWORs)
An order by no-op reordering by sequence order
for $x at $p in (53142)
order by $p
return $x
rArrrArrrArrrArr (53142)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 23[Gru08]
All bound variables in scope in order by
for $x at $p in (53142)
order by $p + $x
return $x
rArrrArrrArrrArr (13524)
Reordering as in SQLs ORDER BY
for $x at $p in (53142)
order by $x
return $x
rArrrArrrArrrArr (12345)
bull FLWOR Reordering examplesndash Value-based reordering of an XPath step result
93 Iteration (FLWORs)
This query reorders the result of the XPath location step descendantb
based on (string) value Which result is to be expectedlet $a = ltagt
ltb id=0gt42ltbgt
ltb id=1gt5ltbgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 24[Gru08]
ltb id=1gt5ltbgt
ltb id=2gt
ltb id=3gt3ltbgt
ltb id=4gt1ltbgt
ltagt
for $b in $adescendantb
order by $btext() empty greatest
return $bid
Answer
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 25
bull FLWOR semantics tuple spacendash In the W3C XQuery specification the interaction of the five clauses of a FLWOR (for-let-where-order by-return) block is formally explained by means of a tuple space
94 FLOWR Semantics
means of a tuple spacebull Size of tuple space equiv number of iterations performed by FLWOR block
bull The fields of the tuples represent for each iteration
1 forlet variable bindings
2 the outcome of the where clause
3 the value of the reordering criterion and
4 the value returned by the return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 26[Gru08]
bull FLWOR semantics tuple space (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y where order by return
10 1 100 false 4 (110)
1 Complete tuple space
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 27[Gru08]
return ($p$x) 10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (2)2 Filtering where clause ($y lt= 42)
94 FLOWR Semantics
$x $p $y where order by return
10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 28[Gru08]
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (3)3 Reordering order by clause
94 FLOWR Semantics
$x $p $y where order by return
1 10 1 true -5 (101)
2 9 4 true -4 (92)
3 8 9 true -3 (83)
4 To emit the final result scan the tuple space in the order specified by the order by column and concatenate the return column entries
(1019283746556)
ndash Observation some values have been computed but never used
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 29[Gru08]
3 8 9 true -3 (83)
4 7 16 true -2 (74)
5 6 25 true -1 (65)
6 5 36 true 0 (56)
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull Explicit vs implicit iterationndash XPath implicit iteration
ndash Equivalent nested FLWOR blocks
93 Iteration (FLWORs)
a[b = foo]c[2]d[e = 42]
for $a in a
ndash NB Unlike the XPath step operator for does not change the context item
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 21[Gru08]
for $a in a
where $ab = foo
return for $c at $p in $ac
where $p = 2
return for $d in $cd
where $de = 42
return $d
bull FLWOR Reorder iteration result via order byndash In a FLWOR block for $v in e1 return e2 the order of e1determines the order of the resulting sequence
93 Iteration (FLWORs)
Reordering via order by
In the FLWOR block
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 22[Gru08]
In the FLWOR block
the value (atomization) of e3 determines the order in which the bindingsof $v are used to evaluate e2
for $v in e1order by e3 [ascendingdescending][empty greatestleast]return e2
bull FLWOR Reordering examples
93 Iteration (FLWORs)
An order by no-op reordering by sequence order
for $x at $p in (53142)
order by $p
return $x
rArrrArrrArrrArr (53142)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 23[Gru08]
All bound variables in scope in order by
for $x at $p in (53142)
order by $p + $x
return $x
rArrrArrrArrrArr (13524)
Reordering as in SQLs ORDER BY
for $x at $p in (53142)
order by $x
return $x
rArrrArrrArrrArr (12345)
bull FLWOR Reordering examplesndash Value-based reordering of an XPath step result
93 Iteration (FLWORs)
This query reorders the result of the XPath location step descendantb
based on (string) value Which result is to be expectedlet $a = ltagt
ltb id=0gt42ltbgt
ltb id=1gt5ltbgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 24[Gru08]
ltb id=1gt5ltbgt
ltb id=2gt
ltb id=3gt3ltbgt
ltb id=4gt1ltbgt
ltagt
for $b in $adescendantb
order by $btext() empty greatest
return $bid
Answer
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 25
bull FLWOR semantics tuple spacendash In the W3C XQuery specification the interaction of the five clauses of a FLWOR (for-let-where-order by-return) block is formally explained by means of a tuple space
94 FLOWR Semantics
means of a tuple spacebull Size of tuple space equiv number of iterations performed by FLWOR block
bull The fields of the tuples represent for each iteration
1 forlet variable bindings
2 the outcome of the where clause
3 the value of the reordering criterion and
4 the value returned by the return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 26[Gru08]
bull FLWOR semantics tuple space (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y where order by return
10 1 100 false 4 (110)
1 Complete tuple space
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 27[Gru08]
return ($p$x) 10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (2)2 Filtering where clause ($y lt= 42)
94 FLOWR Semantics
$x $p $y where order by return
10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 28[Gru08]
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (3)3 Reordering order by clause
94 FLOWR Semantics
$x $p $y where order by return
1 10 1 true -5 (101)
2 9 4 true -4 (92)
3 8 9 true -3 (83)
4 To emit the final result scan the tuple space in the order specified by the order by column and concatenate the return column entries
(1019283746556)
ndash Observation some values have been computed but never used
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 29[Gru08]
3 8 9 true -3 (83)
4 7 16 true -2 (74)
5 6 25 true -1 (65)
6 5 36 true 0 (56)
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull FLWOR Reorder iteration result via order byndash In a FLWOR block for $v in e1 return e2 the order of e1determines the order of the resulting sequence
93 Iteration (FLWORs)
Reordering via order by
In the FLWOR block
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 22[Gru08]
In the FLWOR block
the value (atomization) of e3 determines the order in which the bindingsof $v are used to evaluate e2
for $v in e1order by e3 [ascendingdescending][empty greatestleast]return e2
bull FLWOR Reordering examples
93 Iteration (FLWORs)
An order by no-op reordering by sequence order
for $x at $p in (53142)
order by $p
return $x
rArrrArrrArrrArr (53142)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 23[Gru08]
All bound variables in scope in order by
for $x at $p in (53142)
order by $p + $x
return $x
rArrrArrrArrrArr (13524)
Reordering as in SQLs ORDER BY
for $x at $p in (53142)
order by $x
return $x
rArrrArrrArrrArr (12345)
bull FLWOR Reordering examplesndash Value-based reordering of an XPath step result
93 Iteration (FLWORs)
This query reorders the result of the XPath location step descendantb
based on (string) value Which result is to be expectedlet $a = ltagt
ltb id=0gt42ltbgt
ltb id=1gt5ltbgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 24[Gru08]
ltb id=1gt5ltbgt
ltb id=2gt
ltb id=3gt3ltbgt
ltb id=4gt1ltbgt
ltagt
for $b in $adescendantb
order by $btext() empty greatest
return $bid
Answer
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 25
bull FLWOR semantics tuple spacendash In the W3C XQuery specification the interaction of the five clauses of a FLWOR (for-let-where-order by-return) block is formally explained by means of a tuple space
94 FLOWR Semantics
means of a tuple spacebull Size of tuple space equiv number of iterations performed by FLWOR block
bull The fields of the tuples represent for each iteration
1 forlet variable bindings
2 the outcome of the where clause
3 the value of the reordering criterion and
4 the value returned by the return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 26[Gru08]
bull FLWOR semantics tuple space (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y where order by return
10 1 100 false 4 (110)
1 Complete tuple space
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 27[Gru08]
return ($p$x) 10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (2)2 Filtering where clause ($y lt= 42)
94 FLOWR Semantics
$x $p $y where order by return
10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 28[Gru08]
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (3)3 Reordering order by clause
94 FLOWR Semantics
$x $p $y where order by return
1 10 1 true -5 (101)
2 9 4 true -4 (92)
3 8 9 true -3 (83)
4 To emit the final result scan the tuple space in the order specified by the order by column and concatenate the return column entries
(1019283746556)
ndash Observation some values have been computed but never used
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 29[Gru08]
3 8 9 true -3 (83)
4 7 16 true -2 (74)
5 6 25 true -1 (65)
6 5 36 true 0 (56)
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull FLWOR Reordering examples
93 Iteration (FLWORs)
An order by no-op reordering by sequence order
for $x at $p in (53142)
order by $p
return $x
rArrrArrrArrrArr (53142)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 23[Gru08]
All bound variables in scope in order by
for $x at $p in (53142)
order by $p + $x
return $x
rArrrArrrArrrArr (13524)
Reordering as in SQLs ORDER BY
for $x at $p in (53142)
order by $x
return $x
rArrrArrrArrrArr (12345)
bull FLWOR Reordering examplesndash Value-based reordering of an XPath step result
93 Iteration (FLWORs)
This query reorders the result of the XPath location step descendantb
based on (string) value Which result is to be expectedlet $a = ltagt
ltb id=0gt42ltbgt
ltb id=1gt5ltbgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 24[Gru08]
ltb id=1gt5ltbgt
ltb id=2gt
ltb id=3gt3ltbgt
ltb id=4gt1ltbgt
ltagt
for $b in $adescendantb
order by $btext() empty greatest
return $bid
Answer
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 25
bull FLWOR semantics tuple spacendash In the W3C XQuery specification the interaction of the five clauses of a FLWOR (for-let-where-order by-return) block is formally explained by means of a tuple space
94 FLOWR Semantics
means of a tuple spacebull Size of tuple space equiv number of iterations performed by FLWOR block
bull The fields of the tuples represent for each iteration
1 forlet variable bindings
2 the outcome of the where clause
3 the value of the reordering criterion and
4 the value returned by the return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 26[Gru08]
bull FLWOR semantics tuple space (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y where order by return
10 1 100 false 4 (110)
1 Complete tuple space
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 27[Gru08]
return ($p$x) 10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (2)2 Filtering where clause ($y lt= 42)
94 FLOWR Semantics
$x $p $y where order by return
10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 28[Gru08]
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (3)3 Reordering order by clause
94 FLOWR Semantics
$x $p $y where order by return
1 10 1 true -5 (101)
2 9 4 true -4 (92)
3 8 9 true -3 (83)
4 To emit the final result scan the tuple space in the order specified by the order by column and concatenate the return column entries
(1019283746556)
ndash Observation some values have been computed but never used
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 29[Gru08]
3 8 9 true -3 (83)
4 7 16 true -2 (74)
5 6 25 true -1 (65)
6 5 36 true 0 (56)
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull FLWOR Reordering examplesndash Value-based reordering of an XPath step result
93 Iteration (FLWORs)
This query reorders the result of the XPath location step descendantb
based on (string) value Which result is to be expectedlet $a = ltagt
ltb id=0gt42ltbgt
ltb id=1gt5ltbgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 24[Gru08]
ltb id=1gt5ltbgt
ltb id=2gt
ltb id=3gt3ltbgt
ltb id=4gt1ltbgt
ltagt
for $b in $adescendantb
order by $btext() empty greatest
return $bid
Answer
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 25
bull FLWOR semantics tuple spacendash In the W3C XQuery specification the interaction of the five clauses of a FLWOR (for-let-where-order by-return) block is formally explained by means of a tuple space
94 FLOWR Semantics
means of a tuple spacebull Size of tuple space equiv number of iterations performed by FLWOR block
bull The fields of the tuples represent for each iteration
1 forlet variable bindings
2 the outcome of the where clause
3 the value of the reordering criterion and
4 the value returned by the return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 26[Gru08]
bull FLWOR semantics tuple space (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y where order by return
10 1 100 false 4 (110)
1 Complete tuple space
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 27[Gru08]
return ($p$x) 10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (2)2 Filtering where clause ($y lt= 42)
94 FLOWR Semantics
$x $p $y where order by return
10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 28[Gru08]
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (3)3 Reordering order by clause
94 FLOWR Semantics
$x $p $y where order by return
1 10 1 true -5 (101)
2 9 4 true -4 (92)
3 8 9 true -3 (83)
4 To emit the final result scan the tuple space in the order specified by the order by column and concatenate the return column entries
(1019283746556)
ndash Observation some values have been computed but never used
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 29[Gru08]
3 8 9 true -3 (83)
4 7 16 true -2 (74)
5 6 25 true -1 (65)
6 5 36 true 0 (56)
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 25
bull FLWOR semantics tuple spacendash In the W3C XQuery specification the interaction of the five clauses of a FLWOR (for-let-where-order by-return) block is formally explained by means of a tuple space
94 FLOWR Semantics
means of a tuple spacebull Size of tuple space equiv number of iterations performed by FLWOR block
bull The fields of the tuples represent for each iteration
1 forlet variable bindings
2 the outcome of the where clause
3 the value of the reordering criterion and
4 the value returned by the return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 26[Gru08]
bull FLWOR semantics tuple space (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y where order by return
10 1 100 false 4 (110)
1 Complete tuple space
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 27[Gru08]
return ($p$x) 10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (2)2 Filtering where clause ($y lt= 42)
94 FLOWR Semantics
$x $p $y where order by return
10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 28[Gru08]
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (3)3 Reordering order by clause
94 FLOWR Semantics
$x $p $y where order by return
1 10 1 true -5 (101)
2 9 4 true -4 (92)
3 8 9 true -3 (83)
4 To emit the final result scan the tuple space in the order specified by the order by column and concatenate the return column entries
(1019283746556)
ndash Observation some values have been computed but never used
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 29[Gru08]
3 8 9 true -3 (83)
4 7 16 true -2 (74)
5 6 25 true -1 (65)
6 5 36 true 0 (56)
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull FLWOR semantics tuple spacendash In the W3C XQuery specification the interaction of the five clauses of a FLWOR (for-let-where-order by-return) block is formally explained by means of a tuple space
94 FLOWR Semantics
means of a tuple spacebull Size of tuple space equiv number of iterations performed by FLWOR block
bull The fields of the tuples represent for each iteration
1 forlet variable bindings
2 the outcome of the where clause
3 the value of the reordering criterion and
4 the value returned by the return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 26[Gru08]
bull FLWOR semantics tuple space (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y where order by return
10 1 100 false 4 (110)
1 Complete tuple space
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 27[Gru08]
return ($p$x) 10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (2)2 Filtering where clause ($y lt= 42)
94 FLOWR Semantics
$x $p $y where order by return
10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 28[Gru08]
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (3)3 Reordering order by clause
94 FLOWR Semantics
$x $p $y where order by return
1 10 1 true -5 (101)
2 9 4 true -4 (92)
3 8 9 true -3 (83)
4 To emit the final result scan the tuple space in the order specified by the order by column and concatenate the return column entries
(1019283746556)
ndash Observation some values have been computed but never used
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 29[Gru08]
3 8 9 true -3 (83)
4 7 16 true -2 (74)
5 6 25 true -1 (65)
6 5 36 true 0 (56)
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull FLWOR semantics tuple space (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y where order by return
10 1 100 false 4 (110)
1 Complete tuple space
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 27[Gru08]
return ($p$x) 10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (2)2 Filtering where clause ($y lt= 42)
94 FLOWR Semantics
$x $p $y where order by return
10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 28[Gru08]
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (3)3 Reordering order by clause
94 FLOWR Semantics
$x $p $y where order by return
1 10 1 true -5 (101)
2 9 4 true -4 (92)
3 8 9 true -3 (83)
4 To emit the final result scan the tuple space in the order specified by the order by column and concatenate the return column entries
(1019283746556)
ndash Observation some values have been computed but never used
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 29[Gru08]
3 8 9 true -3 (83)
4 7 16 true -2 (74)
5 6 25 true -1 (65)
6 5 36 true 0 (56)
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull FLWOR semantics tuple space (2)2 Filtering where clause ($y lt= 42)
94 FLOWR Semantics
$x $p $y where order by return
10 1 100 false 4 (110)
9 2 81 false 3 (29)
8 3 64 false 2 (38)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 28[Gru08]
8 3 64 false 2 (38)
7 4 49 false 1 (47)
6 5 36 true 0 (56)
5 6 25 true -1 (65)
4 7 16 true -2 (74)
3 8 9 true -3 (83)
2 9 4 true -4 (92)
1 10 1 true -5 (101)
bull FLWOR semantics tuple space (3)3 Reordering order by clause
94 FLOWR Semantics
$x $p $y where order by return
1 10 1 true -5 (101)
2 9 4 true -4 (92)
3 8 9 true -3 (83)
4 To emit the final result scan the tuple space in the order specified by the order by column and concatenate the return column entries
(1019283746556)
ndash Observation some values have been computed but never used
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 29[Gru08]
3 8 9 true -3 (83)
4 7 16 true -2 (74)
5 6 25 true -1 (65)
6 5 36 true 0 (56)
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull FLWOR semantics tuple space (3)3 Reordering order by clause
94 FLOWR Semantics
$x $p $y where order by return
1 10 1 true -5 (101)
2 9 4 true -4 (92)
3 8 9 true -3 (83)
4 To emit the final result scan the tuple space in the order specified by the order by column and concatenate the return column entries
(1019283746556)
ndash Observation some values have been computed but never used
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 29[Gru08]
3 8 9 true -3 (83)
4 7 16 true -2 (74)
5 6 25 true -1 (65)
6 5 36 true 0 (56)
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull FLWOR populate tuple space lazily (1)ndash Sample FLWOR block
94 FLOWR Semantics
for $x at $p in reverse(1 to 10)
let $y = $x $x
where $y lt= 42
order by 5 - $p
return ($p$x)
$x $p $y
10 1 100
1 Populate variable bindings only
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 30[Gru08]
return ($p$x) 10 1 100
9 2 81
8 3 64
7 4 49
6 5 36
5 6 25
4 7 16
3 8 9
2 9 4
1 10 1
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull FLWOR populate tuple space lazily (2)2 Evaluate where clause ($y lt= 42)
3 Prune tuples
94 FLOWR Semantics
$x $p $y where
10 1 100 false
9 2 81 false
8 3 64 false$x $p $y where
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 31[Gru08]
8 3 64 false
7 4 49 false
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
$x $p $y where
6 5 36 true
5 6 25 true
4 7 16 true
3 8 9 true
2 9 4 true
1 10 1 true
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull FLWOR populate tuple space lazily (3)4 Evaluate order by clause
5 Normalize order by column evaluate return clause
94 FLOWR Semantics
$x $p $y order by
6 5 36 0
5 6 25 -1
4 7 16 -2 evaluate return clause
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 32[Gru08]
4 7 16 -2
3 8 9 -3
2 9 4 -4
1 10 1 -5
$x $p $y position() return
6 5 36 6 (56)
5 6 25 5 (65)
4 7 16 4 (74)
3 8 9 3 (83)
2 9 4 2 (92)
1 10 1 1 (101)
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull Variable bindings Variables are not variablendash Imperative XQuery
ndash Equivalent query
94 FLOWR Semantics
Evaluate the expressionlet $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
let $x =
ltxgtltygt12ltygt
ltygt10ltygt
ltygt7ltygt
bull let-bound variables are named values and thus immutablebull Obtain equivalent query via textual replacement (lhs rarrrhs) (Not valid if rhs value depends on a node constructor)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 33[Gru08]
ltygt7ltygt
ltygt13ltygt
ltxgt
let $sum = 0
for $y in $xy
let $sum = $sum + $y
return $sum
ltygt7ltygt
ltygt13ltygt
ltxgt
for $y in $xy
return 0 + $y
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 34
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull Constructing XML fragmentsndash XQuery expressions may construct nodes with new identity of all 7 node kinds known in XML
bull document nodes elements attributes text nodes comments processing instructions (and namespace nodes)
95 Constructing XML Fragments
comments processing instructions (and namespace nodes)
ndash Since item sequences are flat the nested application of node constructors is the only way to hierarchically structure values in XQuery
bull Nested elements may be used to group or compose data andultimately
bull XQuery may be used as an XSLT replacement ie as an XML transformation language
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 35[Gru08]
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull Direct node constructors
ndash XQuery node constructors come in two flavors
1 direct constructors and
2 computed constructors
95 Constructing XML Fragments
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 36[Gru08]
Direct constructorsThe syntax of direct constructors exactly matches the XML syntax any well-formed XML fragment f also is a correct XQuery expression (which when evaluated yields f )
Note Text content and CDATA sections are both mapped into text nodes by the XQuery data model (CDATA isnt remembered)
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull Direct element constructorsndash CDATA isnt remembered
ndash The tag name of a direct constructor is constant its
95 Constructing XML Fragments
ltxgtlt[CDATA[foo amp bar]]gtltxgt equiv ltxgtfoo ampamp barltxgtXQuery
ndash The tag name of a direct constructor is constant its content however may be computed by any XQuery expression enclosed in curly braces bullbullbull
bull Computed element content
ndashDouble curly braces ( or ) may be used to create content containing literal curly braces
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 37[Gru08]
ltxgt4 max((120)) ltxgt rArrrArrrArrrArr ltxgt42ltxgt
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull Computed element constructors
ndash Definition
95 Constructing XML Fragments
In a computed element constructorelement e1 e2
expression e1 (of type string or QName) determines the
element name e determines the sequence of nodes in the
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 38[Gru08]
element name e2 determines the sequence of nodes in the elements content
Example computed element name and content
element string-join((foobar)-) 40+2
rArr ltfoo-bargt42ltfoo-bargt
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull Constructing XML Fragments
95 Constructing XML Fragments
An application of computed element constructors i18nConsider a dictionary in XML format (bound to variable $dict) with entries
likeltentry word=addressgt
ltvariant lang=degtAdresseltvariantgt
ltvariant lang=itgtindirizzoltvariantgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 39[Gru08]
ltvariant lang=itgtindirizzoltvariantgt
ltentrygt
We can use this dictionary to translate the tag name of an XML element $e
into Italian as follows preserving its contents
element
$dictentry[word=name($e)]variant[lang=it]
$e $enode()
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull Direct and computed attribute constructorsndash In direct attribute constructors computed content may be embedded using curly braces
95 Constructing XML Fragments
Computed attribute content
ltx a=(42)gt rArrrArrrArrrArr ltx a=4 2gt
ltx a= b=gt rArrrArrrArrrArr ltx b= a=gt
rArrrArrrArrrArr
ndash A computed attribute constructor attribute e1 e2 allows to construct parent-less attributes (impossible in XML) with computed names and content
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 40[Gru08]
ltx a= b=gt ltx b= a=gt
ltx a= b=gt rArrrArrrArrrArr ltx a= b=ampquotgt
A computed and re-parented attribute
let $a = attribute a sum((402))
return ltxgt $a ltxgt
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull Text node constructors
95 Constructing XML Fragments
Text nodes may be constructed in one of three ways
1 Characters in element content2 via lt[CDATA[bullbullbull]]gt or
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 41[Gru08]
2 via lt[CDATA[bullbullbull]]gt or3 using the computed text constructor text e
Content sequence e is atomized to yield a sequence of typeanyAtomicType The atomic values are converted to type string and then concatenated with an intervening ˽If e is () no text node is constructed ndash the constructor yields ()
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull Examples computed text node constructorndash Explicit semantics of text node construction text e
95 Constructing XML Fragments
if (empty(e))then ()
else text string-join(for $i in data(e)return string($i)
˽)
ndash Text node construction examples
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 42[Gru08]
˽)
text (123) equiv text 1 2 3
let $n = ltxgt
ltygtltzgt
ltxgtname()
return lttgt text $n lttgt
rArrrArrrArrrArr lttgtx y zlttgt
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull XML documents vs fragmentsndash Unlike XML fragments an XML document is rooted in its document
node The difference is observable via XPathbull Remember the (invisible) document root node
95 Constructing XML Fragments
doc(xyxml) rArrrArrrArrrArr ltxgtltygtltxgt
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
ltxgt
ltygt
xyxml
ndash A document node may be constructed via document e bull Creating a document node
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 43[Gru08]
ltxgtltygtltxgt rArrrArrrArrrArr ltygt
The context node for the first expression above is the document node for document xyxml
ltygt
ltxgt
(document ltxgtltygtltxgt ) rArrrArrrArrrArr ltxgtltygtltxgt
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull Processing element contentndash The XQuery element constructor is quite flexible the content sequence is not restricted and may have typeitem
ndash Yet the content of an element needs to be of typenode
95 Constructing XML Fragments
nodebull Consecutive literal characters yield a single text node containing these characters
bull Expression enclosed in bullbullbull are evaluatedbull Adjacent atomic values are cast to type string and collected in a single text node with intervening ˽
bull A node is copied into the content together with its content All copied nodes receive a new identity
bull Then adjacent text nodes are merged by concatenating their content Text nodes with content are dropped
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 44[Gru08]
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull Example processing element contentndash Evaluate the expression below
95 Constructing XML Fragments
count(
ltxgtFortytwo40 + 2 foo31415ltygtltzgtltygt
()[1] ltxgtnode())
ndash Solution
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 45[Gru08]
The constructed node is
x
text y
z
Fortytwo42foo˽31415
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull Well-formed element contentndash XML fragments constructed by XQuery expressions are subject to the XML rules of well-formedness eg
bull no two attributes of the same element may share a namebull attribute nodes precede any other element content(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
95 Constructing XML Fragments
(The content type needs to be a subtype of attribute()(element()|text()|bullbullbull))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 46[Gru08]
Violating the well-formedness ruleslet $id = id
return
element x rArrrArrrArrrArr (dynamic error)attribute $id 0
attribute id 1
ltxgtfoo attribute id 0 ltxgt) rArrrArrrArrrArr (type error)
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull Construction generates new node identities
ndash
95 Constructing XML Fragments
element x e Deep subtree copy
y y
eequivequivequivequiv
x
notnotnotnotis
ndash Node constructors have side effects
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 47[Gru08]
let $e = ltagtltbgtltcgtltygtfooltygtltcgtltagt
let $x = element x $e rArrrArrrArrrArr false()
return exactly-one($ey) is exactly-one($xy)
Observing node identity
let $x = ltxgt
return $x is $x
let $d = doc(uri)
return $d is $drArr rArr rArr rArr true() rArrrArrrArrrArrtrue()
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull Construction establishes document order
ndash Result of the following query
95 Constructing XML Fragments
let $x = ltxgt
let $y = ltygt
let $unrelated = ($x $y)
let $related = ltzgt $unrelated ltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 48[Gru08]
let $related = ltzgt $unrelated ltzgt
return ($unrelated[1] ltlt $unrelated[2]
$related[1] ltlt $related[2] )
Solution
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull Construction pair join partners
ndash A join query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 49[Gru08]
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
for $z in $xz[id eq $c] ( join predicate )
return ltpairgt $c$ztext() ltpairgt
Result
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull Grouping (attempt 1)ndash A grouping query
95 Constructing XML Fragments
let $a = ltagtltbgtltcgt0ltcgtltbgt
ltbgtltcgt0ltcgtltcgt1ltcgtltcgt2ltcgtltbgt
ltagt
let $x = ltxgtltz id=2gttwoltzgtltz id=0gtzeroltzgt
bull Aggregate functions (sum count ) may be applied to group members ie element mem inside each group
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 50[Gru08]
ltygtltz id=0gtzeroltzgtltz id=3gtthreeltzgtltygt
ltxgt
for $c in $abc
return ltgroupgt
$c ltmemgt for $z in $xz[id eq $c]
return $ztext() ltmemgt
ltgroupgt
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull Grouping (attempt 1)
ndash Result (NB group of ltcgt0ltcgt appears twice)
95 Constructing XML Fragments
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt0ltcgtltmemgtzerozeroltmemgtltgroupgt
ltgroupgtltcgt1ltcgtltmemgtltgroupgt
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
larr empty group
ndash Remarksbull The preservation of the empty group for ltcgt1ltcgt
resembles the effect of a relational left outer join
bull The duplicate elimination implicit in $abc is based on node identity but we group by value (id eq $c)
rArr Such groupings call for value-based duplicate elimination
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 51[Gru08]
ltgroupgtltcgt2ltcgtltmemgttwoltmemgtltgroupgt
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull Grouping (attempt 2)
ndash Improved grouping query
95 Constructing XML Fragments
let $a = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
let $x = bullbullbullbull bullbullbullbull bullbullbullbull unchanged bullbullbullbull bullbullbullbull bullbullbullbull
for $c in distinct-values($abc)
return ltgroupgt
ndash Notebull Need to rebuild element c ($c bound to values)
bull Inner for loop replaced by equivalent XPath expression
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 52[Gru08]
return ltgroupgt
ltcgt $c ltcgt
ltmemgt $xz[id eq $c]text() ltmemgt
ltgroupgt
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
91 Introduction
92 Preliminaries
93 Iteration (FLWORs)
9 XQuery
94 FLWOR Semantics
95 Constructing XML Fragments
96 User-Defined Functions
97 Overview and References
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 53
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull XQuery user-defined functionsndash It is typical for non-toy XQuery expressions to contain user-defined functions which encapsulate query details
bull User-defined functions may be collected into modules and then imported by a query
bull Function declarations may be directly embedded into the query
96 User-Defined Functions
bull Function declarations may be directly embedded into the query prolog (prepended to query separated by )
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 54[Gru08]
Declaration of n-ary function f with body e
declare function f ($p1 as t1 $pn as tn) as t0 e
bull If tiis omitted it defaults to item()
bull The pair (f n) is required to be unique (overloading)bull Atomization is applied to the i-th parameter if ti is atomic
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull User-defined function examples
96 User-Defined Functions
Form textual root-to-node paths
declare default function namespace
httpwww-dbintumdeXQueryfunctions
declare function path($n as node()) as xsstring
fnstring-join(for $a in $nancestor-or-self
ndash May not place user-defd functions in the XQuery builtin function namespace (predefined prefix fn)rArr Use explicit prefix for user-defd or builtin functions
[Gru08]
fnstring-join(for $a in $nancestor-or-self
return fnname($a) )
let $a = ltagtltbgtltcgtltdgtltcgtltdgtltbgtltagt
return $adpath()
rArrrArrrArrrArr (abcdabd)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 55
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull User-defined function examples
96 User-Defined Functions
Reverse a sequence
Reversing a sequence does not inspect the sequences items in any way
declare function reverse($seq)
for $i at $p in $seq
ndash NoteThe calls f() and f(()) invoke different functions
[Gru08]
for $i at $p in $seq
order by $p descending
return $i
reverse((42altbgtdoc(fooxml)))
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 56
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull User-defined functions recursionndash Trees are the prototypical recursive data structure in Computer Science and it is natural to describe computations over trees in a recursive fashion(This is a general and powerful principle in programming derive a functions implementation from the shape of the data it operates over)
96 User-Defined Functions
[Gru08]
Simulate XPath ancestor via parent axis
declare function ancestors($n as node()) as node()
if (fnempty($n)) then ()
else (ancestors($n) $n)
Questions
1 Will the result be in document order and duplicate free2 What if we declare the parameter type as node()
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 57
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull User-defined functions recursion examples
96 User-Defined Functions
Purpose of function hmm and output of this query
declare function localhmm($e as node()) as xsinteger
if (fnempty($e)) then 1
else fnmax(for $c in $e
return localhmm($c)) + 1
ndash Good stylebull Use predefined namespace local for user-defd functionsbull hmm has a more efficient equivalent (cf a previous slide 16) exploiting the recursion built into axes descendant and ancestor
[Gru08]
return localhmm($c)) + 1
localhmm(ltagtltbgt
ltbgtltcgtltdgtfooltdgtltegtltcgtltbgt
ltagt)
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 58
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull User-defined functions rename attribute
96 User-Defined Functions
Rename attribute $from to $todeclare function localxlate($n as node()
$from as xsstring
$to as xsstring)
typeswitch ($n)
case $e as element() return
[Gru08]
case $e as element() return
let $a = ($e)[name() eq $from]
return
element
node-name($e)
$e( except $a)
if ($a) then attribute $to data($a)
else ()
for $c in $enode()
return localxlate($c $from $to)
default return $n
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 59
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull User-defined functions rename attribute
ndash NB This constructs an entirely new tree
ndash In XQuery 10 there is currently no way to
96 User-Defined Functions
Invoke xlate
localxlate(ltx id=0 foo=gt
foo
lty zoo=1gtbarltygt
ltxgt
foo
dArrdArrdArrdArr
is currently no way to modify the properties or content of a node
ndash XQuery Update will fill in this gap (work in progress at W3C)
ndash NB XSLT (see above) has been designed to support XMLtransformations like the one exemplified here
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 60
foo
bar)
dArrdArrdArrdArr
ltx id=0 bar=gt
foo
lty zoo=1gtbarltygt
ltxgt
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull XQuery the missing piecesndash This chapter did not cover XQuery exhaustively
ndash This course will not cover the following XQuery aspects
bull (namespaces)
96 User-Defined Functions
bull (namespaces)
bull modules (declaration and import)
bull collations (string equality and comparison)
ndash Reminder XQuery specificationhttpwwww3orgTRxquery(Has become a W3C Recommendation in January 2007)
[Gru08] XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 61
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
1 Introduction
2 XML Basics
3 Schema definition
4 XML query languages I
5 Mapping relational data
8 XML query languages II ndashXQuery Data Model
9 XML query languages III ndash XQuery
10XML storage I ndash
97 Overview
uumluumluumluuml
uumluumluumluuml
uumluuml
uumluuml5 Mapping relational data
to XML
6 SQLXML
7 XML processing
10XML storage I ndashOverview
11 XML storage II
12 Updates Transactions
13 Systems
62XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
uumluumluumluuml
uumluuml
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull Database-Supported XML Processors [Gru08]ndash Th Grust
ndash Lecture Uni Tuumlbingen WS 0809
bull XML and Databases [Scholl07]ndash M Scholl
97 References
ndash M Scholl
ndash Lecture Uni Konstanz WS0708
bull Querying XML ndash XQuery XPath and SQLXML in Contextndash J Melton S Buxton
ndash Morgan KaufmannElsevier 2006 ISBN 978-1558607118
63XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull XQuery Grundlagen und fortgeschrittene Methoden [LS04]
ndash W Lehner H Schoumlning
ndash dpunktverlag 2004
97 References
bull XML und Datenbanken [KM03]
ndash M Klettke H Meier
ndash dpunktverlag 2003
64XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65
bull Now or
bull Room IZ 232
bull Office our Tuesday 1230 ndash 1330 Uhr
Questions Ideas Comments
bull Office our Tuesday 1230 ndash 1330 Uhr
or on appointment
bull Email ecksteinifiscstu-bsde
XML Databases ndash Silke Eckstein ndash Institut fuumlr Informationssysteme ndash TU Braunschweig 65