1 / 19

Part III: Introduction to XPath

Part III: Introduction to XPath. XML Path Language. Example for XPath Queries. < bib > < book price =“55”> < publisher >Addison-Wesley</ publisher > < author >Serge Abiteboul</ author > < author >< first-name >Rick</ first-name >

Download Presentation

Part III: Introduction to XPath

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Part III: Introduction to XPath XML Path Language

  2. Example for XPath Queries <bib> <bookprice=“55”> <publisher>Addison-Wesley</publisher> <author>Serge Abiteboul</author> <author><first-name>Rick</first-name> <last-name>Hull</last-name> </author> <author>Victor Vianu</author> <title>Foundations of Databases</title> <year>1995</year> </book> <book> <publisher>Freeman</publisher> <author>Jeffrey D. Ullman</author> <title>Principles of Database and Knowledge Base Systems</title> <year>1998</year> </book> </bib>

  3. bib Data Model for XPath The root Processing instruction Comment The root element book book publisher author price=55 . . . . Addison-Wesley Serge Abiteboul

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

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

  6. 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. XPath: Wildcard //author/* Result: <first-name>Rick</first-name> <last-name>Hull</last-name> * Matches any element (but not text or attribute)

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

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

  10. 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. 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. XPath: Summary • bib matches a bib element • * matches any element • / matches the root element • /bib matches a bib element under root • bib/paper matches a paper in bib • bib//paper matches a paper in bib, at any depth

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

  14. The Root <?xml version="1.0"?> <bib> <paper>1</paper> <paper>2</paper> </bib> • 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 <bib>…</bib>; they become siblings of bib element. • This is advanced xmlogy

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

  18. Shorthand notation - Examples

  19. 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 !

More Related