CS 157B: Database Management Systems II February 20 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak
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: Evaluation good for 30 days. _
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 :
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.
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
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";
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 doc("catalog.xml") //article doc("catalog.xml") //article/title[contains(., "Design")] doc("catalog.xml") "2003")]
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 "2003") return $art example-1.xql XMLSpy demo
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 := 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
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 := 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
Department of Computer Science Spring 2013: February 20 CS 157B: Database Management Systems II © R. Mak 11 FLWOR Expressions Ayesha Malik Sean Sullivan Mary Jane Naveen Balani directory.xml
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
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 = "male") then "Mr." else "Ms." }, $person/name } ) } Note the comma! example-4.xql Example: XMLSpy demo
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/ 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
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/ where $art/author = $per/name order by $art/title return { ($art/title, $art/author, $addr ) } } example-6.xql Example: XMLSpy demo
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.
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/ where $art/author = $per/name order by $art/title return { ($art/title, local:splitter($art/author), $addr ) } } example-7.xql XMLSpy demo
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 G:\Altova\Projects>mysql -u root -psesame --xml school mysql> select * from class; Data structures Java programming 101
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")