XPATH and XQUERY � Two query language to search for features in XML documents XML Query Languages � 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 < RESTS> < !DOCTYPE Rests [ RESTS have 0 or m ore REST & SODA < REST name = “JoesRest”> < !ELEMENT RESTS (REST* , SODA* )> Every REST has 1 or < PRICE theSoda = “Dew”> 1.50< /PRICE> < !ELEMENT REST (PRICE+ )> m ore PRI CE and also < PRICE theSoda = “Slice”> 1.75< /PRICE> < !ATTLIST REST name = ID> an attr nam e < /REST> … < !ELEMENT PRICE (# PCDATA)> PRI CE has data < SODA name = “Dew”, soldBy = “JoesRest, < !ATTLIST PRICE theSoda = IDREF> for price & the Soda w ith that SuesRest,…”> < !ELEMENT SODA ()> price. < /SODA> … < !ATTLIST SODA name = ID, soldBy = IDREFS> < /RESTS> ]> SODA nam e for I D and I DREFs 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 tags, in order. instead of directories and files � 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> … < /SODA> … /RESTS/REST/PRICE describes the //PRICE describes the same PRICE set with these two PRICE objects objects, but only because the DTD < /RESTS> < /RESTS> as well as the PRICE objects for 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> < /REST> … � Example: /* /* /PRICE represents all < SODA name = “Dew”, soldBy = “JoesRest, price objects at the third level of 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> < REST name = “JoesRest”> to tags. < 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, SuesRest,…”> as if they were nested within that tag. < /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 of steps to find a set of nodes at each < REST name = “JoesRest”> step. < PRICE theSoda = “Dew”> 1.50< /PRICE> � At each step, we may follow any one of < PRICE theSoda = “Slice”> 1.75< /PRICE> several axes . < /REST> … � The default axis is child:: --- go to any Now, this PRICE object is selected, along with any child of the current set of nodes. other prices for Slice. 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). 2. descendant-or-self:: = the current axis. Thus, node(s) and all descendants. � /RESTS/SODA[@name = “Dew” ] is � Note: // is really a shorthand for this axis. shorthand for 3. ancestor::, ancestor-or-self, etc. /RESTS/SODA[attribute::name = “Dew”] 19 20 XQUERY � SQL XQUERY � 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 FOR < variable> IN < path expression> ,… 1. One or more FOR and/or LET clauses. � Variables begin with $. 2. Then an optional WHERE clause. � A FOR variable takes on each object in 3. A RETURN clause. 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> ,… � Value of the variable becomes the set RETURN < SODANAME> $soda< /SODANAME> of objects defined by the path � $soda ranges over the name attributes expression. � Note LET does not cause iteration; FOR of all sodas in our example document. � Result is a list of tagged names, like does. < SODANAME> Dew< /SODANAME> < SODANAME> Slice< /SODANAME> … 25 26 Example: LET Following IDREF’s � XQUERY (but not XPATH) allows us to LET $sodas := /RESTS/SODA/@name use paths that follow attributes that are RETURN IDREF’s. < SODANAMES> $sodas< /SODANAMES> � If x denotes a set of IDREF’s, then � Returns one object with all the names of x = > y denotes all the objects with tag the sodas, like: y whose ID’s are one of these IDREF’s. < SODANAMES> Dew, Slice,…< /SODANAMES> 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 name is Joe’s Bar. FOR $soda IN /RESTS/SODA LET $joe := $soda/@soldBy= > REST[@name= “JoesRest”] is sold by Joe’s Rest for less than 1.60. 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- Only pass the values of Find that PRICE subobject Rest object, if Joe sells the soda, or the $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
Recommend
More recommend