CSE 636 Data Integration Fall 2006 XML Query Languages XPath
2 (11/99) Building block for other W3C standards: – XSL Transformations (XSLT) – XML Link (XLink) – XML Pointer (XPointer) – XQuery Was originally part of XSL XPath
3 Example for XPath Queries Addison-Wesley Serge Abiteboul Rick Hull Victor Vianu Foundations of Databases 1995 Freeman Jeffrey D. Ullman Principles of Database and Knowledge Base Systems 1998 Addison-Wesley Serge Abiteboul Rick Hull Victor Vianu Foundations of Databases 1995 Freeman Jeffrey D. Ullman Principles of Database and Knowledge Base Systems 1998
4 Data Model for XPath XML PI Element book Element publisher Element author Text Addison-Wesley Text Serge Abiteboul Comment Element book The root The root element … … Element bib / Document
5 XPath: Simple Expressions /bib/book/year Result: /bib/paper/year Result: empty(there were no papers)
6 XPath: Restricted Kleene Closure //author Result: Serge Abiteboul Rick Hull Victor Vianu Jeffrey D. Ullman /bib//first-name Result: Rick
7 XPath: Functions /bib/book/author/text() Result: Serge Abiteboul Jeffrey D. Ullman Rick Hull doesn’t appear because he has firstname, lastname Functions in XPath: –text()= matches the text value –node()= matches any node (= * or text()) –name()= returns the name of the current tag
8 XPath: Wildcard //author/* Result: Rick Hull * Matches any element
9 XPath: Attribute Nodes Result: means that price is has to be an attribute
10 XPath: Qualifiers /bib/book/author[first-name] Result: Rick Hull
11 XPath: More Qualifiers /bib/book/author[firstname][address[//zip][city]]/lastname Result: … …
12 XPath: More Qualifiers < “60”] < “25”] /bib/book[author/text()]
13 XPath: Summary bibmatches a bib element *matches any element /matches the root element /bibmatches a bib element under root bib/papermatches a paper in bib bib//papermatches a paper in bib, at any depth //papermatches a paper at any depth paper|bookmatches a paper or a a price attribute price attribute in book, in bib matches…
14 XPath: More Details An XPath expression, p, establishes a relation between: –A context node, and –A node in the answer set In other words, p denotes a function: –S[p] : Nodes {Nodes} Examples: –author/firstname –. = self –.. = parent –part/*/*/subpart/../name = part/*/*[subpart]/name
15 The Root and the Root 1 2 bib is the “document element” The “root” is above bib /bib = returns the document element / = returns the root Why? Because we may have comments before and after They become siblings of
16 XPath: More Details We can navigate along 13 axes: ancestor ancestor-or-self parent attribute child descendant-or-self descendant following following-sibling namespace preceding preceding-sibling self We’ve only seen these, so far
17 XPath: More Details Examples: –child::author/child:lastname= author/lastname –child::author/descendant-or-self::node()/child::zip = author//zip –child::author/parent::*= author/.. –child::author/attribute::age= What does this mean ? –/bib/book/publisher/parent::*/author –/bib//address[ancestor::book] –/bib//author/ancestor::*//zip
18 XPath: Even More Details name() = the name of the current node /bib//*[name()=book] same as /bib//book What does this mean? /bib//*[ancestor::*[name()!=book]] Is it equivalent to the following? /bib//* /bib//*[name()!=book]//* Navigation axis gives us strictly more power!
19 XPath: Example How do we evaluate this XPath expression? /bib//*[name()!=book]//* Let’s take it one step at a time bib AB book CD
20 XPath: Example /bib returns the following list of one node: Node bib AB book CD
21 XPath: Example /bib//* when executed on the previous node list, returns the following new list of nodes: Node A B book CD CD C D
22 XPath: Example /bib//*[name()!=book] when executed on the previous node list, it eliminates one node: Node A B book CD C D
23 XPath: Example /bib//*[name()!=book]//* gives us the resulting node list of the XPath expression: Node book CD C D
24 Keys in XML Schema We forgot something about XML Schema –Keys –Key References Why? XPath is used for keys and key references
25 Keys in XML Schema Lawnmower Baby Monitor Lapis Necklace Sturdy Shelves Lawnmower Baby Monitor Lapis Necklace Sturdy Shelves XML: XML Schema:
26 Keys in XML Schema … … … … XML Schema:
27 In general, two flavors: Note All XPath expressions “start” at the element currently being defined The fields must identify a single node Keys in XML Schema … … … …
28 Keys in XML Schema Unique = guarantees uniqueness Key = guarantees uniqueness and existence All XPath expressions are “restricted”: –/a/b | /a/cOK for selector –//a/b/*/cOK for field Note: better than DTD’s ID mechanism
29 Keys in XML Schema Examples Recall: must have a single forename, surname
30 Foreign Keys in XML Schema Examples
31 References Lecture Slides –Dan Suciu – 6xpath.htmhttp:// 6xpath.htm – 4constraintkeys.htmhttp:// 4constraintkeys.htm BRICS XML Tutorial –A. Moeller, M. Schwartzbach – W3C's XPath homepage – W3C's XML Schema homepage – XML School –