Download presentation
Presentation is loading. Please wait.
Published byShannon Riley Modified over 9 years ago
1
XPath Laks V.S. Lakshmanan UBC CPSC 534B
2
Overview data model recap XPath examples some advanced features summary
3
XPath in the beginning used to be part of XSLT –A formal semantics of patterns in XSLT (Phil Wadler) also influenced XLink, XPointer, resources: –www.w3c.org/TR/xpathwww.w3c.org/TR/xpath –Galax (complete reference impl. of XQuery) http://db.bell-labs.com/galax/ http://db.bell-labs.com/galax/ (w3c.org – major resource many XML and other web related stuff, incl. XQuery, semantic web, etc.)
4
Example XML DB What is the name of this book? Raymond Smullyan Penguin 1970 Bentam Books New York Douglas R Hofstadter D.C. Dennett The Mind’s I: Reflections on Self and Soul 1981
5
Corresponding Tree root bib comments processing instructions root doc. element book titleauthorpublisher price ooo attribute element unordered usually, single valued. Exception: IDREFS. ordered. no apriori car- dinality constraint. note distinction between the two roots.
6
Simple Examples /bib/book/publisher answer: Penguin Bentam Books New York /bib/book/author/name what’s the answer? / -- returns root element, while /bib -- returns doc. root element, i.e., the bib element under the root.
7
Descendants /bib/book//address answer: New York /bib/book//mi answer: R //title answer: What is the name of this book? The Mind’s I: Reflections on Self and Soul Note: results ordered as per i/p document order.
8
Wildcard //author/* answer: Raymond Smullyan Douglas R Hofstadter why only two authors(’ info.) returned? Note: * matches any element. what does //* return? is the answer identical to that for /bib?
9
Attributes XML data model – diff. kinds of nodes: element, attribute, text, comment, processing instruction,... /bib/book/@price answer: ``15” contrast with answer for previous queries. /bib/book/@* what do you think it should return?
10
Branching & Qualifiers/Predicates /bib/book/author[mi] returns only second book. /bib/book[author/@nationality=american] returns only first book. /bib/book[publisher[address][name]][price<20]//title returns the titles of books with a publisher who has a name & an address and with a price < 20.
11
Reaching out at other nodes XPath has the functions text(), node(), name(). Meanings illustrated below. /bib/book/publisher/text() answer: Penguin –why first pub doesn’t appear? /bib/book/node() returns all child nodes of book, regardless of type (attr, text, element). /bib/*/name() – returns tag of current element.
12
Mixing it all /bib/book[author[hobby=tennis]][title/text( )]//year what does it say? Features of XPath seen so far tree pattern query. $x $y $w $z $x.tag=bib & $y.tag=comment & $z.tag=publisher... distinguished node
13
XPath – Summary / -- matches the root. /bib – matches bib element under root. bib – matches any bib element. * -- matches any element. bib/book – matches any book element under a bib element. bib//book – ditto, but at any depth. //book – matches any book element at any depth in the document. author|editor – matches any author or editor element. @hobby – matches any hobby attribute. //author/@hobby -- matches any price attribute of an author at any depth of the doc. /bib/book[author[@hobby]][@price<20]//publisher – what does it match?
14
XPath – The 13 axes child descendant attribute descendant-or-self following following-sibling ancestor ancestor-or-self parent preceding preceding-sibling self namespace
15
Some Abbreviations child::book/child::author book/author child::book/descendant::mi book//mi child::first/parent::* first/.. child::book/attribute::price book/@price child::book/child::author/parent::*/child::y ear book[author]/year /bib//mi[ancestor::book] ? /bib//mi/ancestor::book//publisher ? /bib//mi/ancestor::*//publisher ?
16
More examples /bib/descendant::*[name()=address] /bib//address /bib//book//first/parent::*[name()=author] /bib//book//mi[ancestor:*[name()=author or name()=editor]] navigation axes increase expressive power BUT, when schema is known, can often simplify XPath expressions
17
Simplifying XPE with schema /bib//book//first/parent::*[name()=author] /bib//book//author[first] /bib//book//mi[ancestor:*[name()=author or name()=editor] S /bib//book//*[name()=author or name()]//mi bib book authortitlepublisheryear firstmilastnameaddress example schema graph S editor * * +1? ? 1 ? ? 1 1
18
XPath, formally speaking XPE a binary relation over document nodes: p(context node, answer node). basic cases: –“.” is, i.e., self (x,x) –“..” is parent, i.e., (current node, its parent) –publisher/address is (current node, address node reachable from current node via publisher child) –book/*/mi/../name() book/*[mi]/name() what is the relationship captured by this XPE?
19
XPath, formally speaking Relative path expressions – every XPE E we have seen so far, except E may be used as a predicate or as an extension to an absolute XPE. Absolute XPE – how you get a (unary) query out of an XPE. E.g.: author/mi and publisher/address are relative XPEs. //author/mi, /bib/book[author/mi], /bib/book[author/mi][publisher/address]//year are all absolute XPEs. More details: see resources and stay tuned for homework.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.