1 / 16

XML Data Model

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

alima
Download Presentation

XML Data Model

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. XML Data Model

  2. 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

  3. 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

  4. 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>

  5. XML Documents • Start the document with a declaration, surrounded by <?xml … ?> • Typical <?xml version = “1.0” encoding = “utf-8” ?>

  6. Tags • Tags are normally matched pairs • e.g., <movie> … </movie> • Tags may be nested arbitrarily. • XML tags are case sensitive.

  7. 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>

  8. 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>

  9. 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 . . . ]>

  10. 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)> ]>

  11. 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”>

  12. 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>

  13. $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>

  14. 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>

  15. 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>

  16. $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>

More Related