XPATH and XQUERY

XPATH and XQUERY

XPATH and XQUERY Two query language to search for XML Query Languages features in XML documents XPATH XQUERY XPATH XQUERY 1 2 XPATH XQUERY XPATH is a language for describing XQUERY is a full query language for paths in XML documents. XML documents with power similar to To be more precise it describes OQL. semistructured data graph and its paths. XML documents documents can be described as semi-structured data graphs • Each subobject as a node of a graph with its subobjects as its children 3 4 Example DTD Example Document <!DOCTYPE Rests [ RESTS have 0 or more REST & SODA <RESTS> <!ELEMENT RESTS (REST*, SODA*)> <REST name = “JoesRest”> Every REST has 1 or <PRICE theSoda =“Dew”>1.50</PRICE> <!ELEMENT REST (PRICE+)> more PRICE and also <!ATTLIST REST name = ID> an attr name <PRICE theSoda = “Slice”>1.75</PRICE> <!ELEMENT PRICE (#PCDATA)> </REST> … PRICE has data <!ATTLIST PRICE theSoda = IDREF> for price & the <SODA name = “Dew”, soldBy = “JoesRest, Soda with that <!ELEMENT SODA ()> price. SuesRest,…”> <!ATTLIST SODA name = ID, soldBy = IDREFS> </SODA> … ]> SODA name for ID and IDREFs </RESTS> for the rest that sell the soda. 5 6 1 XPATH Path Descriptors XPATH Path Descriptors Queries are really path descriptors If the descriptor begins with /, then the Look like UNIX path description with tags path starts at the root and has those instead of directories and files tags, in order. Tags are separated by / If the descriptor begins with //, then Simple path descriptors are sequences the path can start anywhere. of tags separated by slashes (/). 7 8 Example: /RESTS/REST/PRICE Example: //PRICE <RESTS> <RESTS> <REST name = “JoesRest”> <REST name = “JoesRest”> <PRICE theSoda =“Dew”>1.50</PRICE> <PRICE theSoda =“Dew”>1.50</PRICE> <PRICE theSoda = “Slice”>1.75</PRICE> <PRICE theSoda = “Slice”>1.75</PRICE> </REST> … </REST> … <SODA name = “Dew”, soldBy = “JoesRest, <SODA name = “Dew”, soldBy = “JoesRest, SuesRest,…”> SuesRest,…”> </SODA> … /RESTS/REST/PRICE describes the </SODA> … //PRICE describes the same PRICE set with these two PRICE objects objects, but only because the DTD </RESTS> as well as the PRICE objects for </RESTS> forces every PRICE to appear within any other bars. a RESTS and a REST. 9 10 Wild-Card * Example: /RESTS/* <RESTS> A star (*) in place of a tag represents <REST name = “JoesRest”> any one tag. <PRICE theSoda =“Dew”>1.50</PRICE> Acts as a “wildcard” <PRICE theSoda = “Slice”>1.75</PRICE> Example: /*/*/PRICE represents all </REST> … price objects at the third level of <SODA name = “Dew”, soldBy = “JoesRest, SuesRest,…”> nesting. </SODA> … /RESTS/* captures all REST </RESTS> and SODA objects, such as these. 11 12 2 Attributes Example: /RESTS/*/@name We may refer to attributes in addition <RESTS> to tags. <REST name = “JoesRest”> <PRICE theSoda =“Dew”>1.50</PRICE> In XPATH, we refer to attributes by <PRICE theSoda = “Slice”>1.75</PRICE> prepending @ to their name. </REST> … Attributes of a tag may appear in paths <SODA name = “Dew”, soldBy = “JoesRest, as if they were nested within that tag. SuesRest,…”> </SODA> … /RESTS/*/@name selects all name attributes of immediate </RESTS> subobjects of the RESTS object. 13 14 Selection Conditions Example: Selection Condition A condition inside […] may follow a tag. /RESTS/REST/PRICE[PRICE < 1.60] If so, the only paths included in the <RESTS> result of a path expression are ones <REST name = “JoesRest”> that <PRICE theSoda = “Dew”>1.50</PRICE> have that tag and <PRICE theSoda = “Slice”>1.75</PRICE> also satisfy the condition </REST> … The condition that the PRICE be < $1.60 makes this price but not the Slice price satisfy the path descriptor. 15 16 Example: Attribute in Selection Axes /RESTS/REST/PRICE[@theSoda = “Slice”] In general, path expressions allow us to <RESTS> start at the root and execute a sequence <REST name = “JoesRest”> of steps to find a set of nodes at each <PRICE theSoda = “Dew”>1.50</PRICE> step. <PRICE theSoda = “Slice”>1.75</PRICE> At each step, we may follow any one of </REST> … several axes. Now, this PRICE object is The default axis is child:: --- go to any selected, along with any other prices for Slice. child of the current set of nodes. 17 18 3 Example: Axes More Axes /RESTS/SODA is really shorthand for Some other useful axes are: /RESTS/child::SODA . 1. parent:: = parent(s) of the current @ is really shorthand for the attribute:: node(s). axis. Thus, 2. descendant-or-self:: = the current node(s) and all descendants. /RESTS/SODA[@name = “Dew” ] is shorthand for Note: // is really a shorthand for this axis. 3. ancestor::, ancestor-or-self, etc. /RESTS/SODA[attribute::name = “Dew”] 19 20 XQUERY XQUERY Ù SQL XQUERY allows us to query XML where ÙWHERE documents, using path expressions return ÙSELECT from XPATH to describe important sets. for Ù FROM Corresponding to SQL’s select-from- where is the XQUERY FLWR (pronounced “flower”) expression, standing for “for-let-where-return.” 21 22 FLWR Expressions FOR Clauses 1. One or more FOR and/or LET clauses. FOR <variable> IN <path expression>,… 2. Then an optional WHERE clause. Variables begin with $. 3. A RETURN clause. 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. Creates a loop 23 24 4 Example: FOR LET Clauses FOR $soda IN /RESTS/SODA/@name LET <variable> := <path expression>,… RETURN Value of the variable becomes the set <SODANAME>$soda</SODANAME> of objects defined by the path $soda ranges over the name attributes expression. of all sodas in our example document. Note LET does not cause iteration; FOR Result is a list of tagged names, like does. <SODANAME>Dew</SODANAME> <SODANAME>Slice</SODANAME>… 25 26 Example: LET Following IDREF’s LET $sodas := /RESTS/SODA/@name XQUERY (but not XPATH) allows us to RETURN use paths that follow attributes that are <SODANAMES>$sodas</SODANAMES> IDREF’s. Returns one object with all the names of If x denotes a set of IDREF’s, then the sodas, like: x =>y denotes all the objects with tag <SODANAMES>Dew, Slice,…</SODANAMES> y whose ID’s are one of these IDREF’s. 27 28 Example: The Query Example Attribute soldBy is of type IDREFS. Follow each ref to a REST and check if its Find all the soda objects where the soda FOR $soda IN /RESTS/SODA name is Joe’s Bar. is sold by Joe’s Rest for less than 1.60. LET $joe := $soda/@soldBy=>REST[@name=“JoesRest”] LET $joePrice := $joe/PRICE[@theSoda=$soda/@name] Strategy: WHERE $joePrice < 1.60 1. $soda will for-loop over all soda objects. RETURN <CHEAPSODA>$soda</CHEAPSODA> 2. For each $soda, let $joe be either the Joe’s- Rest object, if Joe sells the soda, or the Only pass the values of Find that PRICE subobject $soda, $joe, $joePrice to of the Joe’s Bar object that empty set of rest objects. the RETURN clause if the represents whatever soda is string inside the PRICE currently $soda. 3. Test whether $joe sells the soda for < 1.60. object $joePrice is < 1. 60 29 30 5.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    5 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us