XML, XML Schema, Xpath and XQuery Slides collated from various sources, many from Dan Suciu at Univ. of Washington.

Slides:



Advertisements
Similar presentations
XML: Extensible Markup Language
Advertisements

Web Data Management XQuery 1. In this lecture Summary of XQuery FLWOR expressions – For, Let, Where, Order by, Return FOR and LET expressions Collections.
XML May 3 rd, XQuery Based on Quilt (which is based on XML-QL) Check out the W3C web site for the latest. XML Query data model –Ordered !
Managing XML and Semistructured Data Lecture 12: XML Schema Prof. Dan Suciu Spring 2001.
1 Web Data Management XML Schema. 2 In this lecture XML Schemas Elements v. Types Regular expressions Expressive power Resources W3C Draft:
Database Management Systems, R. Ramakrishnan1 Introduction to Semistructured Data and XML Chapter 27, Part D Based on slides by Dan Suciu University of.
CIS550 Handout 6 1 XPATH. CIS550 Handout 6 2 XPath Primary goal = to permit to access some nodes from a given document XPath main construct : axis navigation.
Database Management Systems, R. Ramakrishnan1 Introduction to Semistructured Data and XML Chapter 27, Part D Based on slides by Dan Suciu University of.
Agenda from now on Done: SQL, views, transactions, conceptual modeling, E/R, relational algebra. Starting: XML To do: the database engine: –Storage –Query.
Querying XML (cont.). Comments on XPath? What’s good about it? What can’t it do that you want it to do? How does it compare, say, to SQL?
1 Lecture 10 XML Wednesday, October 18, XML Outline XML (4.6, 4.7) –Syntax –Semistructured data –DTDs.
QSX (LN 3)1 Query Languages for XML XPath XQuery XSLT (not being covered today!) (Slides courtesy Wenfei Fan, Univ Edinburgh and Bell Labs)
1 Lecture 10: Database Design XML Wednesday, October 20, 2004.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 357 Database Systems I Query Languages for XML.
1 COS 425: Database and Information Management Systems XML and information exchange.
Query Languages - XQuery Slides partially from Dan Suciu.
Managing XML and Semistructured Data Lecture 6: XPath Prof. Dan Suciu Spring 2001.
XML May 1 st, XML for Representing Data John 3634 Sue 6343 Dick 6363 John 3634 Sue 6343 Dick 6363 row name phone “John”3634“Sue”“Dick” persons.
XML, XML Schema, Xpath and Xquery Slides collated from various sources, many from Dan Suciu at Univ. of Washington.
XML and Databases 198:541. XML Motivation  Huge amounts of unstructured data on the web: HTML documents  No structure information  Only format instructions.
XML, XML Schema, XPath and XQuery Query Languages CS561 Slides collated from several sources, including D. Suciu at Univ. of Washington.
1 Lecture 08: XML and Semistructured Data. 2 Outline XML (Section 17) –XML syntax, semistructured data –Document Type Definitions (DTDs) XPath.
Managing XML and Semistructured Data Lecture 2: XML Prof. Dan Suciu Spring 2001.
1 Lecture 08: XML and Semistructured Data. 2 Outline XML (Section 17) –XML syntax, semistructured data –Document Type Definitions (DTDs) XPath.
Xpath to XQuery February 23rd, Other Stuff HW 3 is out. Instructions for Phase 3 are out. Today: finish Xpath, start and finish Xquery. From Wednesday:
1 Lecture 16: Querying XML Data: XPath, XQuery Friday, February 11, 2005.
Querying XML February 12 th, Querying XML Data XPath = simple navigation through the tree XQuery = the SQL of XML XSLT = recursive traversal –will.
Introduction to XPath Bun Yue Professor, CS/CIS UHCL.
Lecture 6 of Advanced Databases XML Schema, Querying & Transformation Instructor: Mr.Ahmed Al Astal.
Introduction to XQuery Resources: Official URL: Short intros:
XML by Dan Suciu 1 Introduction to Semistructured Data and XML Based on slides by Dan Suciu University of Washington.
XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.
TDDD43 XML and RDF Slides based on slides by Lena Strömbäck and Fang Wei-Kleiner 1.
1/17 ITApplications XML Module Session 7: Introduction to XPath.
End of XML February 19 th, FLWR (“Flower”) Expressions FOR... LET... WHERE... RETURN... FOR... LET... WHERE... RETURN...
CIS550 Handout 7 Fall CIS 550 Handout 7 -- XPATH and XQuery.
Fall 2001 CSE3301 Query Languages for XML. Fall 2001 CSE3302 Why a query language? Extracting, Restructuring, Integration, Browsing… XML-QL
XPath. Why XPath? Common syntax, semantics for [XSLT] [XPointer][XSLT] [XPointer] Used to address parts of an XML document Provides basic facilities for.
Lecture 6: XML Query Languages Thursday, January 18, 2001.
Database Systems Part VII: XML Querying Software School of Hunan University
Lecture 5: XML Tuesday, January 16, Outline XML, DTDs (Data on the Web, 3.1) Semistructured data in XML (3.2) Exporting Relational Data in XML (8.3.1)
CSE 636 Data Integration Fall 2006 XML Query Languages XPath.
PROCESSING AND QUERYING XML 1. ROADMAP Models for Parsing XML Documents XPath Language XQuery Language XML inside DBMSs 2.
WPI, MOHAMED ELTABAKH PROCESSING AND QUERYING XML 1.
XML query. introduction An XML document can represent almost anything, and users of an XML query language expect it to perform useful queries on whatever.
1 Introduction to Semistructured Data and XML. 2 How the Web is Today  HTML documents often generated by applications consumed by humans only easy access:
More XML: semantics, DTDs, XPATH February 18, 2004.
The Semistructured-Data Model Programming Languages for XML Spring 2011 Instructor: Hassan Khosravi.
1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries.
1 Lecture 13: XQuery XML Publishing, XML Storage Monday, October 28, 2002.
XPath --XML Path Language Motivation of XPath Data Model and Data Types Node Types Location Steps Functions XPath 2.0 Additional Functionality and its.
CSE 6331 © Leonidas Fegaras XQuery 1 XQuery Leonidas Fegaras.
XQuery 1. In this lecture Summary of XQuery FLWOR expressions – For, Let, Where, Order by, Return FOR and LET expressions Collections and sorting 2.
Lecture 17: XPath and XQuery Wednesday, Nov. 7, 2001.
1 CSE544: Lecture 7 XQuery, Relational Algebra Monday, 4/22/02.
1 Lecture 12: XML, XPath, XQuery Friday, October 24, 2003.
1 The XPath Language. 2 XPath Expressions Flexible notation for navigating around trees A basic technology that is widely used uniqueness and scope in.
SEMI-STRUCTURED DATA (XML) 1. SEMI-STRUCTURED DATA ER, Relational, ODL data models are all based on schema Structure of data is rigid and known is advance.
Lecture 14: Relational Algebra Projects XML?
Management of XML and Semistructured Data
Querying XML and Semistructured Data
Management of XML and Semistructured Data
Lecture 12: XML, XPath, XQuery
Semi-Structured data (XML Data MODEL)
Alin Deutsch, University of Pennsylvania Mary Mernandez, AT&T Labs
Lecture 9: XML Monday, October 17, 2005.
Introduction to Database Systems CSE 444 Lecture 10 XML
Semi-Structured data (XML)
Lecture 11: XML and Semistructured Data
XML, XML Schema, XPath and XQuery Query Languages
Presentation transcript:

XML, XML Schema, Xpath and XQuery Slides collated from various sources, many from Dan Suciu at Univ. of Washington

CS561 - Spring XML W3C standard to complement HTML origins: structured text SGML motivation: –HTML describes presentation –XML describes content (version 2, 10/2000)

CS561 - Spring From HTML to XML HTML describes the presentation

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

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

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

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

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

CS561 - Spring XML Namespaces (1/99) name ::= [prefix:]localpart … 15 …. … 15 ….

CS561 - Spring … … XML Namespaces syntactic:, semantic: provide URL for schema defined here

CS561 - Spring XML Data Model Several competing models: Document Object Model (DOM): – / (2/2001) –class hierarchy (node, element, attribute,…) –objects have behavior –defines API to inspect/modify the document Infoset - PSV (post schema validation) XML Query data model

CS561 - Spring XML Schemas 1/10/2000 generalizes DTDs uses XML syntax two documents: structure and datatypes – – XML-Schema is complex

CS561 - Spring XML Schemas DTD:

CS561 - Spring Elements v.s. Types in XML Schema DTD:

CS561 - Spring Types: –Simple types (integers, strings,...) –Complex types (regular expressions, like in DTDs) Element-type-element alternation: –Root element has a complex type –That type is a regular expression of elements –Those elements have their complex types... –... –On the leaves we have simple types Elements v.s. Types in XML Schema

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

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

CS561 - Spring 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).. = (...)*.. = (...)?

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

CS561 - Spring Derived Types by Extensions Corresponds to inheritance

CS561 - Spring Keys in XML Schema Lawnmower Baby Monitor Lapis Necklace Sturdy Shelves Lawnmower Baby Monitor Lapis Necklace Sturdy Shelves XML: XML Schema:

CS561 - Spring Keys in XML Schema In general, two flavors: Note: all Xpath expressions “start” at the element currently being defined The fields must identify a single node

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

CS561 - Spring Keys in XML Schema Examples Recall: must have A single forename, Single surname

CS561 - Spring Foreign Keys in XML Schema Example

XPATH

CS561 - Spring XPath Goal = permit to access some nodes from document XPath main construct : axis navigation 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 also offers shortcuts –no axis means child –//  / descendant-or-self::node()/

CS561 - Spring XPath- Child axis navigation author is shorthand for child::author. Examples: –aaa -- all the child 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) –. -- the context node –/ -- the root node aaa bbb cccaaa bbb ccc context node

CS561 - Spring XPath- child axis navigation –/ doc -- all the doc children of the root –./ aaa -- all the aaa children of the context node (equivalent to aaa ) –text() -- all the text children of the context node –node() -- all the 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

CS561 - Spring Predicates –[2] -- the second child node of the context node –chapter[5] -- the fifth chapter child of the 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” (the string-value is the concatenation of all the 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 ”

CS561 - Spring Axis navigation So far, nearly all our expressions have moved us down by moving to child nodes. Exceptions were –. -- stay where you are –/ go to the root –// all descendants of the root –.// all descendants of the context node 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 ( self, parent ) describe single nodes, others describe sequences of nodes.

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

CS561 - Spring XPath abbreviated syntax ///descendant-or-self::node().self::node().//descendant-or-self::node..parent::node() /(document root)

CS561 - Spring XPath Reasonably widely adopted -- in XML- Schema and query languages. Neither more expressive nor less expressive than regular path expressions

Query Languages - XQuery

CS561 - Spring 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:

CS561 - Spring XQuery Based on Quilt (which is based on XML-QL) XML Query data model (ordered)

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

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

CS561 - Spring XQuery 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

CS561 - Spring XQuery Result: Jones abc def Smith ghi

CS561 - Spring XQuery FOR $x in expr -- binds $x to each element in the list expr LET $x = expr -- binds $x to the entire list expr –Useful for common subexpressions and for aggregations

CS561 - Spring XQuery count = a (aggregate) function that returns the number of elms 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

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

CS561 - Spring XQuery Summary: FOR-LET-WHERE-RETURN = FLWR FOR/LET Clauses WHERE Clause RETURN Clause List of tuples Instance of Xquery data model

CS561 - Spring FOR v.s. LET FOR Binds node variables  iteration LET Binds collection variables  one value

CS561 - Spring 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:...

CS561 - Spring 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:...

CS561 - Spring 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, 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, SORTBY(price DESCENDING) SORTBY(name)

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

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

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

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