Outline • XML Query Languages CS 235: – XPATH – XQUERY Introduction to Databases Svetlozar Nestorov Lecture Notes #26 XPATH and XQUERY Example DTD • XPATH is a language for describing paths <!DOCTYPE Bars [ <!ELEMENT BARS (BAR*, BEER*)> in XML documents. <!ELEMENT BAR (PRICE+)> – Really think of the semistructured data graph <!ATTLIST BAR name = ID> and its paths. <!ELEMENT PRICE (#PCDATA)> • XQUERY is a full query language for XML <!ATTLIST PRICE theBeer = IDREF> documents. <!ELEMENT BEER ()> <!ATTLIST BEER name = ID, soldBy = IDREFS> ]> Example Document Path Descriptors • Simple path descriptors are sequences of <BARS> tags separated by slashes (/). <BAR name = “JoesBar”> • If the descriptor begins with /, then the <PRICE theBeer = “Bud”>2.50</PRICE> path starts at the root and has those tags, <PRICE theBeer = “Miller”>3.00</PRICE> in order. </BAR> … • If the descriptor begins with //, then the <BEER name = “Bud”, soldBy = “JoesBar, path can start anywhere. SuesBar,…”> </BEER> … </BARS> 1
Example: /BARS/BAR/PRICE Example: //PRICE <BARS> <BARS> <BAR name = “JoesBar”> <BAR name = “JoesBar”> <PRICE theBeer = “Bud”>2.50</PRICE> <PRICE theBeer = “Bud”>2.50</PRICE> <PRICE theBeer = “Miller”>3.00</PRICE> <PRICE theBeer = “Miller”>3.00</PRICE> </BAR> … </BAR> … <BEER name = “Bud”, soldBy = “JoesBar, <BEER name = “Bud”, soldBy = “JoesBar, SuesBar,…”> SuesBar,…”> /BARS/BAR/PRICE describes the //PRICE describes the same PRICE </BEER> … </BEER> … set with these two PRICE objects objects, but only because the DTD as well as the PRICE objects for forces every PRICE to appear within </BARS> </BARS> any other bars. a BARS and a BAR. Wild-Card * Example: /BARS/* • A star (*) in place of a tag represents any <BARS> one tag. <BAR name = “JoesBar”> • Example: /*/*/PRICE represents all price <PRICE theBeer = “Bud”>2.50</PRICE> objects at the third level of nesting. <PRICE theBeer = “Miller”>3.00</PRICE> </BAR> … <BEER name = “Bud”, soldBy = “JoesBar, SuesBar,…”> </BEER> … /BARS/* captures all BAR </BARS> and BEER objects, such as these. Attributes Example: /BARS/*/@name • In XPATH, we refer to attributes by <BARS> prepending @ to their name. <BAR name = “JoesBar”> • Attributes of a tag may appear in paths as <PRICE theBeer = “Bud”>2.50</PRICE> if they were nested within that tag. <PRICE theBeer = “Miller”>3.00</PRICE> </BAR> … <BEER name = “Bud”, soldBy = “JoesBar, SuesBar,…”> /BARS/*/@name selects all </BEER> … name attributes of immediate </BARS> subobjects of the BARS object. 2
Selection Conditions Example: Selection Condition • A condition inside […] may follow a tag. • /BARS/BAR/PRICE[PRICE < 2.75] • If so, then only paths that have that tag <BARS> and also satisfy the condition are included <BAR name = “JoesBar”> in the result of a path expression. <PRICE theBeer = “Bud”>2.50</PRICE> <PRICE theBeer = “Miller”>3.00</PRICE> </BAR> … The condition that the PRICE be < $2.75 makes this price but not the Miller price satisfy the path descriptor. Axes Example: Attribute in Selection • /BARS/BAR/PRICE[@theBeer = “Miller”] • In general, path expressions allow us to start at the root and execute a sequence <BARS> of steps to find a set of nodes at each <BAR name = “JoesBar”> step. <PRICE theBeer = “Bud”>2.50</PRICE> • At each step, we may follow any one of <PRICE theBeer = “Miller”>3.00</PRICE> several axes . </BAR> … Now, this PRICE object is • The default axis is child:: --- go to any selected, along with any child of the current set of nodes. other prices for Miller. Example: Axes More Axes • /BARS/BEER is really shorthand for • Some other useful axes are: /BARS/child::BEER . 1. parent:: = parent(s) of the current node(s). • @ is really shorthand for the attribute:: 2. descendant-or-self:: = the current node(s) and all descendants. axis. Note: // is really a shorthand for this axis. � – Thus, /BARS/BEER[@name = “Bud” ] is 3. ancestor::, ancestor-or-self, etc. shorthand for /BARS/BEER[attribute::name = “Bud”] 3
XQUERY FLWR Expressions • XQUERY allows us to query XML 1. One or more FOR and/or LET clauses. documents, using path expressions from 2. Then an optional WHERE clause. XPATH to describe important sets. 3. A RETURN clause. • Corresponding to SQL’s select-from-where is the XQUERY FLWR expression , standing for “for-let-where-return.” FOR Clauses Example: FOR FOR <variable> IN <path expression>,… FOR $beer IN /BARS/BEER/@name • Variables begin with $. RETURN • A FOR variable takes on each object in the <BEERNAME>$beer</BEERNAME> set denoted by the path expression, in • $beer ranges over the name attributes turn. of all beers in our example document. • Whatever follows this FOR is executed • Result is a list of tagged names, like once for each value of the variable. <BEERNAME>Bud</BEERNAME> <BEERNAME>Miller</BEERNAME>… LET Clauses Example: LET LET <variable> := <path expression>,… LET $beers := /BARS/BEER/@name • Value of the variable becomes the set of RETURN objects defined by the path expression. <BEERNAMES>$beers</BEERNAMES> • Note LET does not cause iteration; FOR • Returns one object with all the names of does. the beers, like: <BEERNAMES>Bud, Miller,…</BEERNAMES> 4
Following IDREF’s Example • XQUERY (but not XPATH) allows us to use • Find all the beer objects where the beer paths that follow attributes that are is sold by Joe’s Bar for less than 3.00. IDREF’s. • Strategy: • If x denotes a set of IDREF’s, then 1. $beer will for-loop over all beer objects. x => y denotes all the objects with tag y 2. For each $beer, let $joe be either the Joe’s- whose ID’s are one of these IDREF’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. Example: The Query Attribute soldBy is of type IDREFS. Follow each ref to a BAR and check if its FOR $beer IN /BARS/BEER name is Joe’s Bar. LET $joe := $beer/@soldBy=>BAR[@name=“JoesBar”] LET $joePrice := $joe/PRICE[@theBeer=$beer/@name] WHERE $joePrice < 3.00 RETURN <CHEAPBEER>$beer</CHEAPBEER> Only pass the values of Find that PRICE subobject $beer, $joe, $joePrice to of the Joe’s Bar object that the RETURN clause if the represents whatever beer is string inside the PRICE currently $beer. object $joePrice is < 3.00 5
Recommend
More recommend