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
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
Wild-Card * Example: /RESTS/*
11 12
2 Attributes Example: /RESTS/*/@name
We may refer to attributes in addition
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
Example: Attribute in Selection Axes
/RESTS/REST/PRICE[@theSoda = “Slice”] In general, path expressions allow us to
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
23 24
4 Example: FOR LET Clauses
FOR $soda IN /RESTS/SODA/@name LET
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
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
29 30
5