XQuery Formal Semantics
XQuery core
[Clause1 Clause2 return Expr]Expr ==
[Clause1
return [Clause2
return Expr]Expr]Expr
[for ForBinding1,
ForBinding2 return
Expr]Expr ==
[for ForBinding1
return [for ForBinding2
return Expr]Expr]Expr
[where Expr1
return Expr2]Expr
==
[if (Expr1)
then Expr2
else ()]Expr
==
if (fn:boolean([Expr1]Expr))then [Expr2]Expr
else ()
[PathExpr]Expr == fs:distinct-doc-order([PathExpr]Path)
[StepExpr / RelativePathExpr]Path ==
let $fs:sequence :=
fs:distinct-doc-order([StepExpr]Path)return
let $fs:last := fn:count($fs:sequence)
return
for
$fs:dot at $fs:position in $fs:sequence return
[RelativePathExpr]Path
[<biblio>
{
for $r in
doc("eb-bib.xml")//cite
where $r/author
return
<citation>{$r}</citation>
}
</biblio>]Expr ==
element biblio
{
for $r in
fs:distinct-doc-order
(let
$fs:seq := fs:distinct-doc-order(doc("eb-bib.xml")) return
let $fs:last := fn:count($fs:seq) return
for $fs:dot AT $fs:position in $fs:seq return
descendant-or-self::cite) return
if
(fn:boolean(fs:distinct-doc-order
(let $fs:seq := fs:distinct-doc-order($r) return
let $fs:last := fn:count($fs:seq) return
for $fs:dot AT $fs:position in $fs:seq return
child::author)) then
element citation {$r}
else
()
}
Judgements
statEnv
├ Expr : Type
in environment
StatEnv, expression Expr
has type Type
Atomic types
(e.g., xs:integer, xs:decimal, xs:double, xs:string)
Union types
(e.g., (xs:string | xs:decimal) )
Sequence
types (e.g., xs:integer*, xs:string?, xs:double+, empty())
Complex
types (e.g., element(biblio), element(biblio,myNames:biblio.type),
attribute(@*,myNames:date.type))
xs:anyType
statEnv
├ Expr0 : xs:integer
statEnv
├ Expr1 : xs:integer
-------------------------------
statEnv
├ Expr0 < Expr1 : xs:boolean
statEnv
├ Expr0 : xs:boolean
statEnv
├ Expr1 : Type1
statEnv
├ Expr2 : Type2
-------------------------------------------------
statEnv
├ if (Expr0
)then
Expr1 else
Expr2 : (Type1 | Type2)
statEnv
├ Expr0 : Type0
statEnv
+ varType(Var : Type0)├ Expr1:Type1
-------------------------------------------------
statEnv
├ let
$Var := Expr0 return Expr1 : Type1
dynEnv
├ Expr0 => Integer0
dynEnv
├ Expr1 => Integer1
-------------------------------
dynEnv
├ Expr0 < Expr1 => Integer0 <Integer1
dynEnv
├ Expr0 : fn:true()
dynEnv
├ Expr1 => Value
-----------------------------------------
dynEnv
├ if (Expr0
)then
Expr1 else
Expr2 => Value
dynEnv
├ Expr0 => Value0
dynEnv
+ varValue(Var => Value0)├ Expr1 =>Value1
-----------------------------------------
dynEnv
├ let
$Var := Expr0 return Expr1 => Value1
dynEnv
├ Expr0 => (Item1 , ... , Itemn)
dynEnv
+ varValue(Var0 =>Item1,Var1 =>1)├ Expr1 =>Value1
...
dynEnv
+ varValue(Var0 =>Itemn,Var1 =>n)├ Expr1 =>Valuen
------------------------------------------------------------
dynEnv
├ for
$Var0 at $Var1 in Expr0 return Expr1
=> (Value1 , ... , Valuen)
Store0;dynEnv
├ Expr => Value1; Store1
Store1;dynEnv
├ Value1 erases to Value2; Store2
NodeId
not in dom(Store2)
Store3=Store2
U store(NodeId => element ElementName {Value2})
Store3;
dynEnv ├ validate
{ element
ElementName {Value2} } => Value3; Store4
--------------------------------------------------------
Store0;dynEnv
├ element
ElementName {Expr} => Value3; Store4
V
matches
T
is a judgment that a value V is of type T
dynEnv
matches
statEnv if dom(dynEnv.varValue)
= dom(statEnv.varType)
and
x in dom(") implies
dynEnv.varValue(x) matches statEnv.varType(x)
if
dynEnv matches
statEnv and
dynEnv ├ Expr => Value and
statEnv ├ Expr : Type
then
Value matches Type
if
dynEnv matches
statEnv and
dynEnv ├ Expr raises
Error and
statEnv ├ Expr : Type
then
Error ≠ typeErr
Assume eb-bib.xml with data shown and query Q
<biblio>
{
for $r in doc("eb-bib.xml")//cite
where $r/author
return
<citation>{$r}</citation>
}
</biblio>
statEnv ├ Q : element (biblio,
element(citation, element(cite, fs:anyType) )* )
dynEnv ├ Q =>
<biblio><citation><cite
type="full"><author>W.L. Morton</author><title
edition="2" date="1969">The Kingdom of
Canada</title></cite></citation><citation><cite
type="full"><author>H.A. Innis</author><title
edition="2" date="1956">The Fur Trade in
Canada</title></cite></citation><citation><cite
type="author><author>W.S.
MacNutt</author></cite></citation><citation><cite
type="author><author>Fernand
Ouellet</author></cite></citation></biblio>
Querying XML, Chapter 10.8, XQuery from the Experts, Chapters 4-5
|