
Objectives An Introduction to XML and Web Technologies How XML generalizes relational databases The XQuery language Querying XML Documents How XML may be supported in databases with XQuery Anders Møller & Michael I. Schwartzbach © 2006 Addison-Wesley An Introduction to XML and Web Technologies 2 XQuery 1.0 From Relations to Trees XML documents naturally generalize database relations XQuery is the corresponding generalization of SQL An Introduction to XML and Web Technologies 3 An Introduction to XML and Web Technologies 4 1 Only Some Trees are Relations Trees Are Not Relations They have height two Not all trees satisfy the previous characterization The root has an unbounded number of children Trees are ordered, while both rows and columns All nodes in the second layer (records) have a of tables may be permuted without changing the fixed number of child nodes (fields) meaning of the data An Introduction to XML and Web Technologies 5 An Introduction to XML and Web Technologies 6 A Student Database A More Natural Model (1/2) <students> <student id="100026"> <name>Joe Average</name> <age>21</age> <major>Biology</major> <results> <result course="Math 101" grade="C-"/> <result course="Biology 101" grade="C+"/> <result course="Statistics 101" grade="D"/> </results> </student> An Introduction to XML and Web Technologies 7 An Introduction to XML and Web Technologies 8 2 A More Natural Model (2/2) Usage Scenario: Data-Oriented <student id="100078"> We want to carry over the kinds of queries that we <name>Jack Doe</name> <age>18</age> performed in the original relational model <major>Physics</major> <major>XML Science</major> <results> <result course="Math 101" grade="A"/> <result course="XML 101" grade="A-"/> <result course="Physics 101" grade="B+"/> <result course="XML 102" grade="A"/> </results> </student> </students> An Introduction to XML and Web Technologies 9 An Introduction to XML and Web Technologies 10 Usage Scenario: Document-Oriented Usage Scenario: Programming Queries could be used Queries could be used to automatically generate • to retrieve parts of documents documentation • to provide dynamic indexes • to perform context-sensitive searching • to generate new documents as combinations of existing documents An Introduction to XML and Web Technologies 11 An Introduction to XML and Web Technologies 12 3 Usage Scenario: Hybrid XQuery Design Requirements Queries could be used to data mine hybrid data, Must have at least one XML syntax and at least such as patient records one human-readable syntax Must be declarative Must be namespace aware Must coordinate with XML Schema Must support simple and complex datatypes Must combine information from multiple documents Must be able to transform and create XML trees An Introduction to XML and Web Technologies 13 An Introduction to XML and Web Technologies 14 Relationship to XPath Relationship to XSLT XQuery 1.0 is a strict superset of XPath 2.0 XQuery and XSLT are both domain-specific Every XPath 2.0 expression is directly an XQuery languages for combining and transforming XML 1.0 expression (a query) data from multiple sources The extra expressive power is the ability to They are vastly different in design, partly for • join information from different sources and historical reasons • generate new XML fragments XQuery is designed from scratch, XSLT is an intellectual descendant of CSS Technically, they may emulate each other An Introduction to XML and Web Technologies 15 An Introduction to XML and Web Technologies 16 4 XQuery Prolog More From the Prolog Like XPath expressions, XQuery expressions are declare default element namespace URI; evaluated relatively to a context declare default function namespace URI; import schema at URI; This is explicitly provided by a prolog declare namespace NCName = URI; Settings define various parameters for the XQuery processor language, such as: xquery version "1.0"; declare xmlspace preserve; declare xmlspace strip; An Introduction to XML and Web Technologies 17 An Introduction to XML and Web Technologies 18 Implicit Declarations XPath Expressions declare namespace xml = XPath expressions are also XQuery expressions "http://www.w3.org/XML/1998/namespace"; declare namespace xs = The XQuery prolog gives the required static "http://www.w3.org/2001/XMLSchema"; context declare namespace xsi = "http://www.w3.org/2001/XMLSchema-instance"; The initial context node, position, and size are declare namespace fn = undefined "http://www.w3.org/2005/11/xpath-functions"; declare namespace xdt = "http://www.w3.org/2005/11/xpath-datatypes"; declare namespace local = "http://www.w3.org/2005/11/xquery-local-functions"; An Introduction to XML and Web Technologies 19 An Introduction to XML and Web Technologies 20 5 Datatype Expressions XML Expressions Same atomic values as XPath 2.0 XQuery expressions may compute new XML Also lots of primitive simple values: nodes xs:string("XML is fun") Expressions may denote element, character data, xs:boolean("true") xs:decimal("3.1415") comment, and processing instruction nodes xs:float("6.02214199E23") xs:dateTime("1999-05-31T13:20:00-05:00") Each node is created with a unique node identity xs:time("13:20:00-05:00") Constructors may be either direct or computed xs:date("1999-05-31") xs:gYearMonth("1999-05") xs:gYear("1999") xs:hexBinary("48656c6c6f0a") xs:base64Binary("SGVsbG8K") xs:anyURI("http://www.brics.dk/ixwt/") xs:QName("rcp:recipe") An Introduction to XML and Web Technologies 21 An Introduction to XML and Web Technologies 22 Direct Constructors Namespaces in Constructors (1/3) Uses the standard XML syntax declare default element namespace "http://businesscard.org"; <card> The expression <name>John Doe</name> <title>CEO, Widget Inc.</title> <foo><bar/>baz</foo> <email>[email protected]</email> <phone>(202) 555-1414</phone> evaluates to the given XML fragment <logo uri="widget.gif"/> Note that </card> <foo/> is <foo/> evaluates to false An Introduction to XML and Web Technologies 23 An Introduction to XML and Web Technologies 24 6 Namespaces in Constructors (2/3) Namespaces in Constructors (3/3) declare namespace b = "http://businesscard.org"; <card xmlns="http://businesscard.org"> <b:card> <name>John Doe</name> <b:name>John Doe</b:name> <title>CEO, Widget Inc.</title> <b:title>CEO, Widget Inc.</b:title> <email>[email protected]</email> <b:email>[email protected]</b:email> <phone>(202) 555-1414</phone> <b:phone>(202) 555-1414</b:phone> <logo uri="widget.gif"/> <b:logo uri="widget.gif"/> </card> </b:card> An Introduction to XML and Web Technologies 25 An Introduction to XML and Web Technologies 26 Enclosed Expressions Explicit Constructors <foo>1 2 3 4 5</foo> <card xmlns="http://businesscard.org"> <foo>{1, 2, 3, 4, 5}</foo> <name>John Doe</name> <title>CEO, Widget Inc.</title> <foo>{1, "2", 3, 4, 5}</foo> <email>[email protected]</email> <foo>{1 to 5}</foo> <phone>(202) 555-1414</phone> <foo>1 {1+1} {" "} {"3"} {" "} {4 to 5}</foo> <logo uri="widget.gif"/> </card> element card { <foo bar="1 2 3 4 5"/> namespace { "http://businesscard.org" }, element name { text { "John Doe" } }, <foo bar="{1, 2, 3, 4, 5}"/> element title { text { "CEO, Widget Inc." } } , <foo bar="1 {2 to 4} 5"/> element email { text { "[email protected]" } }, element phone { text { "(202) 555-1414" } }, element logo { attribute uri { "widget.gif" } } } An Introduction to XML and Web Technologies 27 An Introduction to XML and Web Technologies 28 7 Computed QNames Biliingual Business Cards element { if ($lang="Danish") then "kort" else "card" } { element { "card" } { namespace { "http://businesscard.org" }, namespace { "http://businesscard.org" }, element { if ($lang="Danish") then "navn" else "name" } element { "name" } { text { "John Doe" } }, { text { "John Doe" } }, element { "title" } { text { "CEO, Widget Inc." } }, element { if ($lang="Danish") then "titel" else "title" } element { "email" } { text { "[email protected]" } }, { text { "CEO, Widget Inc." } }, element { "phone" } { text { "(202) 555-1414" } }, element { "email" } element { "logo" } { { text { "[email protected]" } }, attribute { "uri" } { "widget.gif" } element { if ($lang="Danish") then "telefon" else "phone"} } { text { "(202) 456-1414" } }, } element { "logo" } { attribute { "uri" } { "widget.gif" } } } An Introduction to XML and Web Technologies 29 An Introduction to XML and Web Technologies 30 FLWOR Expressions The Difference Between For and Let (1/4) Used for general queries: for $x in (1, 2, 3, 4) let $y := ("a", "b", "c") return ($x, $y) <doubles> { for $s in fn:doc("students.xml")//student let $m := $s/major 1, a, b, c, 2, a, b, c, 3, a, b, c, 4, a, b, c where fn:count($m) ge 2 order by $s/@id return <double> { $s/name/text() } </double> } </doubles> An Introduction to XML and Web Technologies 31 An Introduction to XML and Web Technologies 32 8 The Difference Between For and Let (2/4) The Difference Between For and Let (3/4) let $x in (1, 2, 3, 4) for $x in (1, 2, 3, 4) for $y := ("a", "b", "c") for $y in ("a", "b", "c") return ($x, $y) return ($x, $y) 1, 2, 3, 4, a, 1, 2, 3, 4, b, 1, 2, 3, 4, c 1, a, 1, b, 1, c, 2, a, 2, b, 2, c, 3, a, 3, b, 3, c, 4, a, 4, b, 4, c An Introduction to XML and Web Technologies 33 An Introduction to XML and Web Technologies 34 The Difference Between For and Let (4/4) Computing Joins let $x := (1, 2, 3, 4) What recipes can we (sort of) make? let $y := ("a", "b", "c") return ($x, $y) declare namespace rcp = "http://www.brics.dk/ixwt/recipes"; for $r in fn:doc("recipes.xml")//rcp:recipe
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages15 Page
-
File Size-