Presentation is loading. Please wait.

Presentation is loading. Please wait.

XML, Schemas, and XPath Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems October 14, 2004 Some slide content courtesy.

Similar presentations


Presentation on theme: "XML, Schemas, and XPath Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems October 14, 2004 Some slide content courtesy."— Presentation transcript:

1 XML, Schemas, and XPath Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems October 14, 2004 Some slide content courtesy of Susan Davidson & Raghu Ramakrishnan

2 2 Announcements  Homework 3 due today  Homework 4 handed out  Midterm: Thursday 10/28

3 3 Why We’re Interested in 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 Used for sharing data 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!

4 4 Basic XML Anatomy Kurt P. Brown PRPL: A Database Workload Specification Language 1992 Univ. of Wisconsin-Madison Paul R. McJones The 1995 SQL Reunion Digital System Research Center Report SRC1997-018 1997 db/labs/dec/SRC1997-018.html http://www.mcjones.org/System_R/SQL_Reunion_95/ Processing Instr. Element Attribute Close-tag Open-tag

5 5 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: shortcut for empty tags (equivalent to  Attributes (which are unordered, in contrast to elements) only appear once in an element  There’s a single root element  XML is case-sensitive

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

7 7 XML Data Model Visualized (and simplified!) Root ?xml dblp mastersthesis article mdate key authortitleyearschool editortitleyearjournalvolumeee mdate key 2002… ms/Brown92 Kurt P…. PRPL… 1992 Univ…. 2002… tr/dec/… Paul R. The… Digital… SRC… 1997 db/labs/dec http://www. attribute root p-i element text

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

9 9 XML as a Super-HTML (MS Word) CIS 550: Database and Information Systems Fall 2004 311 Towne, Tuesday/Thursday 1:30PM – 3:00PM

10 10 XML Easily Encodes Relations sidsernoexp-grade 1570103B 23550103A 1 570103 B 23 550103 A Student-course-grade

11 11 But XML is More Flexible… “Non-First-Normal-Form” (NF 2 ) John Joan Jill Felicity … Coincides with “semi-structured data”, invented by DB people at Penn and Stanford

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

13 13 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 http://www.first.com/aspace http://www.fictitious.com/mypath is in the default namespace (aspace) is in myns is a different tag in otherns

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

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

16 16 An Example DTD Example DTD: <!ATTLIST mastersthesis(mdateCDATA#REQUIRED keyID#REQUIRED advisorCDATA#IMPLIED> … Example use of DTD in XML file: …

17 17 Representing Graphs and Links in XML John Smith Paper1 Paper2 …

18 18 Graph Data Model Root !DOCTYPE graph author article name title ref John Smith author1 Paper2 ?xml article id author1 author title Paper1

19 19 Graph Data Model Root !DOCTYPE graph author article name title ref John Smith Paper2 ?xml article id author1 author title Paper1

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

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

22 22 Basics of XML Schema Need to use the XML Schema namespace (generally named xsd)  simpleTypes are a way of restricting domains on scalars  Can define a simpleType based on integer, with values within a particular range  complexTypes are a way of defining element/attribute structures  Basically equivalent to !ELEMENT, but more powerful  Specify sequence, choice between child elements  Specify minOccurs and maxOccurs (default 1)  Must associate an element/attribute with a simpleType, or an element with a complexType

23 23 Simple Schema Example

24 24 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!

25 25 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 Next: basics of querying XML

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

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

28 28 Sample XML Kurt P. Brown PRPL: A Database Workload Specification Language 1992 Univ. of Wisconsin-Madison Paul R. McJones The 1995 SQL Reunion Digital System Research Center Report SRC1997-018 1997 db/labs/dec/SRC1997-018.html http://www.mcjones.org/System_R/SQL_Reunion_95/

29 29 XML Data Model Visualized Root ?xml dblp mastersthesis article mdate key authortitleyearschool editortitleyearjournalvolumeee mdate key 2002… ms/Brown92 Kurt P…. PRPL… 1992 Univ…. 2002… tr/dec/… Paul R. The… Digital… SRC… 1997 db/labs/dec http://www. attribute root p-i element text

30 30 Some Example XPath Queries  /dblp/mastersthesis/title  /dblp/*/editor  //title  //title/text()

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

32 32 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”]

33 33 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-stepparent::path-step  descendant::path-stepancestor::path-step  descendant-or-self::path-stepancestor-or-self::path-step  preceding-sibling::path-stepfollowing-sibling::path-step  preceding::path-stepfollowing::path-step  The previous XPaths we saw were in “abbreviated form”

34 34 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()]

35 35 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 nearly- complete “SQL of XML”…  … And we’ll briefly discuss XSLT, a different attempt to manipulate XML data


Download ppt "XML, Schemas, and XPath Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems October 14, 2004 Some slide content courtesy."

Similar presentations


Ads by Google