Presentation is loading. Please wait.

Presentation is loading. Please wait.

XML, XML Schema, XPath and XQuery Query Languages CS561 Slides collated from several sources, including D. Suciu at Univ. of Washington.

Similar presentations


Presentation on theme: "XML, XML Schema, XPath and XQuery Query Languages CS561 Slides collated from several sources, including D. Suciu at Univ. of Washington."— Presentation transcript:

1 XML, XML Schema, XPath and XQuery Query Languages CS561 Slides collated from several sources, including D. Suciu at Univ. of Washington

2 XML Data

3 CS561 - Spring 2007.3 XML W3C standard to complement HTML origins: structured text SGML motivation: –HTML describes presentation –XML describes content HTML e XML subset SGML

4 CS561 - Spring 2007.4 From HTML to XML HTML describes the presentation

5 CS561 - Spring 2007.5 HTML Bibliography Foundations of Databases Abiteboul, Hull, Vianu Addison Wesley, 1995 Data on the Web Abiteboul, Buneman, Suciu Morgan Kaufmann, 1999

6 CS561 - Spring 2007.6 XML Foundations… Abiteboul Hull Vianu Addison Wesley 1995 … XML describes the content

7 CS561 - Spring 2007.7 XML Terminology tags: book, title, author, … start tag:, end tag: elements: …, … elements are nested empty element: abbrv. an XML document: single root element well formed XML document: if it has matching tags

8 CS561 - Spring 2007.8 XML: Attributes Foundations of Databases Abiteboul … 1995 attributes are alternative ways to represent data

9 CS561 - Spring 2007.9 More XML: Oids and References Jane Mary John oids and references in XML are just syntax

10 CS561 - Spring 2007.10 So Far Differences between “xml data” versus “relational data” ? –Data model? –Typed? –Homogeneity? –Correctness? –Usage/Purpose ?

11 CS561 - Spring 2007.11 “XML Data Model” Numerous competing models: Document Object Model (DOM): –class hierarchy (node, element, attribute,…) –defines API to inspect/modify the document XML query data model (formal)

12 CS561 - Spring 2007.12 XML Namespaces http://www.w3.org/TR/REC-xml-names name ::= [prefix:]localpart … 15 …. … 15 ….

13 CS561 - Spring 2007.13 … … XML Namespaces syntactic:, semantic: provide URL for “shared” schema defined here

14 CS561 - Spring 2007.14 So Far What are “namespaces” good for ? Are they typically available for relational databases?

15 Schemas for XML

16 CS561 - Spring 2007.16 DTD - Element Type Definitions

17 CS561 - Spring 2007.17 XML Schemas generalizes DTDs (SGML derivative) now, instead uses XML syntax two main documents: structure and data types XML Schema more powerful but more complex

18 CS561 - Spring 2007.18 XML Schema </xsd:complexType </xsd:complexType DTD:

19 CS561 - Spring 2007.19 So Far Differences between “xml schema” versus “relational schema” ? –Purpose ? Do we need it ? –Definition time? –Strictness of typing ? –Underlying model ?

20 CS561 - Spring 2007.20 Elements versus Types in XML Schema DTD:

21 CS561 - Spring 2007.21 Types: –Simple types (integers, strings,...) –Complex types (regular expressions, like in DTDs) Element-type-element alternation: –Root element has a complex type –Complex type is a regular expression of elements –Those elements have their complex types... –... –Leaves have simple types Elements versus Types in XML Schema

22 CS561 - Spring 2007.22 Local and Global Types in XML Schema Local type: [define locally the person’s type] Global type: [define here the type ttt] Global types: can be reused in other elements

23 CS561 - Spring 2007.23 Local v.s. Global Elements in XML Schema Local element:... Global element:... Global elements: like in DTDs

24 CS561 - Spring 2007.24 Regular Expressions in XML Schema Recall the element-type-element alternation: [regular expression on elements] Regular expressions: A B C..

25 CS561 - Spring 2007.25 Regular Expressions in XML Schema Regular expressions: A B C = A B C A B C = A | B | C A B C = (A B C).. = (...)*.. = (...)?

26 CS561 - Spring 2007.26 Regular Expressions in XML Schema Recall the element-type-element alternation: [regular expression on elements] Regular expressions: A B C = A B C A B C = A | B | C A B C = (A B C).. = (...)*.. = (...)?

27 CS561 - Spring 2007.27 Attributes in XML Schema............ Attributes are associated with the type, not the element Only to complex types; more trouble if we want to add attributes to simple types.

28 CS561 - Spring 2007.28 Derived Types by Extensions Corresponds to inheritance

29 Key Constraints in XML

30 CS561 - Spring 2007.30 Keys in XML Schema Lawnmower Baby Monitor Lapis Necklace Sturdy Shelves Lawnmower Baby Monitor Lapis Necklace Sturdy Shelves XML: XML Schema for Key :

31 CS561 - Spring 2007.31 Keys in XML Schema In general, syntax is :...... Notes: All XPath expressions “start” at the element currently being defined The fields must identify a single “node”.

32 CS561 - Spring 2007.32 Keys in XML Schema Unique = guarantees uniqueness Key = guarantees uniqueness and existence All XPath expressions are “restricted”: –/a/b | /a/c OK for selector –//a/b/*/c OK for field Note: better than DTD’s ID mechanism

33 CS561 - Spring 2007.33 Examples of Keys in XML Schema Examples Note: Must have single firstname, Single surname

34 CS561 - Spring 2007.34 Foreign Keys in XML Schema Example

35 CS561 - Spring 2007.35 So Far Differences between “keys/foreign-keys”in xml versus relational model? –Purpose ? –Underlying model ?

36 XPath “The Basic Building Block”

37 CS561 - Spring 2007.37 XPath Goal = Permit access some nodes from document XPath main construct : Axis navigation Navigation step : axis + node-test + predicates Examples –descendant::node() –child::author –attribute::booktitle =“XML” XPath path consists of one or more navigation steps, separated by “/” Navigation step : axis + node-test + predicates Examples –/descendant::node()/child::author –/descendant::node()/child::author[parent/attribute::booktitle =“XML”][2]

38 CS561 - Spring 2007.38 XPath Goal = Permit access some nodes from document XPath main construct : Axis navigation Navigation step : axis + node-test + predicates Examples –descendant::node() –child::author –attribute::booktitle =“XML”

39 CS561 - Spring 2007.39 XPath XPath path consists of one or more navigation steps, separated by “/” Navigation step : axis + node-test + predicates Examples –/descendant::node() /child::author –/descendant::node() /child::author [parent /attribute::booktitle =“XML”][2] XPath offers shortcuts : –no axis means child –//  / descendant-or-self::node()/

40 CS561 - Spring 2007.40 XPath- Child Axis Navigation author is shorthand for child::author. Examples: –aaa -- all the children nodes labeled aaa –aaa/bbb -- all the bbb grandchildren of aaa children –*/ bbb all the bbb grandchildren of any child Notes: –. -- the context node –/ -- the root node aaa bbb cccaaa bbb ccc 1 23 4 567 context node

41 CS561 - Spring 2007.41 XPath- Child Axis Navigation author is shorthand for child::author. Examples: –aaa -- all the children nodes labeled aaa (1,3) –aaa/bbb -- all the bbb grandchildren of aaa children (4) –*/ bbb all the bbb grandchildren of any child (4,6) Notes: –. -- the context node –/ -- the root node aaa bbb cccaaa bbb ccc 1 23 4 567 context node

42 CS561 - Spring 2007.42 XPath- Child Axis Navigation –/ doc -- all doc children of the root –./ aaa -- all aaa children of the context node (equivalent to aaa ) –text() -- all text children of context node –node() -- all children of the context node (includes text and attribute nodes) –.. -- parent of the context node –.// -- the context node and all its descendants –// -- the root node and all its descendants –//text() -- all the text nodes in the document

43 CS561 - Spring 2007.43 Predicates –[2] -- the second child node of the context node –chapter[5] -- the fifth chapter child of context node –[last()] -- the last child node of the context node –chapter[title=“introduction”] -- the chapter children of the context node that have one or more title children whose string-value is “introduction” (string-value is concatenation of all text on descendant text nodes) –person[.//firstname = “joe”] -- the person children of the context node that have in their descendants a firstname element with string-value “ Joe ”

44 CS561 - Spring 2007.44 Axis navigation So far, our expressions have moved us down by moving to children nodes. Exceptions are : –. stay where you are –/ go to the root –// all descendants of the root –.// all descendants of the context node

45 CS561 - Spring 2007.45 Axis navigation XPath has several axes: ancestor, ancestor-or-self, attribute, child, descendant, descendant-or-self, following, following-sibling, namespace, parent, preceding, preceding-sibling, self Some of these describe single nodes: – self, parent Some describe sequences of nodes: –All others

46 CS561 - Spring 2007.46 XPath Navigation Axes ancestor descendant followingpreceding following-siblingpreceding-sibling child attribute namespace self

47 CS561 - Spring 2007.47 XPath Abbreviated Syntax (nothing)child:: @attribute:: ///descendant-or-self::node().self::node().//descendant-or-self::node..parent::node() /(document root)

48 CS561 - Spring 2007.48 XPath Widely adopted -- in XML-Schema and in many query languages. About as expressive as regular path expressions

49 CS561 - Spring 2007.49 So Far Differences between SQL and XPATH? What are similar query capabilities? What features does SQL have, but not XPATH? What features does XPATH support, but not SQL? Is XPath a full-fledged query language?

50 Query Languages - XQuery

51 CS561 - Spring 2007.51 Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resources XQuery: A Query Language for XML XQuery: A Query Language for XML Chamberlin, Florescu, et al. W3C recommendation: www.w3.org/TR/xquery/

52 CS561 - Spring 2007.52 XQuery Designed based on Quilt (which is based on XML-QL) http://www.w3.org/TR/xquery/2/2001 XML Query data model (ordered)

53 CS561 - Spring 2007.53 FLWR (“Flower”) Expressions FOR... LET... FOR... LET... WHERE... RETURN...

54 CS561 - Spring 2007.54 XQuery Find the titles of all books published after 1995: FOR $x IN document("bib.xml") /bib/book WHERE $x/year > 1995 RETURN $x/title FOR $x IN document("bib.xml") /bib/book WHERE $x/year > 1995 RETURN $x/title How does result look like?

55 CS561 - Spring 2007.55 XQuery Find all book titles published after 1995: FOR $x IN document("bib.xml") /bib/book WHERE $x/year > 1995 RETURN $x/title FOR $x IN document("bib.xml") /bib/book WHERE $x/year > 1995 RETURN $x/title Result: abc def ghi

56 CS561 - Spring 2007.56 XQuery Example FOR $a IN ( document("bib.xml") /bib/book[publisher=“Morgan Kaufmann”]/author) RETURN $a, FOR $t IN /bib/book[author=$a]/title RETURN $t FOR $a IN ( document("bib.xml") /bib/book[publisher=“Morgan Kaufmann”]/author) RETURN $a, FOR $t IN /bib/book[author=$a]/title RETURN $t

57 CS561 - Spring 2007.57 XQuery Example FOR $a IN ( document("bib.xml") /bib/book[publisher=“Morgan Kaufmann”]/author) RETURN $a, FOR $t IN /bib/book[author=$a]/title RETURN $t FOR $a IN ( document("bib.xml") /bib/book[publisher=“Morgan Kaufmann”]/author) RETURN $a, FOR $t IN /bib/book[author=$a]/title RETURN $t For each author of a book by Morgan Kaufmann, list all books she published: What is query result ?

58 CS561 - Spring 2007.58 XQuery Result: Jones abc def Jones abc def Smith ghi

59 CS561 - Spring 2007.59 XQuery Example: Duplicates For each author of a book by Morgan Kaufmann, list all books she published: FOR $a IN distinct( document("bib.xml") /bib/book[publisher=“Morgan Kaufmann”]/author) RETURN $a, FOR $t IN /bib/book[author=$a]/title RETURN $t FOR $a IN distinct( document("bib.xml") /bib/book[publisher=“Morgan Kaufmann”]/author) RETURN $a, FOR $t IN /bib/book[author=$a]/title RETURN $t distinct = a function that eliminates duplicates

60 CS561 - Spring 2007.60 Example XQuery Result Result: Jones abc def Smith ghi

61 CS561 - Spring 2007.61 XQuery FOR $x in expr – binds $x to each element in the list expr –Useful for iteration over some input list LET $x = expr – binds $x to the entire list expr –Useful for common subexpressions and for grouping and aggregations

62 CS561 - Spring 2007.62 XQuery with LET Clause count = a (aggregate) function that returns number of elements FOR $p IN distinct(document("bib.xml")//publisher) LET $b := document("bib.xml")/book[publisher = $p] WHERE count($b) > 100 RETURN $p FOR $p IN distinct(document("bib.xml")//publisher) LET $b := document("bib.xml")/book[publisher = $p] WHERE count($b) > 100 RETURN $p

63 CS561 - Spring 2007.63 XQuery Find books whose price is larger than average: LET $a = avg( document("bib.xml") /bib/book/@price) FOR $b in document("bib.xml") /bib/book WHERE $b/@price > $a RETURN $b LET $a = avg( document("bib.xml") /bib/book/@price) FOR $b in document("bib.xml") /bib/book WHERE $b/@price > $a RETURN $b

64 CS561 - Spring 2007.64 FOR versus LET FOR Binds node variables  iteration LET Binds collection variables  one value

65 CS561 - Spring 2007.65 FOR v.s. LET FOR $x IN document("bib.xml") /bib/book RETURN $x FOR $x IN document("bib.xml") /bib/book RETURN $x Returns:... LET $x := document("bib.xml") /bib/book RETURN $x LET $x := document("bib.xml") /bib/book RETURN $x Returns:...

66 CS561 - Spring 2007.66 Collections in XQuery Ordered and unordered collections –/bib/book/author = an ordered collection –distinct(/bib/book/author) = an unordered collection LET $a = /bib/book  $a is a collection $b/author  a collection (several authors...) RETURN $b/author Returns:...

67 CS561 - Spring 2007.67 XQuery Summary FOR-LET-WHERE-RETURN = FLWR FOR/LET Clauses WHERE Clause RETURN Clause List of tuples Instances of XQuery data model

68 CS561 - Spring 2007.68 XQuery Some more query features

69 CS561 - Spring 2007.69 Sorting in XQuery FOR $p IN distinct(document("bib.xml")//publisher) RETURN $p/text(), FOR $b IN document("bib.xml")//book[publisher = $p] RETURN $b/title, $b/@price SORTBY (price DESCENDING) SORTBY (name) FOR $p IN distinct(document("bib.xml")//publisher) RETURN $p/text(), FOR $b IN document("bib.xml")//book[publisher = $p] RETURN $b/title, $b/@price SORTBY (price DESCENDING) SORTBY (name)

70 CS561 - Spring 2007.70 Sorting in XQuery Sorting arguments: refer to name space of RETURN clause, not of FOR clause TIP: To sort on an element you don’t want to display, first return it, then remove it with an additional query.

71 CS561 - Spring 2007.71 If-Then-Else FOR $h IN //holding RETURN $h/title, IF $h/@type = "Journal" THEN $h/editor ELSE $h/author SORTBY (title) FOR $h IN //holding RETURN $h/title, IF $h/@type = "Journal" THEN $h/editor ELSE $h/author SORTBY (title)

72 CS561 - Spring 2007.72 Existential Quantifiers FOR $b IN //book WHERE SOME $p IN $b//para SATISFIES contains($p, "sailing") AND contains($p, "windsurfing") RETURN $b/title FOR $b IN //book WHERE SOME $p IN $b//para SATISFIES contains($p, "sailing") AND contains($p, "windsurfing") RETURN $b/title

73 CS561 - Spring 2007.73 Universal Quantifiers FOR $b IN //book WHERE EVERY $p IN $b//para SATISFIES contains($p, "sailing") RETURN $b/title FOR $b IN //book WHERE EVERY $p IN $b//para SATISFIES contains($p, "sailing") RETURN $b/title

74 CS561 - Spring 2007.74 So Far Similarities between SQL and XQuery? Differences between SQL and XQuery?

75 XML, XML Data Model XML Schema, XPath XQuery


Download ppt "XML, XML Schema, XPath and XQuery Query Languages CS561 Slides collated from several sources, including D. Suciu at Univ. of Washington."

Similar presentations


Ads by Google