XML and NonRelational Schemas Zachary G Ives University

  • Slides: 43
Download presentation
XML and Non-Relational Schemas Zachary G. Ives University of Pennsylvania CIS 550 – Database

XML and Non-Relational Schemas Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems Some slide content courtesy of Susan Davidson & Raghu Ramakrishnan

Announcements § Homework 3 due Wednesday 10/21 at 4: 30 § Midterm: 10/26 2

Announcements § Homework 3 due Wednesday 10/21 at 4: 30 § Midterm: 10/26 2

Recall: Two Important Normal Forms Boyce-Codd Normal Form (BCNF). For every relation scheme R

Recall: Two Important Normal Forms Boyce-Codd Normal Form (BCNF). For every relation scheme R and for every X A that holds over R, either A X (it is trivial) , or or X is a superkey for R Third Normal Form (3 NF). For every relation scheme R and for every X A that holds over R, either A X (it is trivial), or X is a superkey for R, or A is a member of some key for R 3

BCNF Decomposition Algorithm (from Korth et al. ; our book gives a recursive version)

BCNF Decomposition Algorithm (from Korth et al. ; our book gives a recursive version) result : = {R} compute F+ while there is a relation schema Ri in result that isn’t in BCNF { i. e. , A doesn’t form a key let A B be a nontrivial FD on Ri s. t. A Ri is not in F+ and A and B are disjoint } result: = (result – Ri) {(Ri - B), (A, B)} 4

An Example Given the schema: Stuff(sid, name, serno, classroom, cid, fid, prof) And FDs:

An Example Given the schema: Stuff(sid, name, serno, classroom, cid, fid, prof) And FDs: sid name fid prof serno classroom, cid, fid § Find the Boyce-Codd Normal Form for this schema § What if instead: sid name fid prof classroom, cid serno cid 5

3 NF Decomposition Algorithm Let F be a minimal cover i: =0 for each

3 NF Decomposition Algorithm Let F be a minimal cover i: =0 for each FD A B in F { if none of the schemas Rj, 1 j i, contains AB { increment i Ri : = (A, B) } } if no schema Rj, 1 j i contains a candidate key for R { increment i Ri : = any candidate key for R } return (R 1, …, Ri) Build dep. preserving decomp. Ensure lossless decomp. 6

An Example Given the schema: Stuff(sid, name, serno, classroom, cid, fid, prof) And FDs:

An Example Given the schema: Stuff(sid, name, serno, classroom, cid, fid, prof) And FDs: sid name fid prof serno classroom, cid, fid § Find the Third Normal Form for this schema § What if instead: sid name fid prof classroom, cid serno cid 7

Normalization Is Good… Or Is It? § In some cases, we might not mind

Normalization Is Good… Or Is It? § In some cases, we might not mind redundancy, if the data isn’t directly updated: § Reports (people like to see breakdowns by semester, department, course, etc. ) § Warehouses (archived copies of data for doing complex analysis) § Data sharing (sometimes we may export data into objectoriented or hierarchical formats) 8

XML: A Semi-Structured Data Model 9

XML: A Semi-Structured Data Model 9

Why XML? XML is the confluence of several factors: § § § The Web

Why XML? XML is the confluence of several factors: § § § The Web needed a more declarative format for data Documents needed a mechanism for extended tags Database people needed a more flexible interchange format “Lingua franca” of data It’s parsable even if we don’t know what it means! Original expectation: § The whole web would go to XML instead of HTML Today’s reality: § Not so… But XML is used all over “under the covers” 10

Why DB People Like XML Can get data from all sorts of sources §

Why DB People Like XML Can get data from all sorts of sources § Allows us to touch data we don’t own! § This was actually a huge change in the DB community Interesting relationships with DB techniques § Useful to do relational-style operations § Leverages ideas from object-oriented, semistructured data Blends schema and data into one format § Unlike relational model, where we need schema first § … But too little schema can be a drawback, too! 11

XML Anatomy Processing Instr. <? xml version="1. 0" encoding="ISO-8859 -1" ? > <dblp> Open-tag

XML Anatomy Processing Instr. <? xml version="1. 0" encoding="ISO-8859 -1" ? > <dblp> Open-tag <mastersthesis mdate="2002 -01 -03" key="ms/Brown 92"> <author>Kurt P. Brown</author> <title>PRPL: A Database Workload Specification Language</title> <year>1992</year> Element <school>Univ. of Wisconsin-Madison</school> </mastersthesis> <article mdate="2002 -01 -03" key="tr/dec/SRC 1997 -018"> <editor>Paul R. Mc. Jones</editor> Attribute <title>The 1995 SQL Reunion</title> <journal>Digital System Research Center Report</journal> <volume>SRC 1997 -018</volume> Close-tag <year>1997</year> <ee>db/labs/dec/SRC 1997 -018. html</ee> <ee>http: //www. mcjones. org/System_R/SQL_Reunion_95/</ee> 12 </article>

Well-Formed XML A legal XML document – fully parsable by an XML parser §

Well-Formed XML A legal XML document – fully parsable by an XML parser § All open-tags have matching close-tags (unlike so many HTML documents!), or a special: <tag/> shortcut for empty tags (equivalent to <tag></tag> § Attributes (which are unordered, in contrast to elements) only appear once in an element § There’s a single root element § XML is case-sensitive 13

XML as a Data Model XML “information set” includes 7 types of nodes: §

XML as a Data Model XML “information set” includes 7 types of nodes: § § § § Document (root) Element Attribute Processing instruction Text (content) Namespace Comment XML data model includes this, plus typing info, plus order info and a few other things 14

XML Data Model Visualized (and simplified!) Root ? xml 2002… element article mdate author

XML Data Model Visualized (and simplified!) Root ? xml 2002… element article mdate author title year school 1992 key editor title journal volume year ee ee 2002… tr/dec/… PRPL… Kurt P…. p-i dblp key ms/Brown 92 attribute text mastersthesis mdate root Digital… Univ…. 1997 The… Paul R. db/labs/dec SRC… http: //www. 15

What Does XML Do? § Serves as a document format (super-HTML) § Allows custom

What Does XML Do? § Serves as a document format (super-HTML) § Allows custom tags (e. g. , used by MS Word, openoffice) § Supplement it with stylesheets (XSL) to define formatting § Data exchange format (must agree on terminology) § Marshalling and unmarshalling data in SOAP and Web Services 16

XML as a Super-HTML (MS Word) <h 1 class="Section 1"> <a name="_top“ />CIS 550:

XML as a Super-HTML (MS Word) <h 1 class="Section 1"> <a name="_top“ />CIS 550: Database and Information Systems</h 1> <h 2 class="Section 1">Fall 2004</h 2> <p class="Mso. Normal"> <place>311 Towne</place>, Tuesday/Thursday <time Hour="13" Minute="30">1: 30 PM – 3: 00 PM</time> </p> 17

XML Easily Encodes Relations Student-course-grade sid serno exp-grade 1 570103 B 23 550103 A

XML Easily Encodes Relations Student-course-grade sid serno exp-grade 1 570103 B 23 550103 A <student-course-grade> <tuple><sid>1</sid><serno>570103</serno><expgrade>B</exp-grade></tuple> <tuple><sid>23</sid><serno>550103</serno><expgrade>A</exp-grade></tuple> </student-course-grade> 18

But XML is More Flexible… “Non-First-Normal-Form” (NF 2) <parents> <parent name=“Jean” > <son>John</son> <daughter>Joan</daughter>

But XML is More Flexible… “Non-First-Normal-Form” (NF 2) <parents> <parent name=“Jean” > <son>John</son> <daughter>Joan</daughter> <daughter>Jill</daughter> </parent> <parent name=“Feng”> <daughter>Felicity</daughter> </parent> Coincides with “semi-structured data”, … invented by DB people at Penn and Stanford 19

XML and Code § Web Services (. NET, recent Java web service toolkits) are

XML and Code § Web Services (. NET, recent Java web service toolkits) are using XML to pass parameters and make function calls § Why? Easy to be forwards-compatible Easy to read over and validate (? ) Generally firewall-compatible § Drawbacks? XML is a verbose and inefficient encoding! § XML is used to represent: § § SOAP: the “envelope” that data is marshalled into XML Schema: gives some typing info about structures being passed WSDL: the IDL (interface def language) UDDI: provides an interface for querying about web services 20

Integrating XML: What If We Have Multiple Sources with the Same Tags? § Namespaces

Integrating XML: What If We Have Multiple Sources with the Same Tags? § Namespaces allow us to specify a context for different tags § Two parts: § Binding of namespace to URI § Qualified names <root xmlns=“http: //www. first. com/aspace” xmlns: otherns=“…”> <tag xmlns: myns=“http: //www. fictitious. com/mypath”> <thistag>is in the default namespace (aspace)</thistag> <myns: thistag>is in myns</myns: thistag> <otherns: thistag>is a different tag in otherns</otherns: thistag> </root> 21

XML Isn’t Enough on Its Own It’s too unconstrained for many cases! § How

XML Isn’t Enough on Its Own It’s too unconstrained for many cases! § How will we know when we’re getting garbage? § How will we query? § How will we understand what we got? We also need: Some idea of the structure Our focus next Presentation, in some cases – XSL(T) We’ll talk about this soon Some way of interpreting the tags…? We’ll talk about this later in the semester 22

Structural Constraints: Document Type Definitions (DTDs) The DTD is an EBNF grammar defining XML

Structural Constraints: Document Type Definitions (DTDs) The DTD is an EBNF grammar defining XML structure § XML document specifies an associated DTD, plus the root element § DTD specifies children of the root (and so on) DTD defines special significance for attributes: § IDs – special attributes that are analogous to keys for elements § IDREFs – references to IDs § IDREFS – a nasty hack that represents a list of IDREFs 23

An Example DTD: <!ELEMENT dblp((mastersthesis | article)*)> <!ELEMENT mastersthesis(author, title, year, school, committeemember*)> <!ATTLIST

An Example DTD: <!ELEMENT dblp((mastersthesis | article)*)> <!ELEMENT mastersthesis(author, title, year, school, committeemember*)> <!ATTLIST mastersthesis(mdate CDATA #REQUIRED key ID #REQUIRED advisor CDATA #IMPLIED> <!ELEMENT author(#PCDATA)> … Example use of DTD in XML file: <? xml version="1. 0" encoding="ISO-8859 -1" ? > <!DOCTYPE dblp SYSTEM “my. dtd"> <dblp>… 24

Representing Graphs and Links in XML <? xml version="1. 0" encoding="ISO-8859 -1" ? >

Representing Graphs and Links in XML <? xml version="1. 0" encoding="ISO-8859 -1" ? > <!DOCTYPE graph SYSTEM “special. dtd"> <graph> <author id=“author 1”> <name>John Smith</name> </author> <article> <author ref=“author 1” /> <title>Paper 1</title> </article> <author ref=“author 1” /> <title>Paper 2</title> </article> … 25

Graph Data Model Root graph ? xml !DOCTYPE author id name article author title

Graph Data Model Root graph ? xml !DOCTYPE author id name article author title Paper 1 author 1 ref author ref title Paper 2 John Smith author 1 26

Graph Data Model Root graph ? xml !DOCTYPE author id name article author title

Graph Data Model Root graph ? xml !DOCTYPE author id name article author title Paper 1 author 1 ref author ref title Paper 2 John Smith 27

DTDs Aren’t Expressive Enough DTDs capture grammatical structure, but have some drawbacks: § Not

DTDs Aren’t Expressive Enough DTDs capture grammatical structure, but have some drawbacks: § Not themselves in XML – inconvenient to build tools for them § Don’t capture database datatypes’ domains § IDs aren’t a good implementation of keys Why not? § No way of defining OO-like inheritance 28

XML Schema Aims to address the shortcomings of DTDs § XML syntax § Can

XML Schema Aims to address the shortcomings of DTDs § XML syntax § Can define keys using XPaths § Type subclassing that’s more complex than in a programming language Programming languages don’t consider order of member variables! Subclassing “by extension” and “by restriction” § … And, of course, domains and built-in datatypes 29

Basics of XML Schema Need to use the XML Schema namespace (generally named xsd)

Basics of XML Schema Need to use the XML Schema namespace (generally named xsd) § simple. Types are a way of restricting domains on scalars § Can define a simple. Type based on integer, with values within a particular range § complex. Types are a way of defining element/attribute structures § Basically equivalent to !ELEMENT, but more powerful § Specify sequence, choice between child elements § Specify min. Occurs and max. Occurs (default 1) § Must associate an element/attribute with a simple. Type, or an element with a complex. Type 30

Simple Schema Example <xsd: schema xmlns: xsd="http: //www. w 3. org/2001/XMLSchema"> <xsd: element name=“mastersthesis"

Simple Schema Example <xsd: schema xmlns: xsd="http: //www. w 3. org/2001/XMLSchema"> <xsd: element name=“mastersthesis" type=“Thesis. Type"/> <xsd: complex. Type name=“Thesis. Type"> <xsd: attribute name=“mdate" type="xsd: date"/> <xsd: attribute name=“key" type="xsd: string"/> <xsd: attribute name=“advisor" type="xsd: string"/> <xsd: sequence> <xsd: element name=“author" type=“xsd: string"/> <xsd: element name=“title" type=“xsd: string"/> <xsd: element name=“year" type=“xsd: integer"/> <xsd: element name=“school" type=“xsd: string”/> <xsd: element name=“committeemember" type=“Committee. Type” min. Occurs=“ 0"/> </xsd: sequence> </xsd: complex. Type> </xsd: schema> 31

Designing an XML Schema/DTD Not as formalized as relational data design § We can

Designing an XML Schema/DTD Not as formalized as relational data design § We can still use ER diagrams to break into entity, relationship sets § ER diagrams have extensions for “aggregation” – treating smaller diagrams as entities – and for composite attributes § Note that often we already have our data in relations and need to design the XML schema to export them! Generally orient the XML tree around the “central” objects Big decision: element vs. attribute § Element if it has its own properties, or if you *might* have more than one of them § Attribute if it is a single property – or perhaps not! 32

Recap: XML as a Data Model XML is a non-first-normal-form (NF 2) representation §

Recap: XML as a Data Model XML is a non-first-normal-form (NF 2) representation § Can represent documents, data § Standard data exchange format § Several competing schema formats – esp. , DTD and XML Schema – provide typing information 33

Querying XML How do you query a directed graph? a tree? The standard approach

Querying XML How do you query a directed graph? a tree? The standard approach used by many XML, semistructured-data, and object query languages: § Define some sort of a template describing traversals from the root of the directed graph § In XML, the basis of this template is called an XPath 34

XPaths In its simplest form, an XPath is like a path in a file

XPaths In its simplest form, an XPath is like a path in a file system: /mypath/subpath/*/morepath § The XPath returns a node set representing the XML nodes (and their subtrees) at the end of the path § XPaths can have node tests at the end, returning only particular node types, e. g. , text(), processing-instruction(), comment(), element(), attribute() § XPath is fundamentally an ordered language: it can query in order-aware fashion, and it returns nodes in order 35

Sample XML <? xml version="1. 0" encoding="ISO-8859 -1" ? > <dblp> <mastersthesis mdate="2002 -01

Sample XML <? xml version="1. 0" encoding="ISO-8859 -1" ? > <dblp> <mastersthesis mdate="2002 -01 -03" key="ms/Brown 92"> <author>Kurt P. Brown</author> <title>PRPL: A Database Workload Specification Language</title> <year>1992</year> <school>Univ. of Wisconsin-Madison</school> </mastersthesis> <article mdate="2002 -01 -03" key="tr/dec/SRC 1997 -018"> <editor>Paul R. Mc. Jones</editor> <title>The 1995 SQL Reunion</title> <journal>Digital System Research Center Report</journal> <volume>SRC 1997 -018</volume> <year>1997</year> <ee>db/labs/dec/SRC 1997 -018. html</ee> <ee>http: //www. mcjones. org/System_R/SQL_Reunion_95/</ee> </article> 36

XML Data Model Visualized Root ? xml 2002… element article mdate author title year

XML Data Model Visualized Root ? xml 2002… element article mdate author title year school 1992 key editor title journal volume year ee ee 2002… tr/dec/… PRPL… Kurt P…. p-i dblp key ms/Brown 92 attribute text mastersthesis mdate root Digital… Univ…. 1997 The… Paul R. db/labs/dec SRC… http: //www. 37

Some Example XPath Queries § § /dblp/mastersthesis/title /dblp/*/editor //title/text() 38

Some Example XPath Queries § § /dblp/mastersthesis/title /dblp/*/editor //title/text() 38

Context Nodes and Relative Paths XPath has a notion of a context node: it’s

Context Nodes and Relative Paths XPath has a notion of a context node: it’s analogous to a current directory § “. ” represents this context node § “. . ” represents the parent node § We can express relative paths: subpath/sub-subpath/. . gets us back to the context node Ø By default, the document root is the context node 39

Predicates – Selection Operations A predicate allows us to filter the node set based

Predicates – Selection Operations A predicate allows us to filter the node set based on selection-like conditions over sub-XPaths: /dblp/article[title = “Paper 1”] which is equivalent to: /dblp/article[. /title/text() = “Paper 1”] 40

Axes: More Complex Traversals Thus far, we’ve seen XPath expressions that go down the

Axes: More Complex Traversals Thus far, we’ve seen XPath expressions that go down the tree (and up one step) § But we might want to go up, left, right, etc. § These are expressed with so-called axes: self: : path-step child: : path-step descendant-or-self: : path-step preceding-sibling: : path-step preceding: : path-step parent: : path-step ancestor-or-self: : path-step following-sibling: : path-step following: : path-step § The previous XPaths we saw were in “abbreviated form” 41

Querying Order § We saw in the previous slide that we could query for

Querying Order § We saw in the previous slide that we could query for preceding or following siblings or nodes § We can also query a node for its position according to some index: § fn: : first() , fn: : last() return index of 0 th & last element matching the last step: § fn: : position() gives the relative count of the current node child: : article[fn: : position() = fn: : last()] 42

Users of XPath § XML Schema uses simple XPaths in defining keys and uniqueness

Users of XPath § XML Schema uses simple XPaths in defining keys and uniqueness constraints § XQuery § XSLT § XLink and XPointer, hyperlinks for XML 43