XQuery - PowerPoint PPT Presentation

About This Presentation
Title:

XQuery

Description:

XQuery Values FLWR Expressions Other Expressions XQuery XQuery extends XPath to a query language that has power similar to SQL. XQuery is an expression language. – PowerPoint PPT presentation

Number of Views:85
Avg rating:3.0/5.0
Slides: 29
Provided by: Jeff564
Category:
Tags: query | xquery

less

Transcript and Presenter's Notes

Title: XQuery


1
XQuery
  • Values
  • FLWR Expressions
  • Other Expressions

2
XQuery
  • XQuery extends XPath to a query language that has
    power similar to SQL.
  • XQuery is an expression language.
  • Like relational algebra --- any XQuery expression
    can be an argument of any other XQuery
    expression.
  • Unlike RA, with the relation as the sole
    datatype, XQuery has a subtle type system.

3
The XQuery Type System
  • Atomic values strings, integers, etc.
  • Also, certain constructed values like true(),
    date(2004-09-30).
  • Nodes.
  • Seven kinds.
  • Well only worry about four, on next slide.

4
Some Node Types
  • Element Nodes are like nodes of semistructured
    data.
  • Described by !ELEMENT declarations in DTDs.
  • Attribute Nodes are attributes, described by
    !ATTLIST declarations in DTDs.
  • Text Nodes PCDATA.
  • Document Nodes represent files.

5
Example Document
  • ltBARSgt
  • ltBAR name JoesBargt
  • ltPRICE theBeer Budgt2.50lt/PRICEgt
  • ltPRICE theBeer Millergt3.00lt/PRICEgt
  • lt/BARgt
  • ltBEER name Bud soldBy JoesBar
  • SuesBar /gt
  • lt/BARSgt

6
Example Nodes
BARS
name JoesBar
SoldBy
name Bud
BEER
BAR
theBeer Miller
theBeer Bud
PRICE
PRICE
3.00
2.50
Green element Gold attribute Purple text
7
Document Nodes
  • Form document(ltfile namegt).
  • Establishes a document to which a query applies.
  • Example document(/usr/ullman/bars.xml)

8
XQuery Values
  • Item node or atomic value.
  • Value ordered sequence of zero or more items.
  • Examples
  • () empty sequence.
  • (Hello, World)
  • (Hello, ltPRICEgt2.50lt/PRICEgt, 10)

9
Nesting of Sequences Ignored
  • A value can, in principle, be an item of another
    value.
  • But nested list structures are expanded.
  • Example ((1,2),(),(3,(4,5))) (1,2,3,4,5)
    1,2,3,4,5.
  • Important when values are computed by
    concatenating other values.

10
FLWR Expressions
  1. One or more for and/or let clauses.
  2. Then an optional where clause.
  3. A return clause.

11
Semantics of FLWR Expressions
  • Each for creates a loop.
  • let produces only a local definition.
  • At each iteration of the nested loops, if any,
    evaluate the where clause.
  • If the where clause returns TRUE, invoke the
    return clause, and append its value to the output.

12
FOR Clauses
  • for ltvariablegt in ltexpressiongt, . . .
  • Variables begin with .
  • A for-variable takes on each item in the sequence
    denoted by the expression, in turn.
  • Whatever follows this for is executed once for
    each value of the variable.

13
Example FOR
  • for beer in document(bars.xml)/BARS/BEER/_at_name
  • return
  • ltBEERNAMEgt beer lt/BEERNAMEgt
  • beer ranges over the name attributes of all
    beers in our example document.
  • Result is a list of tagged names, like
    ltBEERNAMEgtBudlt/BEERNAMEgt ltBEERNAMEgtMillerlt/BEERNAM
    Egt . . .

14
LET Clauses
  • let ltvariablegt ltexpressiongt, . . .
  • Value of the variable becomes the sequence of
    items defined by the expression.
  • Note let does not cause iteration for does.

15
Example LET
  • let d document(bars.xml)
  • let beers d/BARS/BEER/_at_name
  • return
  • ltBEERNAMESgt beers lt/BEERNAMESgt
  • Returns one element with all the names of the
    beers, like
  • ltBEERNAMESgtBud Miller lt/BEERNAMESgt

16
Following IDREFs
  • XQuery (but not XPath) allows us to use paths
    that follow attributes that are IDREFs.
  • If x denotes a sequence of one or more IDREFs,
    then x gty denotes all the elements with tag y
    whose IDs are one of these IDREFs.

17
Example
  • Find all the beer elements where the beer is sold
    by Joes Bar for less than 3.00.
  • Strategy
  • beer will for-loop over all beer elements.
  • For each beer, let joe be either the Joes-Bar
    element, if Joe sells the beer, or the empty
    sequence if not.
  • Test whether joe sells the beer for lt 3.00.

18
Example The Query
  • let d document(bars.xml)
  • for beer in d/BARS/BEER
  • let joe beer/_at_soldBygtBAR_at_nameJoesBar
  • let joePrice joe/PRICE_at_theBeerbeer/_at_name
  • where joePrice lt 3.00
  • return ltCHEAPBEERgt beer lt/CHEAPBEERgt

19
Order-By Clauses
  • FLWR is really FLWOR an order-by clause can
    precede the return.
  • Form order by ltexpressiongt
  • With optional ascending or descending.
  • The expression is evaluated for each output
    element.
  • Determines placement in output sequence.

20
Example Order-By
  • List all prices for Bud, lowest first.
  • let d document(bars.xml)
  • for p in d/BARS/BAR/PRICE_at_theBeerBud
  • order by p
  • return p

21
Predicates
  • Normally, conditions imply existential
    quantification.
  • Example /BARS/BAR_at_name means all the bars
    that have a name.
  • Example /BARS/BAR_at_nameJoesBar/PRICE
    /BARS/BAR_at_nameSuesBar/PRICE means Joe and
    Sue have at least one price in common.

22
Other Operators
  • Use Fortran comparison operators to compare
    atomic values only.
  • eq, ne, gt, ge, lt, le.
  • Arithmetic operators , - , , div, idiv, mod.
  • Apply to any expressions that yield arithmetic or
    date/time values.

23
Effective Boolean Values
  • The effective boolean value (EBV) of an
    expression is
  • The actual value if the expression is of type
    boolean.
  • FALSE if the expression evaluates to 0, the
    empty string, or () the empty sequence.
  • TRUE otherwise.

24
EBV Examples
  1. _at_nameJoesBar has EBV TRUE or FALSE, depending
    on whether the name attribute is JoesBar.
  2. /BARS/BAR_at_nameGoldenRail has EBV TRUE if
    some bar is named the Golden Rail, and FALSE if
    there is no such bar.

25
Boolean Operators
  • E1 and E2, E1 or E2, not(E ), if
    (E1) then E2 else E3 apply to any expressions.
  • Take EBVs of the expressions first.
  • Example not(3 eq 5 or 0) has value TRUE.
  • Also true() and false() are functions that
    return values TRUE and FALSE.

26
Quantifier Expressions
  • some x in E1 satisfies E2
  • Evaluate the sequence E1.
  • Let x (any variable) be each item in the
    sequence, and evaluate E2.
  • Return TRUE if E2 has EBV TRUE for at least one
    x.
  • Analogously
  • every x in E1 satisfies E2

27
Document Order
  • Comparison by document order ltlt and gtgt.
  • Example d/BARS/BEER_at_nameBud ltlt
    d/BARS/BEER_at_nameMiller is true iff the Bud
    element appears before the Miller element in the
    document d.

28
Set Operators
  • union, intersect, except operate on sequences of
    nodes.
  • Meanings analogous to SQL.
  • Result eliminates duplicates.
  • Result appears in document order.
Write a Comment
User Comments (0)
About PowerShow.com