XML Query Languages XPATH XQUERY - PowerPoint PPT Presentation

About This Presentation
Title:

XML Query Languages XPATH XQUERY

Description:

forces every PRICE to appear within. a BARS and a BAR. Wild-Card ... List all prices for Export, lowest price first. let $d := document('bars.xml') for $p in ... – PowerPoint PPT presentation

Number of Views:146
Avg rating:3.0/5.0
Slides: 35
Provided by: jeff465
Category:
Tags: xml | xpath | xquery | languages | query

less

Transcript and Presenter's Notes

Title: XML Query Languages XPATH XQUERY


1
XML Query LanguagesXPATHXQUERY
Zaki Malik November 11, 2008
2
The XPath/XQuery Data Model
  • Corresponding to the fundamental relation of
    the relational model is sequence of items.
  • An item is either
  • A primitive value, e.g., integer or string.
  • A node.

3
Principal Kinds of Nodes
  1. Document nodes represent entire documents.
  2. Elements are pieces of a document consisting of
    some opening tag, its matching closing tag (if
    any), and everything in between.
  3. Attributes are names that are given values
    inside opening tags.

4
Document Nodes
  • Formed by doc(URL) or document(URL) (or
    doc(filename) or document(filename)
  • Example doc(/usr/class/cs145/bars.xml)
  • All XPath (and XQuery) queries refer to a doc
    node, either explicitly or implicitly.

5
Example DTD
  • lt!DOCTYPE Bars
  • lt!ELEMENT BARS (BAR, BEER)gt
  • lt!ELEMENT BAR (PRICE)gt
  • lt!ATTLIST BAR name IDgt
  • lt!ELEMENT PRICE (PCDATA)gt
  • lt!ATTLIST PRICE theBeer IDREFgt
  • lt!ELEMENT BEER ()gt
  • lt!ATTLIST BEER name ID, soldBy IDREFSgt
  • gt

6
Example Document
  • ltBARSgt
  • ltBAR name JoesBargt
  • ltPRICE theBeer Exportgt2.50lt/PRICEgt
  • ltPRICE theBeer Gr.Is.gt3.00lt/PRICEgt
  • lt/BARgt
  • ltBEER name Export soldBy JoesBar
  • SuesBar /gt
  • lt/BARSgt

Document node is all of this, plus the header (
lt? xml version ).
7
Nodes as Semistructured Data
bars.xml
BARS
name JoesBar
SoldBy
name Export
BEER
BAR
theBeer Gr.Is.
theBeer Export
PRICE
PRICE
Blue document Green element Orange
attribute Purple primitive value
3.00
2.50
8
XPATH and XQUERY
  • XPATH is a language for describing paths in XML
    documents.
  • Really think of the semi-structured data graph
    and its paths.
  • The result of the described path is a sequence of
    items.
  • Compare with SQL
  • SQL is a language for describing relations in
    terms of other relations.
  • The result of a query is a relation (bag) made up
    of tuples
  • XQUERY is a full query language for XML documents
    with power similar to SQL.

9
Path Descriptors
  • Simple path descriptors are sequences of tags
    separated by slashes (/).
  • The format used is strongly reminiscent of UNIX
    naming conventions.
  • Construct the result by starting with just the
    doc node and processing each tag from the left.
  • If the descriptor begins with /, then the path
    starts at the root and has those tags, in order.
  • If the descriptor begins with //, then the path
    can start anywhere.

10
Example /BARS/BAR/PRICE
  • ltBARSgt
  • ltBAR name JoesBargt
  • ltPRICE theBeer Budgt2.50lt/PRICEgt
  • ltPRICE theBeer Millergt3.00lt/PRICEgt
  • lt/BARgt
  • ltBEER name Bud, soldBy JoesBar,
  • SuesBar,gt
  • lt/BEERgt
  • lt/BARSgt

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

12
Wild-Card
  • A star () in place of a tag represents any one
    tag.
  • Example ///PRICE represents all price objects
    at the third level of nesting.

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

14
Attributes
  • In XPATH, we refer to attributes by prepending _at_
    to their name.
  • Attributes of a tag may appear in paths as if
    they were nested within that tag.

15
Example /BARS//_at_name
  • ltBARSgt
  • ltBAR name JoesBargt
  • ltPRICE theBeer Budgt2.50lt/PRICEgt
  • ltPRICE theBeer Millergt3.00lt/PRICEgt
  • lt/BARgt
  • ltBEER name Bud, soldBy JoesBar,
  • SuesBar,gt
  • lt/BEERgt
  • lt/BARSgt

16
Selection Conditions
  • A condition inside may follow a tag.
  • If so, then only paths that have that tag and
    also satisfy the condition are included in the
    result of a path expression.

17
Example Selection Condition
  • /BARS/BAR/PRICEPRICE lt 2.75
  • ltBARSgt
  • ltBAR name JoesBargt
  • ltPRICE theBeer Budgt2.50lt/PRICEgt
  • ltPRICE theBeer Millergt3.00lt/PRICEgt
  • lt/BARgt

18
Example Attribute in Selection
  • /BARS/BAR/PRICE_at_theBeer Miller
  • ltBARSgt
  • ltBAR name JoesBargt
  • ltPRICE theBeer Budgt2.50lt/PRICEgt
  • ltPRICE theBeer Millergt3.00lt/PRICEgt
  • lt/BARgt

19
Axes
  • In general, path expressions allow us to start at
    the root and execute a sequence of steps to find
    a set of nodes at each step.
  • At each step, we may follow any one of several
    axes.
  • The default axis is child --- go to any child
    of the current set of nodes.

20
Example Axes
  • /BARS/BEER is really shorthand for
    /BARS/childBEER .
  • _at_ is really shorthand for the attribute axis.
  • Thus, /BARS/BEER_at_name Bud is shorthand for
  • /BARS/BEERattributename Bud

21
More Axes
  • Some other useful axes are
  • parent parent(s) of the current node(s).
  • descendant-or-self the current node(s) and
    all descendants.
  • Note // is really a shorthand for this axis.
  • ancestor, ancestor-or-self, etc.

22
XQuery
  • XQuery extends XPath to a query language that has
    power similar to SQL.
  • Uses the same sequence-of-items data model as
    XPath.
  • XQuery is an expression language.
  • Like relational algebra --- any XQuery expression
    can be an argument of any other XQuery expression.

23
FLWR Expressions
  • The most important form of XQuery expressions
    involves for-, let-, where-, return- (FLWR)
    clauses.
  • A qurey begins with one or more for and/or let
    clauses.
  • The fors and lets can be interspersed.
  • Then an optional where clause.
  • A single return clause.
  • Form
  • for variable in expression
  • let variable expression
  • where condition
  • return expression

24
Semantics of FLWR Expressions
  • Each for creates a loop.
  • let produces only a local variable assignment.
  • 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.
  • So return can be thought of as add to result

25
FOR Clauses
  • FOR ltvariablegt IN ltpath expressiongt,
  • Variables begin with .
  • A FOR variable takes on each object in the set
    denoted by the path expression, in turn.
  • Whatever follows this FOR is executed once for
    each value of the variable.

26
Example FOR
  • FOR beer IN /BARS/BEER/_at_name
  • RETURN ltBEERNAMEgtbeerlt/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

27
LET Clauses
  • LET ltvariablegt ltpath expressiongt,
  • Value of the variable becomes the set of objects
    defined by the path expression.
  • Note LET does not cause iteration FOR does.

28
Example LET
  • LET beers /BARS/BEER/_at_name
  • RETURN ltBEERNAMESgtbeerslt/BEERNAMESgt
  • Returns one object with all the names of the
    beers, like
  • ltBEERNAMESgtBud, Miller,lt/BEERNAMESgt

29
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 assignment
    to variables.
  • Determines placement in output sequence.

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

31
Following IDREFs
  • XQUERY (but not XPATH) allows us to use paths
    that follow attributes that are IDREFs.
  • If x denotes a set of IDREFs, then x gty
    denotes all the objects with tag y whose IDs
    are one of these IDREFs.

32
Example
  • Find all the beer objects where the beer is sold
    by Joes Bar for less than 3.00.
  • Strategy
  • beer will for-loop over all beer objects.
  • For each beer, let joe be either the Joes-Bar
    object, if Joe sells the beer, or the empty set
    of bar objects.
  • Test whether joe sells the beer for lt 3.00.

33
Example The Query
  • FOR beer IN /BARS/BEER
  • LET joe beer/_at_soldBygtBAR_at_nameJoesBar
  • LET joePrice joe/PRICE_at_theBeerbeer/_at_name
  • WHERE joePrice lt 3.00
  • RETURN ltCHEAPBEERgtbeerlt/CHEAPBEERgt

34
Aggregations
  • XQuery allows the usual aggregations, such as
    sum, count, max, min.
  • They take any sequence as argument.
  • E.g. find bars where all beers are under 5.
  • let bars doc(bars.xml)/BARS
  • for price in bars/BAR/PRICE
  • where max(price) lt 5
  • return bar/BAR/_at_name
Write a Comment
User Comments (0)
About PowerShow.com