Why�do�we�need�an�XML�query� language? XQuery:�An�XML�Query�Language ❖ XML�emerging�as�dominant�standard�for�data� representation�and�exchange�in�the�Internet – Flexible�and�self-describing CS433 ❖ To�realize�potential,�XML�needs�a�query� language�of�comparable�flexibility – Quilt, XPath,�XQL,�XML-QL,�Lorel,�YATL,�… Acknowledgment:�Many�of�the�slides� – …�and�XQuery borrowed�from�Don�Chamberlin CS433,�Fall�2001 1 CS433,�Fall�2001 2 XQuery Working�Drafts�(6/7/01) ❖ XML�Query�WG�was�chartered�in�October�1999 ❖ Requirements ❖ XQuery�(with�102�open�issues!) ❖ About�50�members�from�about�35�companies ❖ Use�Cases�(10�applications,�many�queries) ❖ Public�website:�www.w3.org/XML/Query ❖ Data�Model�(for�both�XQuery�and�XPath�2.0) ❖ Public�comments:�www-xml-query- ❖ Formal�Semantics�(with�99�open�issues!) comments@w3.org ❖ XQueryX�(XML�Syntax�for�XQuery) CS433,�Fall�2001 3 CS433,�Fall�2001 4 Terminology Antecedents:�XPath and�XQL ❖ Closely�related�languages�for�navigating�a�hierarchy ❖ Quilt ❖ A� path�expression is�a�series�of� steps ❖ XQuery ❖ Each�step�moves�along�an� axis (children,�ancestors,� ❖ XPath attributes,�etc.)�and�may�apply�a� predicate ❖ XSLT ❖ XPath�has�an�abbreviated�syntax,�adapted�from�XQL ❖ XML�Schema /book[title�=�“War�and�Peace”] ❖ XML�Query�Algebra�("Core") /chapter[title�=�“War”] //figure[contains(caption,�“Guns”)] ❖ XQueryX ❖ XQL�has�some�additional�operators�(BEFORE,�AFTER,�…) CS433,�Fall�2001 5 CS433,�Fall�2001 6
Antecedents:�SQL�and�OQL XQuery Basics ❖ XQuery�is�a�functional�language ❖ SQL�and�OQL�are�database�query�languages ❖ Expressions�can�be�composed�from�other� ❖ SQL�derives�a�table�from�other�tables�by�a�stylized� expressions series�of�clauses:�SELECT-FROM-WHERE ❖ The�data�model�is�an�ordered�forest�of�nodes ❖ OQL�is�a�functional�language – A�query�is�an�expression C C – Expressions�can�take�several�forms C C B A C B – Expressions�can�be�nested�and�combined C B A C A – SELECT-FROM-WHERE�is�one�form�of�OQL�expression A B A C B CS433,�Fall�2001 7 CS433,�Fall�2001 8 A�First�Look�at�XQuery XQuery�Expressions ❖ Find�the�description�and�average�price�of� ❖ Constants�and�variables: each�red�part�that�has�at�least�10�orders – $x,���47.2,���"Hello" FOR�$p�IN�document("parts.xml") ❖ Operators�and�function�calls: //part[color�=�"Red"] – foo($a�+�1,�$b�- 2),���$cats�UNION�$dogs� LET�$o�:=�document("orders.xml") //order[partno =�$p/partno] ❖ Path�expressions�using�XPath�notation: WHERE�count($o)�>=�10 – /chapter[title="Frogs"]/fig[caption="Tree�Frogs"] RETURN ❖ Sequences: <important_red_part> {�$p/description�} – (1,�/a/b/c,�"Hello") <avg_price>�{avg($o/price)}�</avg_price> </important_red_part> CS433,�Fall�2001 9 CS433,�Fall�2001 10 XQuery�Expressions,�cont'd A�FLWR�Expression ❖ Element�constructors: ❖ A�FLWR�expression�binds�variables,�applies�a� predicate,�and�constructs�some�new�results – Pure�XML: – For�…�Let�…�Where�…�Return <a�color�=�"Red"> <b>Hello</b> <c>Goodbye</c> </a> For_clause Return_clause – XML�with�nested�expressions: <a�color�=�{$x/color}> Where_clause Let_clause {�$y�UNION�$z�} </a> CS433,�Fall�2001 11 CS433,�Fall�2001 12
✞ ✍ ✗ ✍ ✄ ✗ ✍ ✄ ✗ ✙ ✙ ✄ ✗ ✍ ✄ ✗ ✍ ✄ ✗ ✄ ✍ ✕ ✞ ✘ ✌ ✙ ✞ ✄ ✗ ✍ ✘ ✗ ✄ ✓ ✆ ✗ ✌ ✍ ✞ ✄ ✍ ✏ ✓ ✂ � ✁ ✞ ✘ ✘ ✆ ✄ ✕ ✘ ✘ ✞ FOR�Clause LET�Clause , , For Let variable expression variable expression ❖ FOR�is�used�for�iterating�over�one�or�more� ❖ LET�is�used�for�binding�variables�(without� collections iteration) ❖ Each�expression�evaluates�to�a�collection�of�nodes ❖ A�LET�clause�produces� one binding�for�each� variable�(therefore,�it�does�not�affect�the�number�of� ❖ The�FOR�clause�produces� many binding-tuples� binding�tuples) from�the�cartesian�product�of�these�collections ❖ The�variable�is�bound�to�the�value�of� expression ,� ❖ In�each�tuple,�the�value�of�each�variable�is� one node� which�may�contain� many nodes and�its�descendants ❖ Document�order�is�preserved�among�the�nodes�in� ❖ The�order�of�the�tuples�preserve�document�order� each�collection�unless� expression contains�a�non- (unless�there�is�a�non-order-preserving�function) order-preserving�function�such�as�distinct() CS433,�Fall�2001 13 CS433,�Fall�2001 14 WHERE�Clause RETURN�Clause RETURN expression Where boolean-expression ❖ Applies�a�predicate�to�the�tuples�of�bound�variables ❖ Constructs�the�result�of�the�FLWR�expression ❖ Retains�only�tuples�that�satisfy�the�predicate ❖ Executed�once�for�each�tuple�of�bound�variables ❖ Preserves�order�of�tuples,�if�any ❖ Preserves�order�of�tuples,�if�any,�… ❖ May�contain�AND,�OR,�NOT ❖ OR�can�impose�a�new�order�using�a�SORTBY�clause ❖ Applies�scalar�conditions�to�scalar�variables: ❖ Often�uses�an�element�constructor $color�=�“Red <item> {$item/itemno} ❖ Applies�set�conditions�to�variables�bound�to�sets: <avg_bid>�{avg($b/bid_amount)}�</avg_bid> avg($emp/salary)�>�10000 </item>�SORTBY�itemno CS433,�Fall�2001 15 CS433,�Fall�2001 16 Summary�of�FLWR�Data�Flow An�Example�Document:�bib.xml FOR/LET ❖ bib.xml�has�the�following�structure ✟✡✠☞☛ tuples of ☎✝✆ <bib> ✠☞✍☞✎☞✏✒✑✔✓✖✕ ✌✔✗ <book> ✚☞✛ ✑✔✓ ✘✖✜ ✚☞✢ ✑✔✓ ✘✖✜ ✚☞✣ ✑✔✓ ✘✖✤ <title>�…�</title> ✚☞✛ ✑✔✓ ✘✖✜ ✚☞✢ ✑✔✓ ✘✖✜ ✚☞✣ ✑✔✓ ✘✖✤ WHERE <author>�…�</author> ✚☞✛ ✑✔✓ ✘✖✜ ✚☞✢ ✑✔✓ ✘✖✜ ✚☞✣ ✑✔✓ ✘✖✤ ✟✡✠☞☛ tuples of … ☎✝✆ <publisher>�…�</publisher> ✠☞✍☞✎☞✏✒✑✔✓✖✕ ✌✔✗ <year>�…�</year> RETURN <price>�…�</price> ✥✧✦★☎ ✏✫☛ ✠☞✕ ✟✡✠☞☛✝✎☞✠✔✏ </book> ✄✪✩ XML … </bib> CS433,�Fall�2001 17 CS433,�Fall�2001 18
✝ ✞ Simple�XQuery Queries Simple�XQuery Queries�(contd.) ❖ Find�all�books�published�in�1998�by�Penguin ❖ Find�titles�of�books�that�have�no�authors FOR�$b�IN�document(“bib.xml”)//book <orphan_books> WHERE�$b/year�=�“1998” {FOR�$b�IN�document(“bib.xml”)//book AND�$b/publisher�=�“Penguin” WHERE�empty($b/author) RETURN�$b�SORTBY�(author,�title) RETURN�$b/title�SORTBY�(.)} </orphan_books> CS433,�Fall�2001 19 CS433,�Fall�2001 20 Nested�Queries Conditional�Expressions IF THEN ELSE expr3 �✂✁ ✄✆☎ �✂✁ ✄✆☎ ❖ Invert�the�hierarchy�from�publishers�inside� ❖ Make�a�list�of�holding�ordered�by�title;�for� books�to�books�inside�publishers journals�include�the�editor,�otherwise�include� the�author FOR�$p�IN�distinct(//publisher) RETURN�<publisher�name={$p/text}> FOR�$h�IN�//holding {FOR�$b�IN�//book[publisher�=�$p] RETURN�<holding> RETURN�<book> {$h/title} {$b/title} {$b/price} {IF�$h/@type =�“Journal” </book> THEN�$h/editor SORTBY�(price�DESCENDING)} ELSE�$h/author} </publisher>�SORTBY�(name) </holding> CS433,�Fall�2001 21 CS433,�Fall�2001 22 Quantified�Expressions Operators�Based�on�Global�Ordering SOME var IN expr SATISFIES predicate EVERY BEFORE expr1 expr2 AFTER ❖ Quantified�expressions�are�a�form�of�predicate� (return�boolean) ❖ Returns�nodes�in� expr1 that�are�before�(after)� ❖ Find�titles�of�books�in�which�both�sailing�and� nodes�in� expr2 windsurfing�are�mentioned�in�the�same� paragraph ❖ Find�procedures�where�no�anesthesia�occurs� before�the�first�incision FOR�$b�IN�//book FOR�$proc�IN�//section[title�=�“Procedure”] WHERE�SOME�$p�IN�$b//para�SATISFIES WHERE�empty($proc//anesthesia�BEFORE contains($p,�“Sailing”)�AND ($proc//incision)[1]) contains($p,�“Windsurfing”) RETURN�$proc RETURN�$b/title CS433,�Fall�2001 23 CS433,�Fall�2001 24
Recommend
More recommend