Xpath and Xquery • We Will Ask for Weekly Update from All Group Members Introduction to Databases to Avoid Any Last Min “X Did Not Do Anything”

Xpath and Xquery • We Will Ask for Weekly Update from All Group Members Introduction to Databases to Avoid Any Last Min “X Did Not Do Anything”

3/5/19 2 Announcements (Tue. Mar. 5) • Homework #3 (2 probs) to be released today. Due in two weeks • Project milestone #1 feedback : See private posts on Piazza XPath and XQuery • We will ask for weekly update from all group members Introduction to Databases to avoid any last min “X did not do anything” .. to be emailed soon CompSci 316 Spring 2019 • Project milestone 2 is due in 3 weeks 3 4 Query languages for XML Example DTD and XML <?xml version="1.0"?> • XPath <!DOCTYPE bibliography [ <!ELEMENT bibliography (book+)> • Path expressions with conditions <!ELEMENT book (title, author*, publisher?, year?, section*)> <!ATTLIST book ISBN ID #REQUIRED> F <!ATTLIST book price CDATA #IMPLIED> Building block of other standards (XQuery, XSLT, XLink, <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA)> XPointer, etc.) <!ELEMENT publisher (#PCDATA)> <!ELEMENT year (#PCDATA)> <!ELEMENT i (#PCDATA)> • XQuery <!ELEMENT content (#PCDATA|i)*> <!ELEMENT section (title, content?, section*)> • XPath + full-fledged SQL-like query language ]> <bibliography> <book ISBN="ISBN-10" price="80.00"> • XSLT: mostly used a stylesheet language <title>Foundations of Databases</title> <author>Abiteboul</author> • XPath + transformation templates <author>Hull</author> <author>Vianu</author> • <publisher>Addison Wesley</publisher> We are not going to cover it in this course <year>1995</year> <section>…</section>… </book> … </bibliography> 5 6 XPath Try the queries in this lecture online • There are many online Xpath testers -- e.g. http://codebeautify.org/Xpath-Tester • XPath specifies path expressions that match XML • Try with this example (or change it for different queries) data by navigating down (and occasionally up and across) the tree <bibliography> <book ISBN="ISBN-10"> <title>Foundations of Databases</title> • Example <author>Abiteboul</author> <author>Hull</author> <author>Vianu</author> • Query: /bibliography/book/author <publisher>Addison Wesley</publisher> <year>1995</year> • <section>abc</section> Like a file system path, except there can be multiple <price>25</price> <price>75</price> “subdirectories” with the same name </book> • Result: all author elements reachable from root via the <book ISBN="ISBN-11"> <title>DBSTS</title> path /bibliography/book/author <author>Ramakrishnan</author> <author>Gehrke</author> <publisher>Addison Wesley</publisher> <year>1999</year> <section>abc</section> <price>15</price> <price>10</price> </book> </bibliography> 1 3/5/19 7 8 Basic XPath constructs Simple XPath examples <!DOCTYPE bibliography [ <!ELEMENT bibliography (book+)> <!ELEMENT book (title, author*, publisher?, year?, section*)> <!ATTLIST book ISBN ID #REQUIRED> / separator between steps in a path • All book titles <!ATTLIST book price CDATA #IMPLIED> <!ELEMENT title (#PCDATA)> /bibliography/book/title <!ELEMENT author (#PCDATA)> name matches any child element with this tag name <!ELEMENT publisher (#PCDATA)> <!ELEMENT year (#PCDATA)> • All book ISBN numbers <!ELEMENT i (#PCDATA)> * matches any child element <!ELEMENT content (#PCDATA|i)*> <!ELEMENT section (title, content?, section*)> /bibliography/book/@ISBN ]> @name matches the attribute with this name • All title elements, anywhere in the document @* matches any attribute //title //matches any descendent element or the current • All section titles, anywhere in the document element itself //section/title . matches the current element • Authors of bibliographical entries (suppose there .. matches the parent element are articles, reports, etc. in addition to books) /bibliography/*/author 9 10 Predicates in path expressions Predicates in path expressions – contd. [condition] matches the “current” element if • Books with author “Abiteboul” condition evaluates to true on the current element /bibliography/book[author='Abiteboul'] • Books with price lower than $50 • Books with a publisher child element /bibliography/book[@price<50] /bibliography/book[publisher] • XPath will automatically convert the price string to a • Prices of books authored by “Abiteboul” numeric value for comparison /bibliography/book[author='Abiteboul']/@price <!DOCTYPE bibliography [ <!DOCTYPE bibliography [ <!ELEMENT bibliography (book+)> <!ELEMENT bibliography (book+)> <!ELEMENT book (title, author*, publisher?, year?, section*)> <!ELEMENT book (title, author*, publisher?, year?, section*)> <!ATTLIST book ISBN ID #REQUIRED> <!ATTLIST book ISBN ID #REQUIRED> <!ATTLIST book price CDATA #IMPLIED> <!ATTLIST book price CDATA #IMPLIED> <!ELEMENT title (#PCDATA)> <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA)> <!ELEMENT author (#PCDATA)> <!ELEMENT publisher (#PCDATA)> <!ELEMENT publisher (#PCDATA)> <!ELEMENT year (#PCDATA)> <!ELEMENT year (#PCDATA)> <!ELEMENT i (#PCDATA)> <!ELEMENT i (#PCDATA)> <!ELEMENT content (#PCDATA|i)*> <!ELEMENT content (#PCDATA|i)*> <!ELEMENT section (title, content?, section*)> <!ELEMENT section (title, content?, section*)> ]> ]> 11 12 More complex predicates Predicates involving node-sets /bibliography/book[author='Abiteboul'] Predicates can use and, or, and not • There may be multiple authors, so author in general • Books with price between $40 and $50 returns a node-set (in XPath terminology) /bibliography/book[40<=@price and @price<=50] • The predicate evaluates to true as long as it • Books authored by “Abiteboul” or those with price evaluates true for at least one node in the node-set, no lower than $50 i.e., at least one author is “Abiteboul” /bibliography/book[author='Abiteboul' or @price>=50] • Tricky query /bibliography/book[author='Abiteboul' or not(@price<50)] • Any difference between these two queries? /bibliography/book[author='Abiteboul' and author!='Abiteboul'] <!DOCTYPE bibliography [ • Will it return any books? <!DOCTYPE bibliography [ <!ELEMENT bibliography (book+)> <!ELEMENT bibliography (book+)> <!ELEMENT book (title, author*, publisher?, year?, section*)> <!ELEMENT book (title, author*, publisher?, year?, section*)> <!ATTLIST book ISBN ID #REQUIRED> <!ATTLIST book ISBN ID #REQUIRED> <!ATTLIST book price CDATA #IMPLIED> <!ATTLIST book price CDATA #IMPLIED> <!ELEMENT title (#PCDATA)> <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA)> <!ELEMENT author (#PCDATA)> <!ELEMENT publisher (#PCDATA)> <!ELEMENT publisher (#PCDATA)> <!ELEMENT year (#PCDATA)> <!ELEMENT year (#PCDATA)> <!ELEMENT i (#PCDATA)> <!ELEMENT i (#PCDATA)> <!ELEMENT content (#PCDATA|i)*> <!ELEMENT content (#PCDATA|i)*> <!ELEMENT section (title, content?, section*)> <!ELEMENT section (title, content?, section*)> ]> ]> 2 3/5/19 13 14 XPath operators and functions More XPath examples Frequently used in conditions: • All elements whose tag names contain “section” x + y, x – y, x * y, x div y, x mod y (e.g., “subsection”) //*[contains(name(), 'section')] contains(x, y) true if string x contains string y • Title of the first section in each book count(node-set) counts the number nodes in node- /bibliography/book/section[position()=1]/title set • A shorthand: /bibliography/book/section[1]/title position() returns the “context position” (roughly, • Title of the last section in each book the position of the current node in the node-set /bibliography/book/section[position()=last()]/title containing it) <!DOCTYPE bibliography [ <!ELEMENT bibliography (book+)> last() returns the “context size” (roughly, the size of <!ELEMENT book (title, author*, publisher?, year?, section*)> <!ATTLIST book ISBN ID #REQUIRED> the node-set containing the current node) <!ATTLIST book price CDATA #IMPLIED> <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA)> name() returns the tag name of the current <!ELEMENT publisher (#PCDATA)> <!ELEMENT year (#PCDATA)> element <!ELEMENT i (#PCDATA)> <!ELEMENT content (#PCDATA|i)*> <!ELEMENT section (title, content?, section*)> ]> 15 16 More XPath examples – contd. A tricky example • Books with fewer than 10 sections • Suppose for a moment that price is a child /bibliography/book[count(section)<10] element of book, and there may be multiple • All elements whose parent’s tag name is not “book” prices per book //*[name()!='book']/* • Books with some price in range [20, 50] • Wrong answer: /bibliography/book[price >= 20 and price <= 50] • Correct answer: /bibliography/book[price[. >= 20 and . <= 50]] <!DOCTYPE bibliography [ <!ELEMENT bibliography (book+)> <!ELEMENT book (title, author*, publisher?, year?, section*)> <!ATTLIST book ISBN ID #REQUIRED> <!ATTLIST book price CDATA #IMPLIED> <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA)> <!ELEMENT publisher (#PCDATA)> <!ELEMENT year (#PCDATA)> <!ELEMENT i (#PCDATA)> <!ELEMENT content (#PCDATA|i)*> <!ELEMENT section (title, content?, section*)> ]> 17 18 De-referencing IDREF’s General XPath location steps id(identifier) returns the element with identifier • Technically, each XPath query consists of a series of location steps separated by / • Suppose that books can reference other books <section><title>Introduction</title> • Each location step consists of XML is a hot topic these days; see <bookref ISBN="ISBN-10"/> for more • An axis: one of self, attribute, parent, child, ancestor,† details… ancestor-or-self,† descendant, descendant-or-self, following, </section> following-sibling, preceding,† preceding-sibling,† and • Find all references to books written by “Abiteboul” namespace in the book with “ISBN-10” • A node-test: either a name test (e.g., book, section, *) or a type test (e.g., text(), node(), comment()), separated from /bibliography/book[@ISBN='ISBN-10'] //bookref[id(@ISBN)/author='Abiteboul'] the axis by :: • Or simply: Zero of more predicates (or

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    7 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us