Presentation is loading. Please wait.

Presentation is loading. Please wait.

XML Query Languages XPATH XQUERY Zaki Malik November 11, 2008.

Similar presentations


Presentation on theme: "XML Query Languages XPATH XQUERY Zaki Malik November 11, 2008."— Presentation transcript:

1 XML Query Languages XPATH XQUERY Zaki Malik November 11, 2008

2 The XPath/XQuery Data Model uCorresponding to the fundamental “relation” of the relational model is: sequence of items. uAn item is either: 1.A primitive value, e.g., integer or string. 2.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 uFormed by doc(URL) or document(URL) (or doc(filename) or document(filename) uExample: doc(“/usr/class/cs145/bars.xml”) uAll XPath (and XQuery) queries refer to a doc node, either explicitly or implicitly.

5 Example DTD <!DOCTYPE Bars [ ]>

6 Example Document 2.50 3.00 … <BEER name = ”Export” soldBy = ”JoesBar SuesBar … ”/> … An element node An attribute node Document node is all of this, plus the header ( <? xml version… ).

7 Nodes as Semistructured Data BARS PRICE BEERBAR name = ”JoesBar” theBeer = ”Gr.Is.” theBeer = ”Export” SoldBy = ”…” name = ”Export” 3.002.50 Blue =document Green = element Orange = attribute Purple = primitive value bars.xml

8 XPATH and XQUERY uXPATH is a language for describing paths in XML documents. wReally think of the semi-structured data graph and its paths. wThe result of the described path is a sequence of items. wCompare 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 uXQUERY is a full query language for XML documents with power similar to SQL.

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

10 Example: /BARS/BAR/PRICE 2.50 3.00 … <BEER name = “Bud”, soldBy = “JoesBar, SuesBar,…”> … /BARS/BAR/PRICE describes the set with these two PRICE objects as well as the PRICE objects for any other bars.

11 Example: //PRICE 2.50 3.00 … <BEER name = “Bud”, soldBy = “JoesBar, SuesBar,…”> … //PRICE describes the same PRICE objects, but only because the DTD forces every PRICE to appear within a BARS and a BAR.

12 Wild-Card * uA star (*) in place of a tag represents any one tag. uExample: /*/*/PRICE represents all price objects at the third level of nesting.

13 Example: /BARS/* 2.50 3.00 … <BEER name = “Bud”, soldBy = “JoesBar, SuesBar,…”> … /BARS/* captures all BAR and BEER objects, such as these.

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

15 Example: /BARS/*/@name 2.50 3.00 … <BEER name = “Bud”, soldBy = “JoesBar, SuesBar,…”> … /BARS/*/@name selects all name attributes of immediate subobjects of the BARS object.

16 Selection Conditions uA condition inside […] may follow a tag. uIf 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 u/BARS/BAR/PRICE[PRICE < 2.75] 2.50 3.00 … The condition that the PRICE be < $2.75 makes this price, but not the Miller price

18 Example: Attribute in Selection u/BARS/BAR/PRICE[@theBeer = “Miller”] 2.50 3.00 … Now, this PRICE object is selected, along with any other prices for Miller.

19 Axes uIn 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. uAt each step, we may follow any one of several axes. uThe default axis is child:: --- go to any child of the current set of nodes.

20 Example: Axes u/BARS/BEER is really shorthand for /BARS/child::BEER. u@ is really shorthand for the attribute:: axis. wThus, /BARS/BEER[@name = “Bud” ] is shorthand for /BARS/BEER[attribute::name = “Bud”]

21 More Axes uSome other useful axes are: 1.parent:: = parent(s) of the current node(s). 2.descendant-or-self:: = the current node(s) and all descendants. wNote: // is really a shorthand for this axis. 3.ancestor::, ancestor-or-self, etc.

22 XQuery uXQuery extends XPath to a query language that has power similar to SQL. uUses the same sequence-of-items data model as XPath. uXQuery is an expression language. wLike 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. 1. A qurey begins with one or more for and/or let clauses. The for’s and let’s can be interspersed. 2. Then an optional where clause. 3. A single return clause. Form: for variable in expression let variable := expression where condition return expression

24 Semantics of FLWR Expressions uEach for creates a loop. wlet produces only a local variable assignment. uAt each iteration of the nested loops, if any, evaluate the where clause. uIf the where clause returns TRUE, invoke the return clause, and append its value to the output. wSo return can be thought of as “add to result”

25 FOR Clauses FOR IN,… uVariables begin with $. uA FOR variable takes on each object in the set denoted by the path expression, in turn. uWhatever follows this FOR is executed once for each value of the variable.

26 Example: FOR FOR $beer IN /BARS/BEER/@name RETURN $beer u$beer ranges over the name attributes of all beers in our example document. uResult is a list of tagged names, like Bud Miller …

27 LET Clauses LET :=,… uValue of the variable becomes the set of objects defined by the path expression. uNote LET does not cause iteration; FOR does.

28 Example: LET LET $beers := /BARS/BEER/@name RETURN $beers uReturns one object with all the names of the beers, like: Bud, Miller,…

29 Order-By Clauses FLWR is really FLWOR: an order-by clause can precede the return. Form: order by 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[@theBeer=”Export”] order by $p return $p Generates bindings for $p to PRICE elements. Order those bindings by the values inside the elements. Each binding is evaluated for the output. The result is a sequence of PRICE elements.

31 Following IDREF’s uXQUERY (but not XPATH) allows us to use paths that follow attributes that are IDREF’s. uIf x denotes a set of IDREF’s, then x =>y denotes all the objects with tag y whose ID’s are one of these IDREF’s.

32 Example uFind all the beer objects where the beer is sold by Joe’s Bar for less than 3.00. uStrategy: 1.$beer will for-loop over all beer objects. 2.For each $beer, let $joe be either the Joe’s- Bar object, if Joe sells the beer, or the empty set of bar objects. 3.Test whether $joe sells the beer for < 3.00.

33 Example: The Query FOR $beer IN /BARS/BEER LET $joe := $beer/@soldBy=>BAR[@name=“JoesBar”] LET $joePrice := $joe/PRICE[@theBeer=$beer/@name] WHERE $joePrice < 3.00 RETURN $beer Attribute soldBy is of type IDREFS. Follow each ref to a BAR and check if its name is Joe’s Bar. Find that PRICE subobject of the Joe’s Bar object that represents whatever beer is currently $beer. Only pass the values of $beer, $joe, $joePrice to the RETURN clause if the string inside the PRICE object $joePrice is < 3.00

34 Aggregations uXQuery allows the usual aggregations, such as sum, count, max, min. uThey take any sequence as argument. uE.g. find bars where all beers are under $5. let $bars = doc(”bars.xml”)/BARS for $price in $bars/BAR/PRICE where max($price) < 5 return $bar/BAR/@name


Download ppt "XML Query Languages XPATH XQUERY Zaki Malik November 11, 2008."

Similar presentations


Ads by Google