<<

XPATH and XQUERY

‹Two 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 of a graph with its subobjects as its children

3 4

Example DTD Example Document

Every REST has 1 or 1.50 more PRICE and also an attr name 1.75 … PRICE has data for price & the price. SuesRest,…”> … ]> SODA name for ID and IDREFs 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

1.50 1.50 1.75 1.75 SuesRest,…”> … /RESTS/REST/PRICE describes the … //PRICE describes the same PRICE 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 any other bars. a RESTS and a REST. 9 10

Wild-Card * Example: /RESTS/* ‹A star (*) in place of a tag represents any one tag. 1.50 Š Acts as a “wildcard” 1.75 ‹Example: /*/*/PRICE represents all … price objects at the third level of nesting. … /RESTS/* captures all REST and SODA objects, such as these.

11 12

2 Attributes Example: /RESTS/*/@name

‹We may refer to attributes in addition to tags. 1.50 ‹In XPATH, we refer to attributes by 1.75 prepending @ to their name. … ‹Attributes of a tag may appear in paths … /RESTS/*/@name selects all name attributes of immediate 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 result of a path expression are ones that 1.50 Š have that tag and 1.75 Š also satisfy the condition … 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 start at the root and execute a sequence of steps to find a set of nodes at each 1.50 step. 1.75 Š At each step, we may follow any one of … 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 IN ,… 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 := ,… RETURN ‹Value of the variable becomes the set $soda 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. Dew Slice… 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 $sodas 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 Dew, Slice,… 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 $soda 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