XQuery Formal Semantics

Normalization

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                             

 

Example

 [<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 ()

 }

Static Semantics

Typing

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

Inference rules

         statEnvExpr0 : xs:integer

         statEnvExpr1 : xs:integer

     -------------------------------

         statEnvExpr0 < Expr1 : xs:boolean

 

         statEnvExpr0 : xs:boolean

         statEnvExpr1 : Type1

         statEnvExpr2 : Type2

     -------------------------------------------------

         statEnvif (Expr0 )then Expr1 else Expr2 : (Type1 | Type2)

 

         statEnvExpr0 : Type0

         statEnv  + varType(Var : Type0)├ Expr1:Type1

     -------------------------------------------------

         statEnvlet $Var := Expr0 return Expr1 : Type1

    

Dynamic Semantics

         dynEnvExpr0 => Integer0

         dynEnvExpr1 => Integer1

     -------------------------------

         dynEnvExpr0 < Expr1 => Integer<Integer1

 

         dynEnvExpr0 : fn:true()

         dynEnvExpr1 => Value

     -----------------------------------------

         dynEnvif (Expr0 )then Expr1 else Expr2 => Value

 

         dynEnvExpr0 => Value0

         dynEnv  + varValue(Var => Value0)├ Expr1 =>Value1

     -----------------------------------------

         dynEnvlet $Var := Expr0 return Expr1 => Value1

 

         dynEnvExpr0 => (Item1 , ... , Itemn)

         dynEnv  + varValue(Var0 =>Item1,Var1 =>1)├ Expr1 =>Value1

                           ...

         dynEnv  + varValue(Var0 =>Itemn,Var1 =>n)├ Expr1 =>Valuen

     ------------------------------------------------------------

         dynEnvfor $Var0  at $Var1  in Expr0 return Expr1 => (Value1 , ... , Valuen)

Dealing with data store

         Store0;dynEnvExpr => Value1; Store1

         Store1;dynEnvValueerases to Value2; Store2

         NodeId not in dom(Store2)

         Store3=Store2 U store(NodeId => element ElementName {Value2})

            Store3; dynEnvvalidate { element ElementName {Value2} } => Value3; Store4

     --------------------------------------------------------

         Store0;dynEnvelement ElementName {Expr} => Value3; Store4

Relating static and dynamic environments

    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(xmatches statEnv.varType(x)

            Type soundness for values

if        dynEnv matches statEnv           and

         dynEnvExpr => Value              and

         statEnvExpr : Type

then   Value matches Type

            Type soundness for errors

if        dynEnv matches statEnv           and

         dynEnvExpr raises Error     and

         statEnvExpr : Type

then   Error  typeErr

Example

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>

statEnvQ : element (biblio, element(citation,  element(cite, fs:anyType) )* )

dynEnvQ =>

<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>

References and related reading

Querying XML, Chapter 10.8, XQuery from the Experts, Chapters 4-5

W3C Semantics

Simeon03