XML Query Languages Salman Azhar XPATH XQUERY These
- Slides: 31
XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from Elmasri-Navathe’s Fundamentals of Database Systems and Molina-Ullman-Widom’s Database Systems 2/10/05 Salman Azhar: Database Systems 1
XPATH and XQUERY n Two query language to search in XML documents n XPATH n n XQUERY n 2/10/05 Language for describing paths Language for querying XML documents Salman Azhar: Database Systems 2
XPATH n A language for describing paths in XML documents n n More precisely XPATH describes semistructured data graph and its paths XML documents can be described as semistructured data graphs n n 2/10/05 each subobject as a node of a graph with its subobjects as its children Salman Azhar: Database Systems 3
XQUERY n XQUERY is a full query language for XML documents n n using path expressions from XPATH XQUERY use FLWR (pronounced “flower”) expression n n 2/10/05 corresponds to SQL’s select-from-where FLWR stands for “for-let-where-return” Salman Azhar: Database Systems 4
Example DTD <!DOCTYPE Rests [ RESTS have 0 or more REST & SODA <!ELEMENT RESTS (REST*, SODA*)> Every REST has 1 or <!ELEMENT REST (PRICE+)> more PRICE and also <!ATTLIST REST name = ID> an attribute name PRICE has <!ELEMENT PRICE (#PCDATA)> data for price <!ATTLIST PRICE the. Soda = IDREF> & the Soda with that <!ELEMENT SODA ()> price <!ATTLIST SODA name = ID, sold. By = IDREFS> SODA name for ID and IDREFs ]> for the rest that sell the soda. 2/10/05 Salman Azhar: Database Systems 5
Example Document <RESTS> <REST name = “Joes. Rest”> <PRICE the. Soda =“Dew”>1. 50</PRICE> <PRICE the. Soda = “Slice”>1. 75</PRICE> </REST> … <SODA name = “Dew”, sold. By = “Joes. Rest, Sues. Rest, …”> </SODA> … </RESTS> 2/10/05 Salman Azhar: Database Systems 6
XPATH Path Descriptors n n Let us study XPATH first Queries are really path descriptors n Look like UNIX path description n with tags instead of directories and files Tags are separated by / Simple path descriptors are n 2/10/05 sequences of tags separated by slashes (/) Salman Azhar: Database Systems 7
Example: /RESTS/REST/PRICE <RESTS> <REST name = “Joes. Rest”> <PRICE the. Soda =“Dew”>1. 50</PRICE> <PRICE the. Soda = “Slice”>1. 75</PRICE> </REST> … <SODA name = “Dew”, sold. By = “Joes. Rest, Sues. Rest, …”> /RESTS/REST/PRICE describes </SODA> … The set with these two PRICE </RESTS> Objects as well as the PRICE objects for any other restaurants. 2/10/05 Salman Azhar: Database Systems 8
XPATH Path Descriptors n If the descriptor begins with /, n n the path starts at the root and has those tags, in order If the descriptor begins with //, n 2/10/05 then the path can start anywhere Salman Azhar: Database Systems 9
Example: //PRICE <RESTS> <REST name = “Joes. Rest”> <PRICE the. Soda =“Dew”>1. 50</PRICE> <PRICE the. Soda = “Slice”>1. 75</PRICE> </REST> … <SODA name = “Dew”, sold. By = “Joes. Rest, Sues. Rest, …”> //PRICE describes the same PRICE </SODA> … objects, but only because the DTD </RESTS> forces every PRICE to appear within a RESTS and a REST. 2/10/05 Salman Azhar: Database Systems 10
Wild-Card * n A star (*) in place of a tag represents any one tag n n acts as a “wildcard” Example: /*/*/PRICE represents n 2/10/05 all price objects at the third level of nesting Salman Azhar: Database Systems 11
Example: /RESTS/* <RESTS> <REST name = “Joes. Rest”> <PRICE the. Soda =“Dew”>1. 50</PRICE> <PRICE the. Soda = “Slice”>1. 75</PRICE> </REST> … <SODA name = “Dew”, sold. By = “Joes. Rest, Sues. Rest, …”> </SODA> … /RESTS/* captures all REST </RESTS> and SODA objects, such as these. 2/10/05 Salman Azhar: Database Systems 12
Attributes n We may refer to attributes in addition to tags n n In XPATH, we refer to attributes by prepending @ to their name Attributes of a tag may appear in paths n 2/10/05 as if they were nested within that tag Salman Azhar: Database Systems 13
Example: /RESTS/*/@name <RESTS> <REST name = “Joes. Rest”> <PRICE the. Soda =“Dew”>1. 50</PRICE> <PRICE the. Soda = “Slice”>1. 75</PRICE> </REST> … <SODA name = “Dew”, sold. By = “Joes. Rest, Sues. Rest, …”> /RESTS/*/@name selects all </SODA> … name attributes of immediate </RESTS> subobjects of the RESTS object. 2/10/05 Salman Azhar: Database Systems 14
Selection Conditions n n A condition inside […] may follow a tag If so, the only paths included in the result of a path expression are ones that n n 2/10/05 have that tag and also satisfy the condition Salman Azhar: Database Systems 15
Example: Selection Condition n /RESTS/REST/PRICE[PRICE < 1. 60] <RESTS> <REST name = “Joes. Rest”> <PRICE the. Soda = “Dew”>1. 50</PRICE> <PRICE the. Soda = “Slice”>1. 75</PRICE> </REST> … The condition that the PRICE be < $1. 60 makes this price but not the price of Slice satisfy the path descriptor. 2/10/05 Salman Azhar: Database Systems 16
Example: Attribute in Selection /RESTS/REST/PRICE[@the. Soda = “Slice”] <RESTS> <REST name = “Joes. Rest”> <PRICE the. Soda = “Dew”>1. 50</PRICE> <PRICE the. Soda = “Slice”>1. 75</PRICE> </REST> … n Now, this PRICE object is selected, along with any other prices for Slice. 2/10/05 Salman Azhar: Database Systems 17
Optional: Axes n In general, path expressions allow us n n to start at the root and execute a sequence of steps n n to find a set of nodes at each step, we may follow any one of several axes n The default axis is child: : n go to any child of the current set of nodes 2/10/05 Salman Azhar: Database Systems 18
Optional: Example of Axes n n /RESTS/SODA is really shorthand for /RESTS/child: : SODA @ is really shorthand for the attribute: : axis. Thus, n 2/10/05 /RESTS/SODA[@name = “Dew” ] is shorthand for /RESTS/SODA[attribute: : name = “Dew”] Salman Azhar: Database Systems 19
Optional: More Axes Some other useful axes are: n 1. 2. parent: : = parent(s) of the current node(s) descendant-or-self: : = the current node(s) and all descendants w 3. 2/10/05 Note: // is really a shorthand for this axis. ancestor: : , ancestor-or-self, etc. Salman Azhar: Database Systems 20
XQUERY n XQUERY allows us to query XML documents n n using path expressions from XPATH to describe important sets XQUERY use FLWR (pronounced “flower”) expressions n n 2/10/05 stands for “for-let-where-return” corresponds to SQL’s select-from-where Salman Azhar: Database Systems 21
XQUERY SQL Mapping n n n where WHERE return SELECT for/let FROM 2/10/05 Salman Azhar: Database Systems 22
FLWR (FLo. We. R) Expressions FLWR expressions are made up of n 1. 2. 3. 2/10/05 One or more FOR and/or LET clauses. Then an optional WHERE clause. A RETURN clause. Salman Azhar: Database Systems 23
FOR Clauses FOR <variable> IN <path expression>, … n Variables begin with $. n A FOR variable takes on n n each object in the set denoted by the path expression (in turn) Whatever follows this FOR is n n 2/10/05 executed once for each value of the variable creates a loop Salman Azhar: Database Systems 24
Example: FOR $soda IN /RESTS/SODA/@name RETURN <SODANAME>$soda</SODANAME> n $soda ranges over the name attributes of all sodas in our example document n Result is a list of tagged names, like <SODANAME>Dew</SODANAME> <SODANAME>Slice</SODANAME>… 2/10/05 Salman Azhar: Database Systems 25
LET Clauses LET <variable> : = <path expression>, … n Value of the variable becomes the set of objects defined by the path expression n Note: n n 2/10/05 LET does not cause iteration FOR does cause iteration Salman Azhar: Database Systems 26
Example: LET $sodas : = /RESTS/SODA/@name RETURN <SODANAMES>$sodas</SODANAMES> n Returns one object with all the names of the sodas, like: n <SODANAMES>Dew, Slice, …</SODANAMES> 2/10/05 Salman Azhar: Database Systems 27
Following IDREFs n XQUERY (but not XPATH) allows us n n to use paths that follow attributes that are IDREFs If x denotes a set of IDREFs, n 2/10/05 then x =>y denotes all the objects with tag y whose IDs are one of these IDREFs Salman Azhar: Database Systems 28
Example n n Find all the soda objects where the soda is sold by Joe’s Rest for less than 1. 60 Strategy: 1. 2. $soda will for-loop over all soda objects For each $soda, a. b. 3. let $joe be either the Joe’s-Rest object, if Joe sells the soda, or the empty set of rest objects. Test whether $joe sells the soda for < 1. 60 2/10/05 Salman Azhar: Database Systems 29
Example: The Query Attribute sold. By is of type IDREFS. Follow each ref to a REST and check if its name is Joe’s Rest. FOR $soda IN /RESTS/SODA LET $joe : = $soda/@sold. By=>REST[@name=“Joes. Rest”] LET $joe. Price : = $joe/PRICE[@the. Soda=$soda/@name] WHERE $joe. Price < 1. 60 RETURN <CHEAPSODA>$soda</CHEAPSODA> Only pass the values of $soda, $joe. Price to the RETURN clause if the string inside the PRICE object $joe. Price is < 1. 60 2/10/05 Find that PRICE subobject of the Joe’s Rest object that represents whatever soda is currently $soda. Salman Azhar: Database Systems 30
Summary n Two query language to search in XML documents n XPATH n n XQUERY n 2/10/05 Queries are really path descriptors SQL like language for querying XML documents Salman Azhar: Database Systems 31
- Salman azhar
- Salman azhar
- Xquery injection
- Xquery vs xslt
- Xquery basics
- Sql query for xml
- Iterative vs recursive dns
- Query tree and query graph
- Query tree and query graph
- Relational query languages in dbms
- Relational algebra and calculus
- Formal relational query languages
- Find the id name dept_name
- Formal relational query languages
- Saiful azhar
- Azhar kasim
- Dr azhar hussain
- Dr azhar parvez
- Prof azhar kasim
- Pflng satu
- Damir azhar
- Msxml xpath
- Xpath exercises
- Xml
- Xpath visualizer
- Org.apache.xpath.xpathapi
- Flukeout answers
- Xpath beispiele
- Xpath root
- Salman f rahman
- Dr aneel salman
- Deque salman