WhydoweneedanXMLquery language? XQuery:AnXMLQueryLanguage - - PDF document

why do we need an xml query language xquery an xml query
SMART_READER_LITE
LIVE PREVIEW

WhydoweneedanXMLquery language? XQuery:AnXMLQueryLanguage - - PDF document

WhydoweneedanXMLquery language? XQuery:AnXMLQueryLanguage XMLemergingasdominantstandardfordata representationandexchangeintheInternet Flexibleandself-describing


slide-1
SLIDE 1

CS433,Fall2001 1

XQuery:AnXMLQueryLanguage CS433 Acknowledgment:Manyoftheslides borrowedfromDonChamberlin

CS433,Fall2001 2

WhydoweneedanXMLquery language?

❖ XMLemergingasdominantstandardfordata

representationandexchangeintheInternet

– Flexibleandself-describing

❖ Torealizepotential,XMLneedsaquery

languageofcomparableflexibility

– Quilt, XPath,XQL,XML-QL,Lorel,YATL,… – …andXQuery

CS433,Fall2001 3

XQuery

❖ XMLQueryWGwascharteredinOctober1999 ❖ About50membersfromabout35companies ❖ Publicwebsite:www.w3.org/XML/Query ❖ Publiccomments:www-xml-query-

comments@w3.org

CS433,Fall2001 4

WorkingDrafts(6/7/01)

❖ Requirements ❖ XQuery(with102openissues!) ❖ UseCases(10applications,manyqueries) ❖ DataModel(forbothXQueryandXPath2.0) ❖ FormalSemantics(with99openissues!) ❖ XQueryX(XMLSyntaxforXQuery)

CS433,Fall2001 5

Terminology

❖ Quilt ❖ XQuery ❖ XPath ❖ XSLT ❖ XMLSchema ❖ XMLQueryAlgebra("Core") ❖ XQueryX

CS433,Fall2001 6

Antecedents:XPath andXQL

❖ Closelyrelatedlanguagesfornavigatingahierarchy ❖ Apathexpression isaseriesofsteps ❖ Eachstepmovesalonganaxis (children,ancestors,

attributes,etc.)andmayapplyapredicate

❖ XPathhasanabbreviatedsyntax,adaptedfromXQL

/book[title=“WarandPeace”] /chapter[title=“War”] //figure[contains(caption,“Guns”)]

❖ XQLhassomeadditionaloperators(BEFORE,AFTER,…)

slide-2
SLIDE 2

CS433,Fall2001 7

Antecedents:SQLandOQL

❖ SQLandOQLaredatabasequerylanguages ❖ SQLderivesatablefromothertablesbyastylized

seriesofclauses:SELECT-FROM-WHERE

❖ OQLisafunctionallanguage

– Aqueryisanexpression – Expressionscantakeseveralforms – Expressionscanbenestedandcombined – SELECT-FROM-WHEREisoneformofOQLexpression

CS433,Fall2001 8

XQuery Basics

❖ XQueryisafunctionallanguage ❖ Expressionscanbecomposedfromother

expressions

❖ Thedatamodelisanorderedforestofnodes

A A A B B B C C C C A B C C C C A B CS433,Fall2001 9

AFirstLookatXQuery

❖ Findthedescriptionandaveragepriceof

eachredpartthathasatleast10orders

FOR$pINdocument("parts.xml") //part[color="Red"] LET$o:=document("orders.xml") //order[partno =$p/partno] WHEREcount($o)>=10 RETURN <important_red_part> {$p/description} <avg_price>{avg($o/price)}</avg_price> </important_red_part>

CS433,Fall2001 10

XQueryExpressions

❖ Constantsandvariables:

– $x,47.2,"Hello"

❖ Operatorsandfunctioncalls:

– foo($a+1,$b- 2),$catsUNION$dogs

❖ PathexpressionsusingXPathnotation: – /chapter[title="Frogs"]/fig[caption="TreeFrogs"] ❖ Sequences:

– (1,/a/b/c,"Hello")

CS433,Fall2001 11

XQueryExpressions,cont'd

❖ Elementconstructors:

– PureXML: <acolor="Red"> <b>Hello</b> <c>Goodbye</c> </a> – XMLwithnestedexpressions: <acolor={$x/color}> {$yUNION$z} </a>

CS433,Fall2001 12

AFLWRExpression

❖ AFLWRexpressionbindsvariables,appliesa

predicate,andconstructssomenewresults

– For…Let…Where…Return For_clause Let_clause Where_clause Return_clause

slide-3
SLIDE 3

CS433,Fall2001 13

FORClause

❖ FORisusedforiteratingoveroneormore

collections

❖ Eachexpressionevaluatestoacollectionofnodes ❖ TheFORclauseproducesmany binding-tuples

fromthecartesianproductofthesecollections

❖ Ineachtuple,thevalueofeachvariableisone node

anditsdescendants

❖ Theorderofthetuplespreservedocumentorder

(unlessthereisanon-order-preservingfunction)

For variable

expression ,

CS433,Fall2001 14

LETClause

❖ LETisusedforbindingvariables(without

iteration)

❖ ALETclauseproducesone bindingforeach

variable(therefore,itdoesnotaffectthenumberof bindingtuples)

❖ Thevariableisboundtothevalueofexpression,

whichmaycontainmany nodes

❖ Documentorderispreservedamongthenodesin

eachcollectionunlessexpression containsanon-

  • rder-preservingfunctionsuchasdistinct()

Let variable

✂ ✄

expression ,

CS433,Fall2001 15

WHEREClause

❖ Appliesapredicatetothetuplesofboundvariables ❖ Retainsonlytuplesthatsatisfythepredicate ❖ Preservesorderoftuples,ifany ❖ MaycontainAND,OR,NOT ❖ Appliesscalarconditionstoscalarvariables:

$color=“Red

❖ Appliessetconditionstovariablesboundtosets:

avg($emp/salary)>10000 Where boolean-expression

CS433,Fall2001 16

RETURNClause

❖ ConstructstheresultoftheFLWRexpression ❖ Executedonceforeachtupleofboundvariables ❖ Preservesorderoftuples,ifany,… ❖ ORcanimposeaneworderusingaSORTBYclause ❖ Oftenusesanelementconstructor

<item> {$item/itemno} <avg_bid>{avg($b/bid_amount)}</avg_bid> </item>SORTBYitemno RETURN expression

CS433,Fall2001 17

SummaryofFLWRDataFlow

FOR/LET

☎✝✆ ✞ ✟✡✠☞☛ tuples of ✌ ✠☞✍☞✎☞✏✒✑✔✓✖✕ ✆ ✓ ✌✔✗ ✘ ✞

WHERE RETURN

☎✝✆ ✞ ✟✡✠☞☛ tuples of ✌ ✠☞✍☞✎☞✏✒✑✔✓✖✕ ✆ ✓ ✌✔✗ ✘ ✞ ✙ ✚☞✛ ✄ ✑✔✓ ✗ ✍ ✘✖✜ ✚☞✢ ✄ ✑✔✓ ✗ ✍ ✘✖✜ ✚☞✣ ✄ ✑✔✓ ✗ ✍ ✘✖✤ ✙ ✚☞✛ ✄ ✑✔✓ ✗ ✍ ✘✖✜ ✚☞✢ ✄ ✑✔✓ ✗ ✍ ✘✖✜ ✚☞✣ ✄ ✑✔✓ ✗ ✍ ✘✖✤ ✙ ✚☞✛ ✄ ✑✔✓ ✗ ✍ ✘✖✜ ✚☞✢ ✄ ✑✔✓ ✗ ✍ ✘✖✜ ✚☞✣ ✄ ✑✔✓ ✗ ✍ ✘✖✤

XML

✥✧✦★☎ ✄✪✩ ✕ ✏ ✘ ✕ ✘ ✏✫☛ ✠☞✕ ✘ ✞ ✟✡✠☞☛✝✎☞✠✔✏ ✘ ✞

CS433,Fall2001 18

AnExampleDocument:bib.xml

❖ bib.xmlhasthefollowingstructure

<bib> <book> <title>…</title> <author>…</author> … <publisher>…</publisher> <year>…</year> <price>…</price> </book> … </bib>

slide-4
SLIDE 4

CS433,Fall2001 19

SimpleXQuery Queries

❖ Findallbookspublishedin1998byPenguin

FOR$bINdocument(“bib.xml”)//book WHERE$b/year=“1998” AND$b/publisher=“Penguin” RETURN$bSORTBY(author,title)

CS433,Fall2001 20

SimpleXQuery Queries(contd.)

❖ Findtitlesofbooksthathavenoauthors

<orphan_books> {FOR$bINdocument(“bib.xml”)//book WHEREempty($b/author) RETURN$b/titleSORTBY(.)} </orphan_books>

CS433,Fall2001 21

NestedQueries

❖ Invertthehierarchyfrompublishersinside

bookstobooksinsidepublishers

FOR$pINdistinct(//publisher) RETURN<publishername={$p/text}> {FOR$bIN//book[publisher=$p] RETURN<book> {$b/title} {$b/price} </book> SORTBY(priceDESCENDING)} </publisher>SORTBY(name)

CS433,Fall2001 22

ConditionalExpressions

❖ Makealistofholdingorderedbytitle;for

journalsincludetheeditor,otherwiseinclude theauthor

FOR$hIN//holding RETURN<holding> {$h/title} {IF$h/@type =“Journal” THEN$h/editor ELSE$h/author} </holding> IF

✂✁ ✄✆☎ ✝

THEN

✂✁ ✄✆☎ ✞

ELSE expr3

CS433,Fall2001 23

QuantifiedExpressions

❖ Quantifiedexpressionsareaformofpredicate

(returnboolean)

❖ Findtitlesofbooksinwhichbothsailingand

windsurfingarementionedinthesame paragraph

FOR$bIN//book WHERESOME$pIN$b//paraSATISFIES contains($p,“Sailing”)AND contains($p,“Windsurfing”) RETURN$b/title var IN expr SATISFIES predicate SOME EVERY

CS433,Fall2001 24

OperatorsBasedonGlobalOrdering

❖ Returnsnodesinexpr1 thatarebefore(after)

nodesinexpr2

❖ Findprocedureswherenoanesthesiaoccurs

beforethefirstincision

FOR$procIN//section[title=“Procedure”] WHEREempty($proc//anesthesiaBEFORE ($proc//incision)[1]) RETURN$proc expr1 BEFORE AFTER expr2

slide-5
SLIDE 5

CS433,Fall2001 25

Functions

❖ Aquerycandefineitsownlocalfunctions ❖ Iffisascalarfunction,f(S)is[f(s):sinS] ❖ Functionscanberecursive ❖ “Computethemaximumdepthofnestedparts

indocumentnamedpartlist.xml”

FUNCTIONdepth($e) {IFempty($e/*)THEN1 ELSEmax(depth($e/*))+1 } depth(document(“partlist.xml”)/*)

CS433,Fall2001 26

Summary

❖ XMLisaveryversatilemarkuplanguage ❖ XQuery isaquerylanguagedesignedtobeas

versatileasXML

❖ XQuery drawsfeaturesfromseveralother

languages

❖ XQuery canhelpXMLrealizeitspotentialasa

universallanguagefordatainterchange

❖ Formoredetails,see:

http://www.w3.org/XML/Query.html