![Page 1: XPath Laks V.S. Lakshmanan UBC CPSC 534B. Overview data model recap XPath examples some advanced features summary](https://reader036.vdocuments.us/reader036/viewer/2022071806/56649d0b5503460f949defc1/html5/thumbnails/1.jpg)
XPath
Laks V.S. Lakshmanan
UBC
CPSC 534B
![Page 2: XPath Laks V.S. Lakshmanan UBC CPSC 534B. Overview data model recap XPath examples some advanced features summary](https://reader036.vdocuments.us/reader036/viewer/2022071806/56649d0b5503460f949defc1/html5/thumbnails/2.jpg)
Overview
• data model recap
• XPath examples
• some advanced features
• summary
![Page 3: XPath Laks V.S. Lakshmanan UBC CPSC 534B. Overview data model recap XPath examples some advanced features summary](https://reader036.vdocuments.us/reader036/viewer/2022071806/56649d0b5503460f949defc1/html5/thumbnails/3.jpg)
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/xpath
– Galax (complete reference impl. of XQuery) http://db.bell-labs.com/galax/
• (w3c.org – major resource many XML and other web related stuff, incl. XQuery, semantic web, etc.)
![Page 4: XPath Laks V.S. Lakshmanan UBC CPSC 534B. Overview data model recap XPath examples some advanced features summary](https://reader036.vdocuments.us/reader036/viewer/2022071806/56649d0b5503460f949defc1/html5/thumbnails/4.jpg)
Example XML DB <bib> <book price=15> <title> What is the name of this book?</title> <author nationality=american> <first> Raymond</first> <last>Smullyan</last> </author> <publisher>Penguin</publisher> <year>1970</year> </book> <book><publisher><name>Bentam Books</name><address>New
York</address></publisher> <author><first>Douglas</first><mi>R</mi><last>Hofstadter</last></author> <author>D.C. Dennett</author> <title>The Mind’s I: Reflections on Self and Soul</title> <year>1981</year> </book> </bib>
![Page 5: XPath Laks V.S. Lakshmanan UBC CPSC 534B. Overview data model recap XPath examples some advanced features summary](https://reader036.vdocuments.us/reader036/viewer/2022071806/56649d0b5503460f949defc1/html5/thumbnails/5.jpg)
Corresponding Tree root
bib
comments
processing instructions
root doc. element
book book
title author publisher
priceooo ooo
attribute
element
unorderedusually, single valued. Exception: IDREFS.
ordered. no apriori car-dinality constraint.
note distinction between the two roots.
![Page 6: XPath Laks V.S. Lakshmanan UBC CPSC 534B. Overview data model recap XPath examples some advanced features summary](https://reader036.vdocuments.us/reader036/viewer/2022071806/56649d0b5503460f949defc1/html5/thumbnails/6.jpg)
Simple Examples
• /bib/book/publisher • answer: <publisher>Penguin</publisher>
<publisher><name>Bentam Books</name>
<address>New York</address></publisher>
• /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.
![Page 7: XPath Laks V.S. Lakshmanan UBC CPSC 534B. Overview data model recap XPath examples some advanced features summary](https://reader036.vdocuments.us/reader036/viewer/2022071806/56649d0b5503460f949defc1/html5/thumbnails/7.jpg)
Descendants• /bib/book//address• answer: <address>New York</address></publisher>
• /bib/book//mi• answer: <mi>R</mi>
• //title • answer: <title> What is the name of this book?</title>
<title>The Mind’s I: Reflections on Self and Soul</title>
Note: results ordered as per i/p document order.
![Page 8: XPath Laks V.S. Lakshmanan UBC CPSC 534B. Overview data model recap XPath examples some advanced features summary](https://reader036.vdocuments.us/reader036/viewer/2022071806/56649d0b5503460f949defc1/html5/thumbnails/8.jpg)
Wildcard
• //author/*• answer: <first> Raymond</first> <last>Smullyan</last>
<first>Douglas</first><mi>R</mi><last>Hofstadter</last>
why only two authors(’ info.) returned?
Note: * matches any element.
• what does //* return?
• is the answer identical to that for /bib?
![Page 9: XPath Laks V.S. Lakshmanan UBC CPSC 534B. Overview data model recap XPath examples some advanced features summary](https://reader036.vdocuments.us/reader036/viewer/2022071806/56649d0b5503460f949defc1/html5/thumbnails/9.jpg)
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?
![Page 10: XPath Laks V.S. Lakshmanan UBC CPSC 534B. Overview data model recap XPath examples some advanced features summary](https://reader036.vdocuments.us/reader036/viewer/2022071806/56649d0b5503460f949defc1/html5/thumbnails/10.jpg)
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.
![Page 11: XPath Laks V.S. Lakshmanan UBC CPSC 534B. Overview data model recap XPath examples some advanced features summary](https://reader036.vdocuments.us/reader036/viewer/2022071806/56649d0b5503460f949defc1/html5/thumbnails/11.jpg)
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.
![Page 12: XPath Laks V.S. Lakshmanan UBC CPSC 534B. Overview data model recap XPath examples some advanced features summary](https://reader036.vdocuments.us/reader036/viewer/2022071806/56649d0b5503460f949defc1/html5/thumbnails/12.jpg)
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
$z$x.tag=bib & $y.tag=comment &$z.tag=publisher ...
distinguished node
![Page 13: XPath Laks V.S. Lakshmanan UBC CPSC 534B. Overview data model recap XPath examples some advanced features summary](https://reader036.vdocuments.us/reader036/viewer/2022071806/56649d0b5503460f949defc1/html5/thumbnails/13.jpg)
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?
![Page 14: XPath Laks V.S. Lakshmanan UBC CPSC 534B. Overview data model recap XPath examples some advanced features summary](https://reader036.vdocuments.us/reader036/viewer/2022071806/56649d0b5503460f949defc1/html5/thumbnails/14.jpg)
XPath – The 13 axes • child • descendant • attribute • descendant-or-self
• following
• following-sibling
• ancestor
• ancestor-or-self
• parent
• preceding
• preceding-sibling
• self
• namespace
![Page 15: XPath Laks V.S. Lakshmanan UBC CPSC 534B. Overview data model recap XPath examples some advanced features summary](https://reader036.vdocuments.us/reader036/viewer/2022071806/56649d0b5503460f949defc1/html5/thumbnails/15.jpg)
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::year book[author]/year• /bib//mi[ancestor::book] ?• /bib//mi/ancestor::book//publisher ?• /bib//mi/ancestor::*//publisher ?
![Page 16: XPath Laks V.S. Lakshmanan UBC CPSC 534B. Overview data model recap XPath examples some advanced features summary](https://reader036.vdocuments.us/reader036/viewer/2022071806/56649d0b5503460f949defc1/html5/thumbnails/16.jpg)
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
![Page 17: XPath Laks V.S. Lakshmanan UBC CPSC 534B. Overview data model recap XPath examples some advanced features summary](https://reader036.vdocuments.us/reader036/viewer/2022071806/56649d0b5503460f949defc1/html5/thumbnails/17.jpg)
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
author title publisher year
first mi last name address
example schema graph S
editor
*
*+ 1 ?
?
1 ??1 1
![Page 18: XPath Laks V.S. Lakshmanan UBC CPSC 534B. Overview data model recap XPath examples some advanced features summary](https://reader036.vdocuments.us/reader036/viewer/2022071806/56649d0b5503460f949defc1/html5/thumbnails/18.jpg)
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?
![Page 19: XPath Laks V.S. Lakshmanan UBC CPSC 534B. Overview data model recap XPath examples some advanced features summary](https://reader036.vdocuments.us/reader036/viewer/2022071806/56649d0b5503460f949defc1/html5/thumbnails/19.jpg)
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.