Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spring 20051 Part III: Introduction to XPath XML Path Language.

Similar presentations

Presentation on theme: "Spring 20051 Part III: Introduction to XPath XML Path Language."— Presentation transcript:

1 Spring 20051 Part III: Introduction to XPath XML Path Language

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

3 Spring 20053 Data Model for XPath bib book publisherauthor.. Addison-WesleySerge Abiteboul The root The root element Processing instruction Comment price=55

4 Spring 20054 XPath: Simple Expressions /bib/book/year Result: 1995 1998 /bib/paper/year Result: an empty set of nodes (there were no papers)

5 Spring 20055 XPath: Restricted Kleene Closure //author Result: Serge Abiteboul Rick Hull Victor Vianu Jeffrey D. Ullman /bib//first-name Result: Rick A set of 4 nodes

6 Spring 20056 XPath: Functions /bib/book/author/text() Result: Serge Abiteboul Victor Vianu Jeffrey D. Ullman Rick Hull doesn’t appear because he has no text node Some functions in XPath: text() = matches text nodes and returns the text value node() = matches any node (regardless of type) name() = returns the name of the current tag

7 Spring 20057 XPath: Wildcard //author/* Result: Rick Hull * Matches any element (but not text or attribute)

8 Spring 20058 XPath: Attribute Nodes /bib/book/@price Result: “55” @price means that price is an attribute @* Matches any attribute

9 Spring 20059 XPath: Qualifiers /bib/book/author[first-name] Result: Rick Hull [first-name] means that author has to have a first-name child element.

10 Spring 200510 XPath: More Qualifiers /bib/book/author[first-name][address[zip][city]]/last-name Result: returns all the last names of all authors with a first name and an address which includes city and zip code. […][…] means that author satisfies both qualifiers.

11 Spring 200511 XPath: More Qualifiers /bib/book[@price < 60] /bib/book[author/first-name = “Rick”] /bib/book[author/text()] /bib/book[2] Boolean expressions Existential expression Positional expression

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

13 Spring 200513 XPath: Summary (cont.) //papermatches a paper at any depth paper|bookmatches a paper or a book @pricematches a price attribute bib/book/@pricematches price attribute in book, in bib bib/book[@price<55]/author/last-name matches?

14 Spring 200514 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 bib element. This is advanced xmlogy

15 Spring 200515 XPath: More details We can navigate along 13 axes: ancestor ancestor-or-self attribute child descendant descendant-or-self following following-sibling namespace parent preceding preceding-sibling self

16 Spring 200516 XPath: More details Examples: child::author/child::last-name = author/last-name child::author/descendant-or-self::node()/child::zip = author//zip child::author/parent::node() = author/.. child::author/attribute::age = author/@age What does this mean ? book/publisher/parent::*/author /bib//address[ancestor::book]

17 Spring 200517 Shorthand notation Short formLong form child:: @attribute::.self::node()..parent::node() ///descendant-or-self::node()/ [number][position() = number]

18 Spring 200518 Shorthand notation - Examples Short formLong form /bib/book/child::bib/child::book /bib/book/@price/child::bib/child::book/ attribute::price /.//title (equivalent to //title) /self::node()/descendant-or- self::node()/child::title //author/../descendant-or-self::node()/ child::author/parent::node()

19 Spring 200519 XPath: Even more details name() = the name of the current node /bib//*[name()=”book”] same as /bib//book What does this mean ? /bib/book/author[.=../following-sibling::*/author and not(.=../preceding-sibling::*/author)] Navigation axis gives us more power !

Download ppt "Spring 20051 Part III: Introduction to XPath XML Path Language."

Similar presentations

Ads by Google