Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 157B: Database Management Systems II February 20 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.

Similar presentations


Presentation on theme: "CS 157B: Database Management Systems II February 20 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron."— Presentation transcript:

1 CS 157B: Database Management Systems II February 20 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak www.cs.sjsu.edu/~mak

2 Department of Computer Science Spring 2013: February 20 CS 157B: Database Management Systems II © R. Mak 2 Project #2  Due: Friday, March 1.  Experience: Writing XML schemas Validating XML documents Marshalling and unmarshalling XML data XQuery Altova XMLSpy tool  Download the Altova XMLSpy 2013 tool from: http://www.altova.com/download-trial.html# http://www.altova.com/download-trial.html# Evaluation good for 30 days. _

3 Department of Computer Science Spring 2013: February 20 CS 157B: Database Management Systems II © R. Mak 3 XMLSpy XQuery Tutorial  How to edit, debug, and execute XQuery with XMLSpy YouTube video : http://www.youtube.com/watch?v=piVbWtChd6I http://www.youtube.com/watch?v=piVbWtChd6I

4 Department of Computer Science Spring 2013: February 20 CS 157B: Database Management Systems II © R. Mak 4 XQuery 1.0  A query language used to: Select content from an XML data source. Transform the content. Return the content in some format (XML, HTML,...). Uses XPath expressions.  Analogous to SQL for relational database tables.  Compact and easy to learn. Does not use the XML syntax.

5 Department of Computer Science Spring 2013: February 20 CS 157B: Database Management Systems II © R. Mak 5 XQuery Design XML Schemas Using UML Ayesha Malik Design service-oriented architecture frameworks with J2EE technology Naveen Balani Advance DAO Programming Sean Sullivan XML document adapted from the book Pro XML Development with Java Technology, by Ajay Vohra and Deepak Vohra, Apress, 2006 catalog.xql

6 Department of Computer Science Spring 2013: February 20 CS 157B: Database Management Systems II © R. Mak 6 XQuery Scripts  XQuery queries are kept in script files. Convention: Use the.xql suffix in the file name.  Example: Return the entire node tree from catalog.xml : The first line is mandatory (and won’t be shown in subsequent examples). _ xquery version "1.0"; doc("catalog.xml") xquery version "1.0";

7 Department of Computer Science Spring 2013: February 20 CS 157B: Database Management Systems II © R. Mak 7 XQuery Scripts  More examples: Return only the articles. Return articles whose titles contain the word “Design”. Return the articles that were written in the year 2003. doc("catalog.xml") //article doc("catalog.xml") //article/title[contains(., "Design")] doc("catalog.xml") //article[contains(@date, "2003")]

8 Department of Computer Science Spring 2013: February 20 CS 157B: Database Management Systems II © R. Mak 8 FLWOR Expressions  Pronounced “flower”.  XQuery expressions can contain For (loops) Let (local variables) Where Order Return  Example: for $art in doc("catalog.xml") //article where contains($art/@date, "2003") return $art example-1.xql XMLSpy demo

9 Department of Computer Science Spring 2013: February 20 CS 157B: Database Management Systems II © R. Mak 9 FLWOR Expressions  Example: for $art in doc("catalog.xml") //article let $d := $art/@date where contains($d, "2003") order by $art/title return ($art/title, $art/author) example-2.xql Note the := Note the parentheses and comma! A return clause can return only one expression. XMLSpy demo

10 Department of Computer Science Spring 2013: February 20 CS 157B: Database Management Systems II © R. Mak 10 FLWOR Expressions { for $art in doc("catalog.xml") //article let $d := $art/@date where contains($d, "2003") order by $art/title return {$art/title, $art/author} } example-3.xql Note the curly braces and the comma! XMLSpy demo

11 Department of Computer Science Spring 2013: February 20 CS 157B: Database Management Systems II © R. Mak 11 FLWOR Expressions Ayesha Malik amalik@sjsu.edu Sean Sullivan ssullivan@mac.com Mary Jane mjane@google.com Naveen Balani nbalani@yahoo.com directory.xml

12 Department of Computer Science Spring 2013: February 20 CS 157B: Database Management Systems II © R. Mak 12 FLWOR Expressions <xs:element ref="person" minOccurs="0" maxOccurs="unbounded"/> directory.xsd

13 Department of Computer Science Spring 2013: February 20 CS 157B: Database Management Systems II © R. Mak 13 FLWOR Expressions { for $person in doc("directory.xml") //person return ( { { if ($person/@gender = "male") then "Mr." else "Ms." }, $person/name } ) } Note the comma! example-4.xql  Example: XMLSpy demo

14 Department of Computer Science Spring 2013: February 20 CS 157B: Database Management Systems II © R. Mak 14 FLWOR Expressions for $art in doc("catalog.xml") //article for $per in doc("directory.xml") //person let $addr := $per/email where $art/author = $per/name order by $art/title return ($art/title, $art/author, $addr) A join of two XML documents! example-5.xql  Example: XMLSpy demo

15 Department of Computer Science Spring 2013: February 20 CS 157B: Database Management Systems II © R. Mak 15 FLWOR Expressions { for $art in doc("catalog.xml") //article for $per in doc("directory.xml") //person let $addr := $per/email where $art/author = $per/name order by $art/title return { ($art/title, $art/author, $addr ) } } example-6.xql  Example: XMLSpy demo

16 Department of Computer Science Spring 2013: February 20 CS 157B: Database Management Systems II © R. Mak 16 User-Defined Functions in XQuery  Example: declare function local:splitter($name) { let $first := substring-before($name, ' ') let $last := substring-after ($name, ' ') return ( {$first}, {$last} ) }; NOTE! Standard XQuery functions Use the local namespace to prevent name clashes with standard functions.

17 Department of Computer Science Spring 2013: February 20 CS 157B: Database Management Systems II © R. Mak 17 User-Defined Functions in XQuery { for $art in doc("catalog.xml") //article for $per in doc("G:\Altova\Projects\directory.xml") //person let $addr := $per/email where $art/author = $per/name order by $art/title return { ($art/title, local:splitter($art/author), $addr ) } } example-7.xql XMLSpy demo

18 Department of Computer Science Spring 2013: February 20 CS 157B: Database Management Systems II © R. Mak 18 XML and Relational Databases  Modern RDBM systems provide XML support. For MySQL, see https://dev.mysql.com/doc/refman/5.5/en/load-xml.html https://dev.mysql.com/doc/refman/5.5/en/load-xml.html G:\Altova\Projects>mysql -u root -psesame --xml school mysql> select * from class; 908 7008 Data structures 114 926 7003 Java programming 101

19 Department of Computer Science Spring 2013: February 20 CS 157B: Database Management Systems II © R. Mak 19 XML and Relational Databases  Native XML support allows you to load and fetch XML data directly into and out of a relational database.  XQuery can directly query relational database tables. If database school contains table Teacher : However, the XQuery standard (as of version 1.0) doesn’t specify how to make a connection to a database.  There’s more to XQuery than can be presented in just one lecture! But this should give you a strong start for Project #2. _ let $teachers := collection("school.Teacher")


Download ppt "CS 157B: Database Management Systems II February 20 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron."

Similar presentations


Ads by Google