260 likes | 715 Views
XML Data Model. Relational Data Model. We have learned relational data model use tables to store data ( structured data ) very simple model Often matches how we think about data supports powerful query language SQL. XML Overview. We now learn another data model: XML
E N D
Relational Data Model • We have learned relational data model • use tables to store data (structured data) • very simple model • Often matches how we think about data • supports powerful query language SQL
XML Overview • We now learn another data model: XML • represent data using trees (semi-structured data) • why? • flexible representation of data • facilitate sharing and exchange ofinformation among systems and databases (e.g., e-commerce, web data) root movie movie blog show title theater price title price 那 $65 SN $50 PP theater parking addr • Nodes = objects • Labels on arcs (like attributenames) • Values at leaf nodes 城 kln $30/h
XML • XML = Extensible Markup Language • Key idea: create tags (or markups) and translate all data into properly tagged XML documents • tag: a description of what the data represents • Example: <movie> <title>The Social Network</title> <price>65</price> <theater> Pacific Place</theater> <blog> http://www.thesocialnetwork-movie.com </blog> </movie>
XML Documents • Start the document with a declaration, surrounded by <?xml … ?> • Typical <?xml version = “1.0” encoding = “utf-8” ?>
Tags • Tags are normally matched pairs • e.g., <movie> … </movie> • Tags may be nested arbitrarily. • XML tags are case sensitive.
A movie object A show subobject Example: An XML Document <?xml version = “1.0” encoding = “utf-8” ?> <movies> <movie> <title>The Social Network</title> <price>65</price> <theater> Pacific Place</theater> <blog>http://www.thesocialnetwork-movie.com/</blog> </movie> <movie> <title>那些年</title> <price> 50 </price> <show> <theater> 又一城</theater> <address> Kowloon Tong </address> <parking> 30/hour</parking> </show> </movie> </movies>
title andprice areattributes Attributes • Opening tag in XML can have attribute = value pairs • Movies using attributes • <movie title = “The Social Network” price = “65”> • <theater> Pacific Place</theater> • <blog>http://www.thesocialnetwork-movie.com</blog> • </movie>
DTD (Document Type Definition) • A schema of xml data model (like table definition) • A dtd says what tags and attributes are required or optional • Definition form: <!DOCTYPE <root tag> [ <!ELEMENT <name>(<components>)> . . . more elements . . . ]>
A movies object can contain zero or more movies A movie has one title and one price, played_at, theater, and blog are optional A title contains text Example: DTD <!DOCTYPE movies [ <!ELEMENT movies (movie*)> <!ELEMENT movie (title, price, show?, theater?, blog?)> <!ELEMENT title (#PCDATA)> <!ELEMENT price (#PCDATA)> <!ELEMENT show (theater, parking, address)> <!ELEMENT theater (#PCDATA)> <!ELEMENT parking (#PCDATA)> <!ELEMENT address (#PCDATA)> <!ELEMENT blog (#PCDATA)> ]>
Characterstring Required = must occur; Implied = optional Attributes • Opening tags in XML can have attributes. • In a DTD <!ATTLIST E . . . > declares an attribute for tagE, along with its datatype • Example <!ATTLIST movie title CDATA #REQUIRED, price CDATA #IMPLIED> Example use: <movietitle=“The Social Network”> <movie title=“The Social Network” price=“65”>
XQuery • A language to query XML data • FLWR expressions • FOR, LET, WHERE, and RETURN clauses • The following query returns the titles of all movies, assuming that the XML document is at www.ours.com/movies.xml. • FOR $a IN doc(www.ours.com/movies.xml)/movies/movie/title RETURN <RESULT>$a</RESULT>
$a <?xml version = “1.0” encoding = “utf-8” ?> <movies> <movie> <title>The Social Network</title> <price>65</price> <theater> Pacific Place</theater> <blog>http://www.thesocialnetwork-movie.com/</blog> </movie> <movie> <title>那些年</title> <price> 50 </price> <show> <theater> 又一城</theater> <address> Kowloon Tong </address> <parking> 30/hour</parking> </show> </movie> </movies> $a XPath expression: returns a sequence of items • FOR $a IN doc(www.ours.com/movies.xml)/movies/movie/title RETURN <RESULT>$a</RESULT>
XQuery (Cont.) • FOR $a IN doc(www.ours.com/movies.xml) • Analogous to the FROM clause in SQL • /movies/movie/title • A path expression • Evaluating a path expression returns a set of tags that match the expression • Variable a is bound to each title tag returned by the expression • RETURN <RESULT> $a </RESULT> • Similar to the SELECT clause in SQL • Result: • <RESULT><title> The Social Network </title></RESULT><RESULT><title> 那些年</title><RESULT>
XQuery (Cont.) • FOR$b IN doc(www.ours.com/movies.xml)/movies/movieWHERE $b/title = “The Social Network”RETURN <RESULT>$b/price</RESULT> • WHERE clause expresses the selection condition • The above query finds the price of the movie entitled “The Social Network” • Result: • <RESULT><price> 65 </price></RESULT>
$b <?xml version = “1.0” encoding = “utf-8” ?> <movies> <movie> <title>The Social Network</title> <price>65</price> <theater> Pacific Place</theater> <blog>http://www.thesocialnetwork-movie.com/</blog> </movie> <movie> <title>那些年</title> <price> 50 </price> <show> <theater> 又一城</theater> <address> Kowloon Tong </address> <parking> 30/hour</parking> </show> </movie> </movies> $b FOR$b IN doc(www.ours.com/movies.xml)/movies/movieWHERE $b/title = “The Social Network”RETURN <RESULT>$b/price</RESULT>