Processing and Querying XML

Slides:



Advertisements
Similar presentations
Web Data Management XQuery 1. In this lecture Summary of XQuery FLWOR expressions – For, Let, Where, Order by, Return FOR and LET expressions Collections.
Advertisements

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 !
XML, XML Schema, Xpath and XQuery Slides collated from various sources, many from Dan Suciu at Univ. of Washington.
XQUERY. What is XQuery? XQuery is the language for querying XML data The best way to explain XQuery is to say that XQuery is to XML what SQL is to database.
Database Management Systems, R. Ramakrishnan1 Introduction to Semistructured Data and XML Chapter 27, Part D Based on slides by Dan Suciu University of.
Introduction to XML, XPath, & XQuery CS186, Fall 2005 R &G - Chapters 7-27 Bill Gates, The Revolution, and a Network of Trees ( based on a true story)
1 Part 3: Query Languages Managing XML and Semistructured Data.
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?
Introduction to XML Algebra
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 12: XQuery in SQL Server Monday, October 23, 2006.
1 Lecture 9: XQuery. 2 XQuery Motivation XPath expressivity insufficient –no join queries (as in SQL) –no changes to the XML structure possible –no quantifiers.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 357 Database Systems I Query Languages for XML.
XQuery language Presented by: Tayeb sbihi supervised by: Dr. H. Haddouti.
Query Languages - XQuery Slides partially from Dan Suciu.
CSC056-Z1 – Database Management Systems – Vinnie Costa – Hofstra University1 Database Management Systems Session 10 Instructor: Vinnie Costa
1 Introduction To XML Algebra Wan Liu Bintou Kane Advanced Database Instructor: Elka 2/11/
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.
1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008.
1 Lecture 11: Xpath/XQuery Friday, October 20, 2006.
SDPL 2001Notes 8.2: XQuery1 8.2 W3C XML Query Language –Thanks for Helena Ahonen-Myka (University of Helsinki) for borrowing her slide originals for this.
XQuery – The W3C XML Query Language Jonathan Robie, Software AG Don Chamberlin, IBM Research Daniela Florescu, INRIA.
XML, XML Schema, Xpath and Xquery Slides collated from various sources, many from Dan Suciu at Univ. of Washington.
1 Introduction to XML Algebra Based on talk prepared for CS561 by Wan Liu and Bintou Kane.
XML, XML Schema, XPath and XQuery Query Languages CS561 Slides collated from several sources, including D. Suciu at Univ. of Washington.
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.
Processing of structured documents Spring 2003, Part 8 Helena Ahonen-Myka.
Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:
Lecture 7 of Advanced Databases XML Querying & Transformation Instructor: Mr.Ahmed Al Astal.
Introduction to XQuery Resources: Official URL: Short intros:
1 XQuery Slides From Dr. Suciu. 2 FLWR (“Flower”) Expressions FOR... LET... WHERE... RETURN... FOR... LET... WHERE... RETURN...
XML by Dan Suciu 1 Introduction to Semistructured Data and XML Based on slides by Dan Suciu University of Washington.
Lecture 6 of Advanced Databases XML Querying & Transformation Instructor: Mr.Eyad Almassri.
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.
Computing & Information Sciences Kansas State University Thursday, 15 Mar 2007CIS 560: Database System Concepts Lecture 24 of 42 Thursday, 15 March 2007.
Database Systems Part VII: XML Querying Software School of Hunan University
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.
CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.
1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries.
XML May 6th, Instructor AnHai Doan Brief bio –high school in Vietnam & undergrad in Hungary –M.S. at Wisconsin –Ph.D. at Washington under Alon &
1 Lecture 13: XQuery XML Publishing, XML Storage Monday, October 28, 2002.
Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.
CSE 6331 © Leonidas Fegaras XQuery 1 XQuery Leonidas Fegaras.
1 Lecture 5: Relational Algebra and XML Monday, April 26th, 2004.
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.
USING ANDROID WITH THE DOM. Slide 2 Lecture Summary DOM concepts SAX vs DOM parsers Parsing HTTP results The Android DOM implementation.
Querying and Transforming XML Data
Lecture 11: Xpath/XQuery
End of XQuery DBMS Internals
Querying XML and Semistructured Data
{ XML Technologies } BY: DR. M’HAMED MATAOUI
XML and Databases.
XML: Schemas, Queries Wednesday, 4/17/2002
Lecture 12: XML, XPath, XQuery
Introduction to Database Systems CSE 444 Lecture 12 More Xquery and Xquery in SQL Server April 25, 2008.
Xquery Slides From Dr. Suciu.
Lecture 12: XQuery in SQL Server
Introduction to Database Systems CSE 444 Lecture 12 Xquery in SQL Server October 22, 2007.
CS561-Spring 2012 WPI, Mohamed eltabakh
Lecture 13: XQuery XML Publishing, XML Storage
Siddhesh Bhobe Persistent eBusiness Solutions
XML, XML Schema, XPath and XQuery Query Languages
Presentation transcript:

Processing and Querying XML CS561-Spring 2012 WPI, Mohamed eltabakh

roadmap Models for Parsing XML Documents XPath Language XQuery Language XML inside DBMSs

Processing XML

DOM Structure Model

Example of DOM Tree

SAX: Simple Api for XML

DOM Summary Object-Oriented approach to traverse the XML node tree Automatic processing of XML docs Operations for manipulating XML tree Manipulation & Updating of XML on client & server Database interoperability mechanism Memory-intensive

SAX Summary Pros: The whole file doesn’t need to be loaded into memory XML stream processing Simple and fast Allows you to ignore less interesting data Cons: limited expressive power (query/update) when working on streams => application needs to build (some) parse-tree when necessary

roadmap Models for Parsing XML Documents XPath Language XQuery Language XML inside DBMSs

Xpath Goal = Permit access some nodes from document XPath main construct : Axis navigation Navigation step : axis + node-test + predicates Examples descendant::book  //book child::author  ./author or author attribute::booktitle =“XML”  @booktitle = “XML”

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 * - all children of content node //text() -- all the text nodes in the document CS561 - Spring 2007.

Xpath Example //aaa bbb ./aaa or aaa //aaa/ccc Nodes 1, 3, 5 None

Predicates [2] or ./[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”

More Examples Find all books written by author ‘Bob’ //book[author/first-name= ‘Bob’] Given a content bookstore node, find all books with price > $50 book[@price > 50] Find all books where the category equals the bookstore specialty? //book[../bookstore/@specialty= @category] <Doc> <bookstore specialty=“science”> <book category=“science” price = 10> <author> <first-name>Bob</first-name> </author> </book> … </bookstore> …. <bookstore specialty=“sport”> <book> </Doc>

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 ancestor preceding-sibling following-sibling self child attribute preceding following namespace descendant

roadmap Models for Parsing XML Documents XPath Language XQuery Language XML inside DBMSs

FLWR (“Flower”) Expressions XQuery is more powerful and expressive that XPath FOR ... LET... WHERE... RETURN... CS561 - Spring 2007.

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

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 Result: <title> abc </title> <title> def </title> <title> ghi </title> CS561 - Spring 2007.

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

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

XQuery Result: <result> <author>Jones</author> <title> abc </title> <title> def </title> </result> <author> Smith </author> <title> ghi </title> How to eliminate duplicates? CS561 - Spring 2007.

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 <result> $a, FOR $t IN /bib/book[author=$a]/title RETURN $t </result> distinct = a function that eliminates duplicates CS561 - Spring 2007.

Example XQuery Result Result: <result> <author>Jones</author> <title> abc </title> <title> def </title> </result> <author> Smith </author> <title> ghi </title> CS561 - Spring 2007.

XQuery FOR $x in expr LET $x = 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 CS561 - Spring 2007.

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

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

FOR vs. LET FOR Binds node variables  iteration LET Binds collection variables  one value CS561 - Spring 2007.

FOR vs. LET Returns: <result> <book>...</book></result> ... FOR $x IN document("bib.xml")/bib/book RETURN <result> $x </result> Returns: <result> <book>...</book> <book>...</book> ... </result> LET $x := document("bib.xml")/bib/book RETURN <result> $x </result> CS561 - Spring 2007.

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...) Returns: <result> <author>...</author> <author>...</author> ... </result> RETURN <result> $b/author </result> CS561 - Spring 2007.

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

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

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

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

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

Example: XML Source Documents Invoice.xml <Invoice_Document> <invoice> <account_number>2 </account_number> <carrier>AT&T</carrier> <total>$0.25</total> </invoice> <account_number>1 </account_number> <carrier>Sprint</carrier> <total>$1.20</total> <total>$0.75</total> <auditor> maria </auditor> </Invoice_Document> Customer.xml <Customer_Document> <customer> <account>1 </account> <name>Tom </name> </customer > <account>2 </account> <name>George </name> </Customer _Document>

Xquery Example List account number, customer name, and invoice total for all invoices that have carrier = “Sprint”. FOR $i in (invoices.xml)//invoice, $c in (customers.xml)//customer WHERE $i/carrier = “Sprint” and $i/account_number= $c/account RETURN <Sprint_invoices> $i/account_number, $c/name, $i/total </Sprint_invoices>

Example: Xquery output <Sprint_Invoice> <account_number>1 </account_number> <name>Tom </name> <total>$1.20</total> </Sprint_Invoice >

Algebra Tree Execution Account_number name total Expose (*.account_number , *.name, *.total ) invoice(2) customer(1) Join (*.invoice.account_number=*.customer.account) invoice (2) Select (carrier= “Sprint” ) Invoice (1) invoice (2) invoice (3) customer(1) customer (2) Follow (*.invoice) Follow (*.customer) Source (Invoices.xml) Source (cutomers.xml)

roadmap Models for Parsing XML Documents XPath Language XQuery Language XML inside DBMSs

XML vs. Tables Before providing a native support for XML, documents is translated back and forth to relational tables

Generating XML from relational data Step 1 : Set up the database connection // Create an instance of the JDBC driver so that it has // a chance to register itself Class.forName(sun.jdbc.odbc.JdbcOdbcDriver).newInstance(); // Create a new database connection. Connection con = DriverManager.getConnection(jdbc:odbc:myData, “”, “”); // Create a statement object that we can execute queries with Statement stmt = con.createStatement();

Generating XML (cont’d) Step 2 : Execute the JDBC query String query = “Select Name, Age from Customers”; ResultSet rs = stmt.executeQuery(query);

Generating XML (cont’d) Step 3 : Create the XML! StringBuffer xml = “<?xml version=‘1.0’?><myDatabase><customers>”; while (rs.next()) { xml.append(“<custRec><custName>”); xml.append(rs.getString(“Name”)); xml.append(“</custName><custAge>”); xml.append(rs.getInt(“Age”)); xml.append(“</custAge></custRec>”); } xml.append(“</customers></myDatabase>”);

Storing XML in Relational tables Step 1 : Set up the parser StringReader stringReader = new StringRead(xmlString); InputSource inputSource = new InputSource(stringReader); DOMParser domParser = new DOMParser(); domParser.parse(inputSource); Document document = domParser.getDocument();

Storing XML (Cont’d) Step 2 : Read values from parsed XML document NodeList nameList = doc.getElementsByTagName(“custName”); NodeList ageList = doc.getElementsByTagName(“custAge”);

Storing XML (Cont’d) Step 3 : Set up database connection Class.forName(sun.jdbc.odbc.JdbcOdbcDriver).newInstance(); Connection con = DriverManager.getConnection (jdbc:odbc:myDataBase, “”, “”); Statement stmt = con.createStatement();

Storing XML (Cont’d) Step 4 : Insert data using appropriate JDBC update query String sql = “INSERT INTO Customers (Name, Age) VALUES (?,?)”; PreparedStatement pstmt = conn.prepareStatement(sql); int size = nameList.getLength(); for (int i = 0; i < size; i++) { pstmt. setString(1, nameList.item(i).getFirstChild().getNodeValue()); pstmt.setInt(2, ageList.item(i).getFirstChild().getNodeValue()); pstmt.executeUpdate(sql); }

Use case: Oracle 10g New Data type “XMLType” to store XML documents Before XMLType Either parse the documents and store it in relational tables Or, store the documents in CLOB, BLOB Creating a Table with an XMLType Column CREATE TABLE mytable1 (key_column VARCHAR2(10) PRIMARY KEY, xml_column XMLType); Creating a Table of XMLType CREATE TABLE mytable2 OF XMLType; Creating a Table of XMLType with schema CREATE TABLE mytable3 OF XMLType XMLSCHEMA “....”;

Inserting XML into Oracle Many ways… Inserting XML Content into an XMLType Table using SQL CREATE DIRECTORY xmldir AS path_to_folder_containing_XML_file'; INSERT INTO mytable2 VALUES (XMLType(bfilename('XMLDIR', 'purchaseOrder.xml'), nls_charset_id('AL32UTF8'))); Inserting XML Content into an XMLType Table using Java/DOM public void doInsert(Connection conn, Document doc) throws Exception { String SQLTEXT = "INSERT INTO purchaseorder VALUES (?)"; XMLType xml = null; xml = XMLType.createXML(conn,doc); OraclePreparedStatement sqlStatement = null; sqlStatement = (OraclePreparedStatement) conn.prepareStatement(SQLTEXT); sqlStatement.setObject(1,xml); sqlStatement.execute(); }

Oracle XPATH operations/Functions SELECT warehouse_id, warehouse_name, ExtractValue(warehouse_spec, '/Warehouse/Building/Owner') "Prop.Owner" FROM warehouses WHERE ExistsNode(warehouse_spec, '/Warehouse/Building/Owner') = 1; Update warehouse SET warehouse_spec = AppendChildXML(warehouse_spec, 'Warehouse/Building', XMLType('<Owner>Grandco</Owner>’) Where ExtractValue(warehouse_spec, ‘//Warehouse/Building') = 'Rented'; UPDATE warehouses SET warehouse_spec = InsertChildXML(warehouse_spec, '/Warehouse/Building', 'Owner', XMLType('<Owner>LesserCo</Owner>')) WHERE warehouse_id = 3;

More Xpath examples

Oracle XQuery

What we covered Models for Parsing XML Documents XPath Language XQuery Language XML inside DBMSs