Module 6 Module 6 XQuery XQuery
XML queries XML queries An XQuery basic structure: An XQuery basic structure: a a prolog prolog + an + an expression expression Role of the prolog: Role of the prolog: Populate the context where the expression is compiled Populate the context where the expression is compiled and evaluated and evaluated Prologue contains: Prologue contains: namespace definitions namespace definitions schema imports schema imports default element and function namespace default element and function namespace function definitions function definitions collations declarations collations declarations function library imports function library imports global and external variables definitions global and external variables definitions etc etc 01/31/07 2
XQuery expressions XQuery expressions XQuery Expr :=Constants | Variable | FunctionCalls | PathExpr Constants | Variable | FunctionCalls | PathExpr XQuery Expr := ComparisonExpr | ArithmeticExpr | LogicExpr | ComparisonExpr | ArithmeticExpr | LogicExpr | FLWRExpr | ConditionalExpr | QuantifiedExpr | FLWRExpr | ConditionalExpr | QuantifiedExpr | TypeSwitchExpr | InstanceofExpr | CastExpr | TypeSwitchExpr | InstanceofExpr | CastExpr | UnionExpr | IntersectExceptExpr | UnionExpr | IntersectExceptExpr | ConstructorExpr | ValidateExpr ConstructorExpr | ValidateExpr Expressions can be nested with full generality ! Expressions can be nested with full generality ! Functional programming heritage (ML, Haskell, Lisp) Functional programming heritage (ML, Haskell, Lisp) 01/31/07 3
Constants Constants XQuery grammar has built-in support for: XQuery grammar has built-in support for: Strings: Strings: “125.0” or ‘125.0’ “125.0” or ‘125.0’ Integers: Integers: 150 150 Decimal: Decimal: 125.0 125.0 Double: Double: 125.e2 125.e2 19 other 19 other atomic types atomic types available via XML Schema available via XML Schema Values can be constructed Values can be constructed with constructors in F&O doc: with constructors in F&O doc: fn:true(), fn:date(“2002-5-20”) fn:true(), fn:date(“2002-5-20”) by casting by casting by schema validation by schema validation 01/31/07 4
Variables Variables $ + Qname (e.g. $x, $ns:foo) $ + Qname (e.g. $x, $ns:foo) bound, not assigned bound, not assigned XQuery does not allow variable assignment XQuery does not allow variable assignment created by created by let , for , some/every, typeswitch let , for , some/every, typeswitch expressions, function parameters expressions, function parameters example: example: let $x := ( 1, 2, 3 ) let $x := ( 1, 2, 3 ) return count($x) return count($x) above scoping ends at conclusion of above scoping ends at conclusion of return expression return expression 01/31/07 5
A built-in function sampler A built-in function sampler fn:document(xs:anyURI)=> document? fn:document(xs:anyURI)=> document? fn:empty(item*) => boolean fn:empty(item*) => boolean fn:index-of(item*, item) => xs:unsignedInt? fn:index-of(item*, item) => xs:unsignedInt? fn:distinct-values(item*) => item* fn:distinct-values(item*) => item* fn:distinct-nodes(node*) => node* fn:distinct-nodes(node*) => node* fn:union(node*, node*) => node* fn:union(node*, node*) => node* fn:except(node*, node*) => node* fn:except(node*, node*) => node* fn:string-length(xs:string?) => xs:integer? fn:string-length(xs:string?) => xs:integer? fn:contains(xs:string, xs:string) => xs:boolean fn:contains(xs:string, xs:string) => xs:boolean fn:true() => xs:boolean fn:true() => xs:boolean fn:date(xs:string) => xs:date fn:date(xs:string) => xs:date fn:add-date(xs:date, xs:duration) => xs:date fn:add-date(xs:date, xs:duration) => xs:date See Functions and Operators W3C specification See Functions and Operators W3C specification 01/31/07 6
Atomization Atomization fn:data(item*) -> fn:data(item*) -> xs:anyAtomicType* Extracting the “value” of a node, or returning Extracting the “value” of a node, or returning the atomic value the atomic value fn:data(<a>001</a>) fn:data(<a>001</a>) (“001”, xs:untypedAtomic) (“001”, xs:untypedAtomic) fn:data(validate {<a xsi:type=“xs:integer”>001</a>}) fn:data(validate {<a xsi:type=“xs:integer”>001</a>}) (1, xs:integer) (1, xs:integer) Implicitly applied: •Arithmetic expressions Arithmetic expressions • •Comparison expressions Comparison expressions • •Function calls and returns Function calls and returns • •Cast expressions Cast expressions • •Constructor expressions for various kinds of nodes • Constructor expressions for various kinds of nodes 01/31/07 •order by order by clauses in FLWOR expressions 7 • clauses in FLWOR expressions
Constructing sequences Constructing sequences (1, 2, 2, 3, 3, <a/>, <b/>) (1, 2, 2, 3, 3, <a/>, <b/>) “ “,” is the sequence concatenation operator ,” is the sequence concatenation operator Nested sequences are flattened: Nested sequences are flattened: (1, 2, 2, (3, 3)) => (1, 2, 2, 3,3) (1, 2, 2, (3, 3)) => (1, 2, 2, 3,3) range expressions: range expressions: (1 to 3) => (1, 2,3) (1 to 3) => (1, 2,3) 01/31/07 8
Combining sequences Combining sequences Union, Intersect, Except Union, Intersect, Except Work only for sequences of nodes, not atomic values Work only for sequences of nodes, not atomic values Eliminate duplicates and reorder to document order Eliminate duplicates and reorder to document order $x := <a/>, $y := <b/>, $z := <c/> $x := <a/>, $y := <b/>, $z := <c/> ($x, $y) union ($y, $z) => (<a/>, <b/>, ($x, $y) union ($y, $z) => (<a/>, <b/>, <c/>) <c/>) F&O specification provides other functions & F&O specification provides other functions & operators; eg. fn:distinct-values() and operators; eg. fn:distinct-values() and particularly useful fn:distinct-nodes() particularly useful fn:distinct-nodes() 01/31/07 9
Arithmetic expressions Arithmetic expressions 1 + 4 $a div 5 1 + 4 $a div 5 5 div 6 $b mod 10 5 div 6 $b mod 10 1 - (4 * 8.5) -55.5 1 - (4 * 8.5) -55.5 <a>42</a> + 1 <a>baz</a> + 1 <a>42</a> + 1 <a>baz</a> + 1 validate {<a xsi:type=“xs:integer”>42</a> }+ 1 validate {<a xsi:type=“xs:integer”>42</a> }+ 1 validate {<a xsi:type=“xs:string”>42</a> }+ 1 validate {<a xsi:type=“xs:string”>42</a> }+ 1 Apply the following rules: Apply the following rules: atomize atomize all operands. if either operand is (), => () all operands. if either operand is (), => () if an operand is untyped, cast to if an operand is untyped, cast to xs:double (if unable, => error) xs:double (if unable, => error) if the operand types differ but can be if the operand types differ but can be promoted promoted to common type, do so to common type, do so (e.g.: xs:integer can be promoted to xs:double ) (e.g.: xs:integer can be promoted to xs:double ) if operator is consistent w/ types, apply it; result is either atomic if operator is consistent w/ types, apply it; result is either atomic value or error value or error if type is not consistent, throw type exception if type is not consistent, throw type exception 01/31/07 10
Logical expressions Logical expressions expr1 and and expr2 expr2 expr1 expr1 expr1 or or expr2 expr2 fn:not fn:not() as a function () as a function return return true, false true, false Different from SQL Different from SQL two two value logic, value logic, not not three three value logic value logic Different from imperative languages Different from imperative languages and and , , or or are commutative in Xquery, but not in Java. are commutative in Xquery, but not in Java. if (($x castable as xs:integer) and (($x cast as xs:integer) eq 2) ) ….. if (($x castable as xs:integer) and (($x cast as xs:integer) eq 2) ) ….. Non-deterministic Non-deterministic false and error => false or or error ! (non-deterministically) error ! (non-deterministically) false and error => false • Rules: Rules: first compute the first compute the Boolean Effective Value (BEV) Boolean Effective Value (BEV) for each operand: for each operand: if (), “”, NaN, 0, then return if (), “”, NaN, 0, then return false false if the operand is of type xs:boolean, return it; if the operand is of type xs:boolean, return it; If operand is a sequence with first item a node, return true If operand is a sequence with first item a node, return true else raises an error else raises an error then use standard two value Boolean logic on the two BEV's as appropriate then use standard two value Boolean logic on the two BEV's as appropriate 01/31/07 11
Comparisons Comparisons for comparing single Value eq, ne, lt, values le, gt, ge General Existential =, !=, <=, quantification + <, >, >= automatic type coercion Node for testing identity of is, isnot single nodes testing relative position Order <<, >> of one node vs. another (in document order) 01/31/07 12
Recommend
More recommend