360 likes | 477 Views
XML: Semistructured Data. Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems October 2, 2003. Some slide content courtesy of Susan Davidson & Raghu Ramakrishnan. Administrivia. HW3 due Tuesday 10/7 Please return HW2s for grade recording (sorry…)
E N D
XML: Semistructured Data Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems October 2, 2003 Some slide content courtesy of Susan Davidson & Raghu Ramakrishnan
Administrivia • HW3 due Tuesday 10/7 • Please return HW2s for grade recording (sorry…) • My office is now 576 GRW (5th floor of the old CIS bldg., by the old stairwell) • Upcoming schedule: • Tues. 10/7: XQuery & XSLT; intro to views; HW4 out (Read Section 3.6 in book) • Thurs. 10/9: Views + datalog, ctd.; review for midterm • Tues. 10/14: Fall Break • Thurs. 10/16: HW4 due; Midterm
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”
Why DB People Like XML • Can get data from all sorts of things • 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!
XML Anatomy Processing Instr. <?xml version="1.0" encoding="ISO-8859-1" ?> <dblp> <mastersthesis mdate="2002-01-03" key="ms/Brown92"> <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/SRC1997-018"> <editor>Paul R. McJones</editor> <title>The 1995 SQL Reunion</title> <journal>Digital System Research Center Report</journal> <volume>SRC1997-018</volume> <year>1997</year> <ee>db/labs/dec/SRC1997-018.html</ee> <ee>http://www.mcjones.org/System_R/SQL_Reunion_95/</ee> </article> Open-tag Element Attribute Close-tag
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
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
XML Data Model Visualized(and simplified!) attribute root p-i element Root text dblp ?xml mastersthesis article mdate mdate key key author title year school 2002… editor title journal volume year ee ee 2002… 1992 1997 The… ms/Brown92 tr/dec/… PRPL… Digital… db/labs/dec Univ…. Paul R. Kurt P…. SRC… http://www.
What Does XML Do? • Serves as a document format that’s better than HTML • Allows custom tags (e.g., used by MS Word, openoffice) • Supplement it with stylesheets (XSL) to define formatting • Provides an exchange format for data (still need to agree on terminology) • Basis for RDF format for describing libraries and the Semantic Web (more on this later) • Format for marshalling and unmarshalling data in SOAP and Web Services
XML as a Super-HTML(MS Word) <h1class="Section1"><aname="_top“ />CIS 550: Database and Information Systems</h1> <h2class="Section1">Fall 2003</h2> <pclass="MsoNormal"> <place>311 Towne</place>, Tuesday/Thursday <timeHour="13" Minute="30">1:30PM – 3:00PM</time> </p>
XML Easily Encodes Relations Student-course-grade <student-course-grade> <tuple><sid>1</sid><serno>570103</serno><exp-grade>B</exp-grade></tuple> <tuple><sid>23</sid><serno>550103</serno><exp-grade>A</exp-grade></tuple> </student-course-grade>
But XML is More Flexible…“Non-First-Normal-Form” (NF2) <parents> <parentname=“Jean” > <son>John</son> <daughter>Joan</daughter> <daughter>Jill</daughter> </parent> <parentname=“Feng”> <daughter>Felicity</daughter> </parent> …
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
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 <tagxmlns:myns=“http://www.fictitious.com/mypath”> <thistag>is in namespace myns</thistag> <myns:thistag>is the same</myns:thistag><otherns:thistag>is a different tag</otherns:thistag> </tag>
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
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
An Example DTD 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>…
Representing Graphs in XML <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE graph SYSTEM “special.dtd"> <graph> <author id=“author1”> <name>John Smith</name> </author> <article> <author ref=“author1” /> <title>Paper1</title> </article> <article> <author ref=“author1” /> <title>Paper2</title> </article> …
Graph Data Model Root graph ?xml !DOCTYPE article article author id title title author author name Paper1 author1 ref Paper2 ref John Smith author1 author1
Graph Data Model Root graph ?xml !DOCTYPE article article author id title title author author name Paper1 author1 ref Paper2 ref John Smith
DTDs Aren’t Enough for Some People 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 • No way of expressing FDs
XML Schema Aims to address the shortcomings of DTDs … But an “everything including the kitchen sink” spec Features: • XML syntax • Better way of defining 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
Simple Schema Example <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name=“mastersthesis" type=“ThesisType"/> <xsd:complexType name=“ThesisType"> <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=“CommitteeType” minOccurs=“0"/> </xsd:sequence> </xsd:complexType>
Whither XML Schema? • Adopted by a few tools, but… • The jury is still out on this spec • Considered too big, bulky, inconsistent • Still no way of expressing FDs, and the key mechanism isn’t that elegant • Other people have defined more elegant schema languages, FD languages, and key languages (much of the work was here at Penn) • But it’s also at the core of the data model of the XQuery language and the other W3C specs…
Designing an XML Schema/DTD • XML data design is generally 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! • We generally orient the XML tree around the “central” objects in a particular application • A 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!
XML as a Data Model XML is a non-first-normal-form representation • Can represent documents, data • Standard data exchange format • Several competing schema formats – esp., DTD and XML Schema – provide typing information Now: the core ideas in querying XML
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
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
Sample XML <?xml version="1.0" encoding="ISO-8859-1" ?> <dblp> <mastersthesis mdate="2002-01-03" key="ms/Brown92"> <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/SRC1997-018"> <editor>Paul R. McJones</editor> <title>The 1995 SQL Reunion</title> <journal>Digital System Research Center Report</journal> <volume>SRC1997-018</volume> <year>1997</year> <ee>db/labs/dec/SRC1997-018.html</ee> <ee>http://www.mcjones.org/System_R/SQL_Reunion_95/</ee> </article>
XML Data Model Visualized attribute root p-i element Root text dblp ?xml mastersthesis article mdate mdate key key author title year school 2002… editor title journal volume year ee ee 2002… 1992 1997 The… ms/Brown92 tr/dec/… PRPL… Digital… db/labs/dec Univ…. Paul R. Kurt P…. SRC… http://www.
Some Example XPath Queries • /dblp/mastersthesis/title • /dblp/*/editor • //title • //title/text()
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
Predicates – Selection Operations A predicate allows us to filter the node set based on selection-like conditions over sub-XPaths: /dblp/article[title = “Paper1”] which is equivalent to: /dblp/article[./title/text() = “Paper1”]
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 parent::path-step • descendant::path-step ancestor::path-step • descendant-or-self::path-step ancestor-or-self::path-step • preceding-sibling::path-step following-sibling::path-step • preceding::path-step following::path-step • The previous XPaths we saw were in “abbreviated form”
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 0th & last element matching the last step: • fn::position() gives the relative count of the current node child::article[fn::position() = fn::last()]
Users of XPath • XML Schema uses simple XPaths in defining keys and uniqueness constraints • XQuery • XSLT • XLink and XPointer, hyperlinks for XML • Next time we’ll focus on XQuery, the still-under-development “SQL of XML”… • … And we’ll briefly discuss XSLT, a different attempt to manipulate XML data