XQuery 1.0 - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

XQuery 1.0

Description:

... me all books where 'Sailing' and 'Windsurfing' appear at least once in the ... AND contains($p, 'Windsurfing')) RETURN $b/title. Quantified Expressions ... – PowerPoint PPT presentation

Number of Views:165
Avg rating:3.0/5.0
Slides: 39
Provided by: yildira
Category:
Tags: surfing | wind | xquery

less

Transcript and Presenter's Notes

Title: XQuery 1.0


1
XQuery 1.0
  • Ceng 352

2
Outline
  • Introduction
  • What is XQuery?
  • Example queries
  • Detail
  • Expressions
  • XPath
  • FLOWR
  • Joins
  • Quantified Expressions
  • Examples

3
What is XQuery?
  • XQuery is the language for querying XML data
  • XQuery for XML is like SQL for databases
  • XQuery is built on XPath expressions
  • XQuery is defined by the W3C
  • XQuery is supported by all the major database
    engines (IBM, Oracle, Microsoft, etc.)
  • XQuery will become a W3C standard - and
    developers can be sure that the code will work
    among different products

4
XQuery - Examples of Use
  • Extract information to use in a Web Service
  • Generate summary reports
  • Transform XML data to XHTML
  • Search Web documents for relevant information

5
XQuery is Not (Yet) a Web Standard
  • XQuery is compatible with several W3C standards,
    such as XML, Namespaces, XSLT, XPath, and XML
    Schema.
  • However, XQuery 1.0 is not yet a W3C
    Recommendation (XQuery is a Working Draft).
    Hopefully it will be a recommendation in the near
    future.

6
Books.xml
7
How to Select Nodes From "books.xml"?
  • Functions
  • XQuery uses functions to extract data from XML
    documents.
  • The doc() function is used to open the
    "books.xml" file
  • doc("books.xml")
  • Path Expressions
  • XQuery uses path expressions to navigate through
    elements in an XML document.
  • The following path expression is used to select
    all the title elements in the "books.xml" file
  • doc("books.xml")/bookstore/book/titl
    e

Everyday Italian
Harry Potter
XQuery Kick Start
Learning XML
8
How to Select Nodes From "books.xml"?
  • Predicates
  • XQuery uses predicates to limit the extracted
    data from XML documents.
  • The following predicate is used to select all the
    book elements under the bookstore element that
    have a price element with a value that is less
    than 30
  • doc("books.xml")/bookstore/bookprice

Har
ry Potter J K. Rowling
2005 29.99
book
9
How to Select Nodes From "books.xml" With FLWOR
  • doc("books.xml")/bookstore/bookprice30/title
  • all the title elements under the book elements
    that is under
  • the bookstore element that have a price element
    with a value that is higher than 30.
  • for x in doc("books.xml")/bookstore/book
  • where x/price30
  • return x/title
  • for x in doc("books.xml")/bookstore/book where
    x/price30
  • order by x/title
  • return x/title

XQuery Kick Start
Learning XML
Learning XML
XQuery Kick Start
10
XQuery Conditional Expressions
  • for x in doc("books.xml")/bookstore/book
  • return
  • if (x/_at_category"CHILDREN")
  • then x/title/text()
  • else x/title/text()
  • Everyday Italian Harry
    Potter Learning XML
    XQuery Kick Start

11
Detail
12
XQuery Expressions
  • Literals Hello 47 4.7
  • Variables x
  • Constructed sequences
  • a, b means concatenation
  • 5 to 8 means 5,6,7,8
  • (1, (2, 3), 4) is the same as 1,2,3,4

13
Path Expressions
  • Result of path expression set of values that
    along with their containing elements/attributes
    match the specified path
  • /bank/customer/customer-name
  • Joe
  • Ali
  • /bank/customer/customer-name/text()
  • Joe
  • Ali

14
Path expressions
  • Initial / denotes the root of the document
  • Path expressions are evaluated from left to
    right
  • Selection predicates may follow any step in a
    path, in
  • /bank/accountbalance400
  • Attributes are accessed using _at_
  • /bank/accountbalance400/_at_account-number

15
XPath
  • 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/paper1 matches first paper in bib
  • bib/paperlast() matches last paper in bib
  • bib//paper matches a paper in bib, at any
    depth
  • //paper matches a paper at any depth
  • paperbook matches a paper or a book
  • _at_price matches a price attribute
  • bib/book/_at_price matches price attribute in book,
    in bib

16
Construction mode, Computation mode
  • Element construction ...
  • eg, John Smithx1234hone
  • may include attribute bindings in the start tag
  • eg, ...
  • the content between the start and end tags (as
    well as the attribute values) is in construction
    mode
  • to switch to computation mode, must use
  • eg, 23 41 is
    equivalent to
    y3541

17
FLWOR semantics
  • FOR clause, LET clause generate list of tuples of
    bound variables
  • By iterating over a set of nodes
  • By binding a variable to the result of an
    expression
  • WHERE clause applies a predicate to filter the
    tuples produced by FOR/LET
  • ORDER BY clause imposes order on the surviving
    tuples
  • RETURN clause is executed for each surviving tuple

18
FOR v.s. LET
  • FOR
  • Binds node variables ? iteration
  • LET
  • Binds collection variables ? one value

19
FOR v.s. LET
Returns ...
...
...
...
FOR x IN document("bib.xml")/bib/book
RETURN x
LET x document("bib.xml")/bib/book
RETURN x
Returns ...
... ...

...
20
Joins
  • FOR a in /bank/account,
  • c in /bank/customer,
  • d in /bank/depositor
  • WHERE a/account-number d/account-number
  • and
  • c/customer-name d/customer-name
  • RETURN c a

21
Joins
  • List all suppliers. If a supplier offers medical
    items, list the descriptions of the items
  • FOR s IN document(suppliers.xml)//supplier
  • ORDER BY s/name
  • RETURN
  • s/name,
  • FOR ci IN document(catalog.xml)//itemsupp_no
    s/number,
  • mi IN document(medical_items.xml)//itemnumbe
    r ci/item_no
  • RETURN mi/description

22
Quantified Expressions
  • Existential Quantification
  • Give me all books where Sailing and
    Windsurfing appear at least once in the same
    paragraph
  • FOR b IN //book
  • WHERE SOME p IN b//para SATISFIES
    (contains(p, Sailing)
  • AND contains(p, Windsurfing))
  • RETURN b/title

23
Quantified Expressions
  • Universal Quantification
  • Give me all books where Sailing appears in
    every paragraph
  • FOR b IN //book
  • WHERE EVERY p IN b//para SATISFIES
    contains(p, Sailing)
  • RETURN b/title

24
XQuery Functions
  • Predefined Functions
  • avg, sum, count, max, min
  • empty(), text()
  • User defined Functions

25
Examples
FOR p IN distinct(docu
ment("bib.xml")//publisher) LET b doc
ument("bib.xml")/bookpublisher p
WHERE count(b) 100 RETURN p

count a (aggregate) function that returns the
number of elms
Publishers who published more than 100 books
26
Examples
Find books whose price is larger than average
LET aavg(document("bib.xml")/bib/book/_at_price)
FOR b in document("bib.xml")/bib/book
WHERE b/_at_price a
RETURN b
27
Examples
  • for b in document(books.xml)//book
  • where b/author/firstname John
  • and b/author/lastname Smith
  • return
  • b/title,
  • b/price
  • May return
  • XML29.99ook
  • DOM and SAX40e

28
Examples
  • for b in document(books.xml)//book
  • where b/author/firstname John
  • and b/author/lastname Smith
  • return
  • b/title,
  • b/price
  • Will return
  • b/title,b/price
  • b/title,b/price

29
Examples Equivalent Query
  • for b in document(books.xml)//book
  • author/firstname
    John
  • and author/lastname
    Smith
  • return
  • b/title,
  • b/price

30
Example
  • let d document(depts.xml)//departmentname
    cse
  • for s in d//gradstudent
  • where s/gpa 3.5
  • return
  • s/name,s/gpa

31
Example xml
32
Example Aggregates
  • Find all books with more than 3 authors
  • FOR x IN document("bib.xml")/bib/book
  • WHERE count(x/author)3
  • RETURN x
  • count a function that counts
  • avg computes the average
  • sum computes the sum
  • distinct-values eliminates duplicates

33
Same Query
  • FOR x IN document("bib.xml")/bib/bookcount(autho
    r)3
  • RETURN x

34
Example
  • For each author, return all book titles he/she
    wrote

What about duplicate authors?
35
Example
  • Same eliminate duplicate authors

36
Example sorting
  • Sort the expensive books by first author name,
    book title
  • LET b document(bib.xml)//bookprice 100
  • ORDER BY b/author1, b/title
  • RETURN b

37
Questions 1
  •       for i in document("data/R-item
    s.xml")//item_tuple    where      contains(i/de
    scription, "Bicycle")    return        
                 i/itemno             
    i/description           

List the item number and description of all
bicycles
38
Question 2
  • for u in document("data\R-users.xml")//user_tuple
    return           u/name
                   for b in document("data\R-bids.
    xml")//bid_tuple        where u/userid
    b/userid        order by b/itemno
            return                   b/item
    no          b/bid_date          b/bid   
                   

List all users, and for each user, list its
bidding item, bidding price, and date
Write a Comment
User Comments (0)
About PowerShow.com