Spring Part III: Introduction to XPath XML Path Language
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
Spring Data Model for XPath bib book publisherauthor.. Addison-WesleySerge Abiteboul The root The root element Processing instruction Comment price=55
Spring XPath: Simple Expressions /bib/book/year Result: /bib/paper/year Result: an empty set of nodes (there were no papers)
Spring 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
Spring 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
Spring XPath: Wildcard //author/* Result: Rick Hull * Matches any element (but not text or attribute)
Spring XPath: Attribute Nodes Result: means that price is an Matches any attribute
Spring XPath: Qualifiers /bib/book/author[first-name] Result: Rick Hull [first-name] means that author has to have a first-name child element.
Spring 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.
Spring XPath: More Qualifiers < 60] /bib/book[author/first-name = “Rick”] /bib/book[author/text()] /bib/book[2] Boolean expressions Existential expression Positional expression
Spring 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
Spring XPath: Summary (cont.) //papermatches a paper at any depth paper|bookmatches a paper or a a price attribute price attribute in book, in bib matches?
Spring 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
Spring 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
Spring 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 = What does this mean ? book/publisher/parent::*/author /bib//address[ancestor::book]
Spring Shorthand notation Short formLong form ///descendant-or-self::node()/ [number][position() = number]
Spring Shorthand notation - Examples Short formLong form /bib/book/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()
Spring 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 !