Download XQuery: Element Constructors, Sequence Operators, Conditional Expressions, Order, Quantifi and more Study notes Database Programming in PDF only on Docsity!
INFS 740, Prof. A. Brodsky, IT&E, GMU. Based on material by Prof. X.S. Wang, UVM
Xquery
Prof. Alex Brodsky
INFS 740
GMU
INFS 740, Prof. A. Brodsky, IT&E, GMU. Based on material by Prof. X.S. Wang, UVM
Element constructors
• To create or add to XML documents• Can use straight XML
– Create a student with name and ID
Laurie
Reeves
123456
INFS 740, Prof. A. Brodsky, IT&E, GMU. Based on material by Prof. X.S. Wang, UVM
Another FLWR Example
Return
a
list
of
students
who
have
taken
at
least
courses
FOR
$p
IN
document(
“students.xml")//student
LET
$b
document(
“courses.xml)//course[student
$p] WHERE
count($b)
RETURN
$p
INFS 740, Prof. A. Brodsky, IT&E, GMU. Based on material by Prof. X.S. Wang, UVM
Sequence Operators
•^
Comma separated values^ – 1, 2, 3
•^
Optional brackets^ – (1, 2, 3)– (1,2,(3,4)) = (1,2,3,4)
•^
TO^ – binary operator– Changes operands to integers– e.g. 2 to 5 = (2,3,4,5)
INFS 740, Prof. A. Brodsky, IT&E, GMU. Based on material by Prof. X.S. Wang, UVM
Conditional expressions
• IF, THEN, and ELSE FOR
$h
IN
//item
RETURN
{$h/name,
IF
($h/@type
“Student")
THEN
$h/ID
ELSE
$h/description
INFS 740, Prof. A. Brodsky, IT&E, GMU. Based on material by Prof. X.S. Wang, UVM
Order in XML
• BEFORE and AFTER
LET $p := //course[1]FOR $e IN //* AFTER ($p//midterm_1)[1] BEFORE
($p//midterm_2)[2]
RETURN $e
INFS 740, Prof. A. Brodsky, IT&E, GMU. Based on material by Prof. X.S. Wang, UVM
Quantifiers
• Tests for existence of some elements that
satisfy a condition
• Also used to test whether all elements in a
collection satisfy a condition
• XQuery provides for existential (“some”)
and universal (“every”) quantifiers
•^
Key words
satisfies
and
contains
INFS 740, Prof. A. Brodsky, IT&E, GMU. Based on material by Prof. X.S. Wang, UVM
Quantified expressions
SOME variable IN expr SATISFIES expre.g. Find the names of faculty who teach a
course in which the description containsJava and XML
FOR $b IN
//faculty
WHERE
SOME $p IN $b//course/description
SATISFIES
(contains($p,
“java") AND
contains($p,
“XML"))
RETURN $b/name
INFS 740, Prof. A. Brodsky, IT&E, GMU. Based on material by Prof. X.S. Wang, UVM
Filtering
•^
“filter” is a function in the XQuery core functionlibrary
•^
Takes two operands, each of which is anexpression
•^
Returns copies of the nodes in the first operandthat are top level nodes in the second operand
•^
If the two operands don’t have a common root,then filter returns an empty list
INFS 740, Prof. A. Brodsky, IT&E, GMU. Based on material by Prof. X.S. Wang, UVM
Filtering – Diagram
filter(/C, //A | //B)
INFS 740, Prof. A. Brodsky, IT&E, GMU. Based on material by Prof. X.S. Wang, UVM
Test or modify datatypes
-^
INSTANCEOF – Boolean binary operator, returnstrue if left operand is the same datatype as theright^ $x INSTANCEOF zoonames:animal{5} instance of element(*, xs:integer)
-^
CAST – changes the datatype^ (x DIV y) CAST AS integer
-^
TREAT – treats one datatype as another for agiven expression^ $mypet TREAT AS Cat
INFS 740, Prof. A. Brodsky, IT&E, GMU. Based on material by Prof. X.S. Wang, UVM
Explicit Data Types
import schema "ipo.xsd"declare namespace ipo="http://www.example.com/IPO"define function check-address( $a as ipo:address? )
as xsd:boolean
{ … }for $x in document(“foo”)//ipo:invoicewhere $x/ipo:shipTo
instance of
ipo:USAddress
INFS 740, Prof. A. Brodsky, IT&E, GMU. Based on material by Prof. X.S. Wang, UVM
Conformance Features
• Basic XQuery
– Cannot import XML Schemas; use “built-in”
type names only
– But data do have types– Types are checked dynamically
• Schema Import Feature
– Import schemas: give names to element and
attribute types
INFS 740, Prof. A. Brodsky, IT&E, GMU. Based on material by Prof. X.S. Wang, UVM
Conformance Features
• Static Typing Feature
– Use the type rules specified in the Formal
Semantics Document to check for type errorsbefore query execution.