




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
WhatisXQuery?
XQueryisthelanguageforqueryingXMLdataXQueryforXMLislikeSQLfordatabasesXQueryisbuiltonXPathexpressionsXQueryisdefinedbytheW3CXQueryissupportedbyallthemajordatabaseengines(IBM,Oracle,Microsoft,etc.)XQuerywillbecomeaW3Cstandard-anddeveloperscanbesurethatthecodewillworkamongdifferentproductsWhatisXQuery?XQ
WhyXQueryXML的發(fā)展趨勢(shì)使愈來(lái)愈多的資料以XML為標(biāo)準(zhǔn)格式,如何從”資料”轉(zhuǎn)變成有價(jià)值的”資訊”是另一波XML發(fā)展的重點(diǎn)。要對(duì)資料作有效的管理需透過(guò)資料庫(kù)管理系統(tǒng)的應(yīng)用,但由於XML的資料格式和傳統(tǒng)資料不同,不再適用傳統(tǒng)的資料庫(kù)及查詢語(yǔ)法,因此W3C依據(jù)XML的特性發(fā)展出XML的查詢語(yǔ)言,以方便對(duì)XML資料的管理及處理。
XPath的不足-
XPath是專門針對(duì)XML文件中定址(addressing)使用的語(yǔ)言,也就是用於找出XML文件中的資料元素,其當(dāng)初設(shè)計(jì)目的是為了供XSLT、XLink及XPointer使用。WhyXQueryXML
ExamplesofUsesXQuerycanbeusedto:ExtractinformationtouseinaWebServiceGeneratesummaryreportsTransformXMLdatatoXHTMLSearchWebdocumentsforrelevantinformationExamplesofUsesXQu
XQueryHistory
1998年,W3C公佈XML1.0版規(guī)格1998年十二月,W3C成立QueryLanguageWorkshop
1999年九月,W3C成立XMLQueryWorkingGroup1999年十一月,XPath1.0成為W3C的標(biāo)準(zhǔn)2001年六月,W3C釋出XQuery1.0草案規(guī)格(workingdraft)
2001年十二月,XQuery1.0規(guī)格第二版2002年十一月,W3C釋出最新版XQuery1.0以及XPath2.0相關(guān)文件2007年一月,成為W3C
Recommendation2010年12月,XQuery1.0XQueryHistory
XQuery的基本語(yǔ)法XQuery並不是使用XML語(yǔ)法,XQuery程式是一個(gè)巢狀的運(yùn)算式集合。XQuery是由PathExpressions、FLWOR運(yùn)算式、條件運(yùn)算式和
XQuery函數(shù)所組成。XQueryiscase-sensitive.XQueryelements,attributes,andvariablesmustbevalidXMLnames.AnXQuerystringvaluecanbeinsingleordoublequotes.AnXQueryvariableisdefinedwitha$followedbyaname,e.g.$bookstore.XQuerycommentsaredelimitedby(:and:),e.g.(:XQueryComment:).XQuery的基本語(yǔ)法XQueryHowtoSelectNodesFrom
"books.xml"?
XQueryusesfunctionstoextractdatafromXMLdocuments.Thedoc()functionisusedtoopenthe"books.xml"file:doc("books.xml")Books.xmlcontainsthefollowingchildelementswithinthe“book”element:
<titlelang="en"><author><year><price>HowtoSelectNodesFrom
Books.xml(1/2)<?xmlversion="1.0"encoding="ISO-8859-1"?><bookstore><bookcategory="COOKING"><titlelang="en">EverydayItalian</title><author>GiadaDeLaurentiis</author><year>2005</year><price>30.00</price></book><bookcategory="CHILDREN"><titlelang="en">HarryPotter</title><author>JK.Rowling</author><year>2005</year><price>29.99</price></book>Books.xml(1/2
Books.xml(2/2)<bookcategory="WEB"><titlelang="en">XQueryKickStart</title><author>JamesMcGovern</author><author>PerBothner</author><author>KurtCagle</author><author>JamesLinn</author><author>VaidyanathanNagarajan</author><year>2003</year><price>49.99</price></book><bookcategory="WEB"><titlelang="en">LearningXML</title><author>ErikT.Ray</author><year>2003</year><price>39.95</price></book></bookstore>Books.xml(2/2)
PathExpressions
XQueryusespathexpressionstonavigatethroughelementsinanXMLdocument.Thefollowingpathexpressionisusedtoselectallthetitleelementsinthe"books.xml"file:doc("books.xml")/bookstore/book/title(/bookstoreselectsthebookstoreelement,/bookselectsallthebookelementsunderthebookstoreelement,and/titleselectsallthetitleelementsundereachbookelement)TheaboveXQuerywillextractthefollowingtitles:<titlelang="en">EverydayItalian</title><titlelang="en">HarryPotter</title><titlelang="en">XQueryKickStart</title><titlelang="en">LearningXML</title>PathExpressionsXQ
UseofPredicatesUseofPredicates
XQueryFLWORExpressions
FLWORisanacronymfor"For,Let,Where,Orderby,Return".Asinfor$xindoc("books.xml")/bookstore/book
where$x/price>30
return$x/titleTheforclauseselectsallbookelementsunderthebookstoreelement
intoavariablecalled$x.Thewhereclauseselectsonlybookelementswithapriceelementwithavaluegreaterthan30.Theorderby
clausedefinesthesort-order.Willbesortedbythetitleelement.Thereturnclausespecifieswhatshouldbereturned.Hereitreturnsthetitleelements.XQueryFLWORExpressions
AnotherExampledoc("books.xml")/bookstore/book[price>30]/titleThefollowingFLWORexpressionwillselectexactlythesameasthepathexpressionabove:for$xindoc("books.xml")/bookstore/book
where$x/price>30
return$x/titleTheresultswillbe:
<titlelang="en">XQueryKickStart</title><titlelang="en">LearningXML</title>AnotherExampledo
AnXQueryExample(1/2)根元素:第2~9列的items是查詢結(jié)果XML文件的根元素,在本例執(zhí)行XQuery查詢結(jié)果的根元素都是items,如下所示:02:<items>03:{04:for$bookindoc("bookstore.xml")/bookstore/book05:where$book/price>60006:orderby$book/title07:return$book/title08:}09:</items>第3~8列的大括號(hào)之間才是真正的XQuery程式碼。AnXQueryExample(1/2)AnXQueryExample(2/2)FLWOR運(yùn)算式:第4~7列是XQuery的FLWOR運(yùn)算式,類似SQL語(yǔ)言Select/Where指令,屬於XQuery程式執(zhí)行查詢功能的程式區(qū)塊,如下所示:04:for$bookindoc("bookstore.xml“)/bookstore/book05:where$book/price>60006:orderby$book/title07:return$book/titleAnXQueryExample(2/2)FL
XQueryDataModelXQuery資料模型(DataModel)和XPath2.0相同,都是將XML文件視為一棵邏輯上的樹狀結(jié)構(gòu),將XML文件視為各種不同節(jié)點(diǎn)的集合。XQuery資料模型是節(jié)點(diǎn)(Nodes)、原子值(AtomicValues)和項(xiàng)目(Items)組成的類別架構(gòu)。XQueryDataModelXQueryFLWORExpressions
(1/4)原始文件:
<bib>
<books>
<book>
<title>TCP/IPIllustrated</title>
<author>Stevens</author>
<publisher>Addison-Wesley</publisher>
</book>
<book>
<title>AdvancedUnixProgramming</title>
<author>Stevens</author>
<publisher>Addison-Wesley</publisher>
</book>
<book>
<title>DataontheWeb</title>
<author>Abiteboul</author>
<author>Buneman</author>
<author>Suciu</author>
</book>
<books>
</bib>FLWORExpressions(1/4)原始文FLWORExpressions(2/4)XQuery:
<authlist>
{
for$booksindoc("bib.xml")//books
for$aindistinct-values($books//author)
orderby$a
return
<author>
<name>
{$a/text()}
</name>
<books>
{
for$bin$books//book[author=$a]
orderby$b/title
return$b/title
}
</books>
</author>
}
</authlist>FLWORExpressions(2/4)XQuFLWORExpressions(3/4)結(jié)果
(tobecontinued):
<authlist>
<author>
<name>Abiteboul</name>
<books>
<title>DataontheWeb</title>
</books>
</author>
<author>
<name>Buneman</name>
<books>
<title>DataontheWeb</title>
</books>
</author>
FLWORExpressions(3/4)結(jié)果FLWORExpressions(4/4)結(jié)果(續(xù)):
<author>
<name>Stevens</name>
<books>
<title>TCP/IPIllustrated</title>
<title>AdvancedUnixProgramming</title>
</books>
</author>
<author>
<name>Suciu</name>
<books>
<title>DataontheWeb</title>
</books>
</author>
</authlist>FLWORExpressions(4/4)結(jié)果(
XQuerySyntax–Rules(1/5)XQuery語(yǔ)言並不是使用XML語(yǔ)法,其基本語(yǔ)法規(guī)則,如下所示:XQuey語(yǔ)言區(qū)分英文字母大小寫,Items、items和ITEMS是不同的名稱,通常是使用小寫英文字母。XQuery程式碼的元素、屬性、函數(shù)和變數(shù)名稱必須是合法的XML名稱,可以加上名稱空間的字首。XQuerySyntax–Rules(
XQuerySynatex–DataType(2/5)XQuery的基本資料型態(tài)和XMLSchema內(nèi)建資料型態(tài)相同,如下所示:布林(Boolean)資料型態(tài):布林值true或false。數(shù)字(Number)資料型態(tài):包含整數(shù)和浮點(diǎn)數(shù)值。字串(Strings)資料型態(tài):使用單引號(hào)或雙引號(hào)括起的字元集合。日期時(shí)間資料型態(tài):代表日期、時(shí)間和期間。XML相關(guān)的資料型態(tài):例如:QName等資料型態(tài)。XQuerySynatex–DataTypXQuerySyntax–VariablesandLiterals(3/5)在XQuery程式碼使用的變數(shù)並不需事先宣告,變數(shù)是以「$」符號(hào)開始的合法XML名稱。
例如:XQuery變數(shù)範(fàn)例如下:$x、$y、$name、$bookXQuery的文字值(LiteralValues)主要分為三種型態(tài),如下所示:字串(Strings):XQuery的字串可以使用單引號(hào)或雙引號(hào)括起的字元集合。例如:"P679"。數(shù)字(Numbers):XQuery的數(shù)字可以是整數(shù)或浮點(diǎn)數(shù)。例如:1、2、34.5。建構(gòu)型態(tài):使用函數(shù)建構(gòu)的特殊型態(tài)。例如:日期xs:date("2006-12-31")。XQuerySyntax–Variablesand
XQuerySyntax–Comparators(4/5)XQuery1.0和XPath2.0使用相同的運(yùn)算子,XQuery的比較運(yùn)算子分為兩種,如下所示:一般比較(GeneralComparisons):一般比較運(yùn)算子有=、!=、<、<=、>、>=,可以用來(lái)比較多個(gè)項(xiàng)目。值比較(ValueComparisons):值比較運(yùn)算子有eq、ne、lt、le、gt、ge,和一般比較的差異在於它是比較單獨(dú)值,也就是說(shuō),運(yùn)算元一定是原子值(AtomicValue),或包含原子值的節(jié)點(diǎn)。XQuerySyntax–Comparators(
XQuerySyntax–Comments(5/5)在XQuery程式碼可以使用XQuery的註解文字,這是「(:」開頭到「:)」之間的字串,如下所示:(:程式範(fàn)例:Ch7-2-1.xq:)註解文字不可以出現(xiàn)在查詢結(jié)果的XML文件之中。因?yàn)閄Query查詢結(jié)果是一份XML文件或XHTML文件,所以也可以使用XML文件的註解,如下所示:<!--文件範(fàn)例:Bookstore.xml-->XQuerySyntax–Comments(letandreturnclause–letclauseFLWOR運(yùn)算式的let子句可以指定XQuery變數(shù)的值,return子句傳回查詢結(jié)果。FLWOR運(yùn)算式let子句指定XQuery變數(shù)的值可以是項(xiàng)目或循序,如下所示:let$book:=doc("bookstore.xml")//book[1]let$title:="Java2程式設(shè)計(jì)範(fàn)例教本"let$list:=(1to5)let$price:=650letandreturnclause–letcl
letandreturnclause–return(1/4)FLWOR運(yùn)算式return子句可以輸出查詢結(jié)果,如果是路徑運(yùn)算式,就是輸出選取的節(jié)點(diǎn)內(nèi)容,如下所示:let$book:=doc("bookstore.xml")//book[1]return$book/@code$book變數(shù)是第1個(gè)book元素節(jié)點(diǎn),return子句傳回book元素的code屬性節(jié)點(diǎn)。letandreturnclause–reletandreturnclause–return(2/4)如果輸出的XQuery變數(shù)是文字值,我們可以在return子句加上標(biāo)籤來(lái)建立元素節(jié)點(diǎn),如下所示:let$title:="Java2程式設(shè)計(jì)範(fàn)例教本"return<title>{$title}</title>$title變數(shù)值是字串,大括號(hào)括起變數(shù)$title,表示取出變數(shù)內(nèi)容,再加上標(biāo)籤就可以建立title元素節(jié)點(diǎn),如下所示:<title>Java2程式設(shè)計(jì)範(fàn)例教本</title>letandreturnclause–returletandreturnclause–return(3/4)變數(shù)值如果是循序,例如:使用return子句輸出變數(shù)$list,如下所示:let$list:=(1to5)return<num>{$list}</num>return子句只會(huì)輸出1個(gè)num元素,如下所示:<num>12345</num>letandreturnclause–retuletandreturnclause–return(4/4)return子句還可以合併多個(gè)運(yùn)算式成為一個(gè)循序,然後輸出循序的內(nèi)容,如下所示:let$price:=650return(<listprice>{$price}</listprice>,<price>{$price*0.8}</price>)「(」和「)」括起的是一個(gè)循序,輸出的內(nèi)容包含兩個(gè)元素listprice和price,price元素值是變數(shù)$price的八折。letandreturnclause–retu
forclause–basicforclause(1/6)基本for子句是以迴圈方式一一取出循序的項(xiàng)目,通常我們是使用路徑運(yùn)算式來(lái)取出循序。例如:取出bookstore.xml文件的所有price元素節(jié)點(diǎn),如下所示:for$priceindoc("bookstore.xml")//book/pricereturn$price運(yùn)算式在in指令後是路徑運(yùn)算式,取得所有price元素節(jié)點(diǎn)的循序,在in指令前是變數(shù)$price,指定成每執(zhí)行一次迴圈所取得的項(xiàng)目,return子句可以傳回變數(shù)內(nèi)容,也就是每一個(gè)price元素。forclause–basicforclauseforclause–countingexpression(2/6)在for子句可以加上at指令的計(jì)數(shù)變數(shù),以便計(jì)算迴圈總共執(zhí)行幾次。例如:在bookstore.xml文件取得所有book元素節(jié)點(diǎn),並且傳回每一個(gè)節(jié)點(diǎn)的計(jì)數(shù),如下所示:for$bookat$iindoc("bookstore.xml")//bookreturn(<num>{$i}</num>,$book/title)運(yùn)算式每執(zhí)行一次,變數(shù)$i就加1,return子句傳回的num元素就是變數(shù)$i的值。forclause–countingexpressiforclause–rangeexpression(3/6)FLWOR運(yùn)算式的for子句可以如同程式語(yǔ)言的迴圈敘述建立範(fàn)圍運(yùn)算式。例如:執(zhí)行1~3範(fàn)圍的for子句,如下所示:for$iin(1to3)return<count>{$i}</count>運(yùn)算式在in指令後的是自定循序,即1~3,換句話說(shuō),變數(shù)$i的值就是1~3,所以return子句傳回的就是3個(gè)count元素,其值也是1~3。forclause–rangeexpressionforclause–multiplefor(4/6)FLWOR運(yùn)算式允許同時(shí)使用多個(gè)for子句,每個(gè)子句使用「,」符號(hào)分隔,例如:兩個(gè)for子句的範(fàn)圍運(yùn)算式,如下所示:for$xin(1,2),$yin(5,6)return<eval>x={$x}y={$y}</eval>運(yùn)算式一共有2個(gè)範(fàn)圍運(yùn)算式,類似巢狀迴圈,可以建立4個(gè)eval元素。forclause–multiplefor(4/forclause–ResultsinHTMLlist(5/6)<ul>
{
for$xindoc("books.xml")/bookstore/book/title
orderby$x
return<li>{$x}</li>
}
</ul>Output:
<ul>
<li><titlelang="en">EverydayItalian</title></li>
<li><titlelang="en">HarryPotter</title></li>
<li><titlelang="en">LearningXML</title></li>
<li><titlelang="en">XQueryKickStart</title></li>
</ul>forclause–ResultsinHTMLlforclause–ResultsinHTMLlist(6/6)<ul>
{
for$xindoc("books.xml")/bookstore/book/title
orderby$x
return<li>{data($x)}</li>
}
</ul>Output:
<ul>
<li>EverydayItalian</li>
<li>HarryPotter</li>
<li>LearningXML</li>
<li>XQueryKickStart</li>
</ul>forclause–ResultsinHTMLl
whereclause–singlecondition(1/2)單一條件的where子句只有一個(gè)條件運(yùn)算式,例如:過(guò)濾price元素值需要大於600,如下所示:for$priceindoc("bookstore.xml")//book/pricewhere$price>600return$pricewhere子句只有當(dāng)$price>600值為true時(shí),才會(huì)輸出return子句的元素。whereclause–singleconditwhereclause–multipleconditions(2/2)在where子句可以使用and和or運(yùn)算式建立包含多個(gè)子條件的運(yùn)算式,如下所示:for$bookat$iindoc("bookstore.xml“)//bookwherestarts-with($book/title,"P")and$book/price>=600return(<num>{$i}</num>,$book/title)where子句使用and連接兩個(gè)條件運(yùn)算式,第1個(gè)是XQuery函數(shù)starts-with()檢查書名是否是"P"開頭,第2個(gè)運(yùn)算式過(guò)濾price元素大於等於600。whereclause–multiplecondit
orderbyclause在FLWOR運(yùn)算式的orderby子句,可以指定輸出結(jié)果的排序方式。例如:指定輸出結(jié)果使用book的title子元素值進(jìn)行排序,如下所示:for$bookindoc("bookstore.xml")//bookorderby$book/titlereturn$book/title運(yùn)算式的orderby子句指定使用title元素進(jìn)行排序。orderbyclause在
DistinctOutput在FLWOR運(yùn)算式的for子句,可以使用XQuery函數(shù)distinct-values()指定輸出結(jié)果沒(méi)有重複值。例如:bookstore.xml文件的price元素?fù)碛兄匮}值,我們可以使用函數(shù)來(lái)輸出沒(méi)有重複值的結(jié)果,如下所示:let$books:=doc("bookstore.xml")//bookfor$pindistinct-values($books/price)return<price>{$p}</price>for子句使用distinct-values()函數(shù)顯示沒(méi)有重複的值,也就是刪除重複的price元素值。DistinctOutpu
ConditionalOperationsXQuery語(yǔ)言也支援if/then/else條件運(yùn)算式,如果if的條件為true,就執(zhí)行then之後的運(yùn)算式,否則執(zhí)行else之後的運(yùn)算式,如下所示:for$bookindoc("bookstore.xml")//bookreturnif($book/@code="P697")then<newtitle>{data($book/title)}</newtitle>else$book/titleConditionalOper
CreateaNewElement在XQuery路徑運(yùn)算式雖然只能輸出查詢結(jié)果的XML元素與屬性,不過(guò),F(xiàn)LWOR運(yùn)算式的return子句,就可以新增XML或XHTML元素,如下所示:return<li>{data($book/title)}</li>return子句新增XHTML的<li>標(biāo)籤,使用data()函數(shù)取出title元素內(nèi)容來(lái)新增XHTML元素。CreateaNewElement
CreatingaNewAttributeFLWOR運(yùn)算式的return子句可以新增XML或XHTML元素的屬性,如下所示:return<liclass="{data($book/@code)}">{data($book/title)}</li>return子句不只新增XHTML的<li>標(biāo)籤,還新增class屬性,其值是XML元素book的code屬性值。CreatingaNewAttriJoinOperation–usingPredicates在FLWOR運(yùn)算式可以使用多重for子句,配合return
來(lái)執(zhí)行合併查詢。例如:在Order.xml查詢dept屬性為"CS"的訂單項(xiàng)目,並且使用合併查詢來(lái)查詢?cè)擁?xiàng)目在Bookstore.xml的書名,如下所示:
let$order:=doc("Order.xml")let$bookstore:=doc("Bookstore.xml")for$itemin$order//item[@dept="CS"],$bookin$bookstore//book[@code=$item/@code]return<itemcode="{$item/@code}"><title>{data($book/title)}</title><quantity>{data($item/@quantity)}</quantity></item>JoinOperation–usingPredicaJoinOperation–usingwhereclause在FLWOR運(yùn)算式也可以使用多重for子句,配合where子句來(lái)執(zhí)行合併查詢。在Order.xml查詢dept屬性為"CS"的訂單項(xiàng)目,並且使用合併查詢來(lái)查詢?cè)擁?xiàng)目在Bookstore.xml的書名,如下所示:
let$order:=doc("Order.xml")let$bookstore:=doc("Bookstore.xml")for$itemin$order//item[@dept="CS"],$bookin$bookstore//bookwhere$item/@code=$book/@codereturn<itemcode="{$item/@code}"title="{$book/title}"quantity="{$item/@quantity}"/>JoinOperation–usingwherec
ExternalJoinOperation外部合併查詢是查詢第一份XML文件的全部項(xiàng)目,但是在第二份XML文件只取出存在的元素。在XQuery程式是使用兩個(gè)FLWOR運(yùn)算式來(lái)執(zhí)行外部合併查詢。ExternalJoinOperation外ExternalJoinOperation–Example例如:顯示Bookstore.xml所有圖書資料,但是只顯示Discount.xml文件中擁有折扣元素的書價(jià),如下所示:let$discount:=doc("Discount.xml")for$bookindoc("Bookstore.xml")//bookreturn<bookcode="{$book/@code}"><price>{for$pricein$discount//bookwhere$book/@code=$price/@codereturn$book/price*$price/discount}</price></book>ExternalJoinOperation–Exam
Grouping群組(Grouping)可以將資料進(jìn)行分類,分類方式是將來(lái)源XML文件的元素或?qū)傩灾抵?,重複值結(jié)合起來(lái)歸成一類。例如:在Order.xml文件訂單項(xiàng)目的item元素是使用dept屬性來(lái)分類,以便區(qū)分學(xué)校中屬於不同科系的訂單項(xiàng)目,如下所示:
for$deptindistinct-values($order//item/@dept)let$item:=$order//item[@dept=$dept]return<deptcode="{$dept}">{$item}</dept>Grouping群
AggregateFunctions(1/2)聚合函數(shù)可以執(zhí)行群組元素或?qū)傩再Y料的計(jì)算,以便提供進(jìn)一步的資料分析。例如:筆數(shù)、總和、平均、最大和最小值等。在XQuery常用的聚合函數(shù)說(shuō)明,如下表所示:AggregateFunctions(1/AggregateFunctions(2/2)例如:繼續(xù)群組查詢,我們可以使用count()和sum()聚合函數(shù)顯示每一群組的計(jì)數(shù)和訂單總量,如下所示:
for$deptindistinct-values($order//item/@dept)let$item:=$order//item[@dept=$dept]return<deptcode="{$dept}"><numOfItem>{count(distinct-values($item/@code))}</numOfItem><totalQuantity>{sum($item/@quantity)}</totalQuantity></dept>AggregateFunctions(2/2
XQuerySelf-definedFunctionsXQuery自訂函數(shù)是以declarefunction關(guān)鍵字進(jìn)行宣告,函數(shù)名稱擁有名稱空間字首。例如:minPrice()函數(shù)的宣告,如下所示:declarefunctionlocal:minPrice($priceasxs:integer?,$discountasxs:decimal?)asxs:decimal?{let$minprice:=$price*$discountreturn$minprice};XQuerySelf-definedFunctiXQueryFunctionInvocations在XQuery程式呼叫自訂函數(shù)時(shí),函數(shù)名稱需要包含字首。例如:呼叫自訂函數(shù)local:minPrice(),如下所示:
return<price>{
local:minPrice($book/price,$price/discount)}</price>XQueryFunctionInvocations在XQXQueryBulit-inFunctionsXQuery、XPath與XSLT擁有共用的內(nèi)建函數(shù),XPath的函數(shù)也一樣可以在XQuery程式使用。在XQuery程式呼叫XQuery內(nèi)建函數(shù)的方法時(shí),並不需要加上名稱空間的字首。在元素呼叫XQuery函數(shù)在路徑運(yùn)算式呼叫XQuery函數(shù)在FLWOR運(yùn)算式呼叫XQuery函數(shù)XQueryBulit-inFunctions
WhatisXQuery?
XQueryisthelanguageforqueryingXMLdataXQueryforXMLislikeSQLfordatabasesXQueryisbuiltonXPathexpressionsXQueryisdefinedbytheW3CXQueryissupportedbyallthemajordatabaseengines(IBM,Oracle,Microsoft,etc.)XQuerywillbecomeaW3Cstandard-anddeveloperscanbesurethatthecodewillworkamongdifferentproductsWhatisXQuery?XQ
WhyXQueryXML的發(fā)展趨勢(shì)使愈來(lái)愈多的資料以XML為標(biāo)準(zhǔn)格式,如何從”資料”轉(zhuǎn)變成有價(jià)值的”資訊”是另一波XML發(fā)展的重點(diǎn)。要對(duì)資料作有效的管理需透過(guò)資料庫(kù)管理系統(tǒng)的應(yīng)用,但由於XML的資料格式和傳統(tǒng)資料不同,不再適用傳統(tǒng)的資料庫(kù)及查詢語(yǔ)法,因此W3C依據(jù)XML的特性發(fā)展出XML的查詢語(yǔ)言,以方便對(duì)XML資料的管理及處理。
XPath的不足-
XPath是專門針對(duì)XML文件中定址(addressing)使用的語(yǔ)言,也就是用於找出XML文件中的資料元素,其當(dāng)初設(shè)計(jì)目的是為了供XSLT、XLink及XPointer使用。WhyXQueryXML
ExamplesofUsesXQuerycanbeusedto:ExtractinformationtouseinaWebServiceGeneratesummaryreportsTransformXMLdatatoXHTMLSearchWebdocumentsforrelevantinformationExamplesofUsesXQu
XQueryHistory
1998年,W3C公佈XML1.0版規(guī)格1998年十二月,W3C成立QueryLanguageWorkshop
1999年九月,W3C成立XMLQueryWorkingGroup1999年十一月,XPath1.0成為W3C的標(biāo)準(zhǔn)2001年六月,W3C釋出XQuery1.0草案規(guī)格(workingdraft)
2001年十二月,XQuery1.0規(guī)格第二版2002年十一月,W3C釋出最新版XQuery1.0以及XPath2.0相關(guān)文件2007年一月,成為W3C
Recommendation2010年12月,XQuery1.0XQueryHistory
XQuery的基本語(yǔ)法XQuery並不是使用XML語(yǔ)法,XQuery程式是一個(gè)巢狀的運(yùn)算式集合。XQuery是由PathExpressions、FLWOR運(yùn)算式、條件運(yùn)算式和
XQuery函數(shù)所組成。XQueryiscase-sensitive.XQueryelements,attributes,andvariablesmustbevalidXMLnames.AnXQuerystringvaluecanbeinsingleordoublequotes.AnXQueryvariableisdefinedwitha$followedbyaname,e.g.$bookstore.XQuerycommentsaredelimitedby(:and:),e.g.(:XQueryComment:).XQuery的基本語(yǔ)法XQueryHowtoSelectNodesFrom
"books.xml"?
XQueryusesfunctionstoextractdatafromXMLdocuments.Thedoc()functionisusedtoopenthe"books.xml"file:doc("books.xml")Books.xmlcontainsthefollowingchildelementswithinthe“book”element:
<titlelang="en"><author><year><price>HowtoSelectNodesFrom
Books.xml(1/2)<?xmlversion="1.0"encoding="ISO-8859-1"?><bookstore><bookcategory="COOKING"><titlelang="en">EverydayItalian</title><author>GiadaDeLaurentiis</author><year>2005</year><price>30.00</price></book><bookcategory="CHILDREN"><titlelang="en">HarryPotter</title><author>JK.Rowling</author><year>2005</year><price>29.99</price></book>Books.xml(1/2
Books.xml(2/2)<bookcategory="WEB"><titlelang="en">XQueryKickStart</title><author>JamesMcGovern</author><author>PerBothner</author><author>KurtCagle</author><author>JamesLinn</author><author>VaidyanathanNagarajan</author><year>2003</year><price>49.99</price></book><bookcategory="WEB"><titlelang="en">LearningXML</title><author>ErikT.Ray</author><year>2003</year><price>39.95</price></book></bookstore>Books.xml(2/2)
PathExpressions
XQueryusespathexpressionstonavigatethroughelementsinanXMLdocument.Thefollowingpathexpressionisusedtoselectallthetitleelementsinthe"books.xml"file:doc("books.xml")/bookstore/book/title(/bookstoreselectsthebookstoreelement,/bookselectsallthebookelementsunderthebookstoreelement,and/titleselectsallthetitleelementsundereachbookelement)TheaboveXQuerywillextractthefollowingtitles:<titlelang="en">EverydayItalian</title><titlelang="en">HarryPotter</title><titlelang="en">XQueryKickStart</title><titlelang="en">LearningXML</title>PathExpressionsXQ
UseofPredicatesUseofPredicates
XQueryFLWORExpressions
FLWORisanacronymfor"For,Let,Where,Orderby,Return".Asinfor$xindoc("books.xml")/bookstore/book
where$x/price>30
return$x/titleTheforclauseselectsallbookelementsunderthebookstoreelement
intoavariablecalled$x.Thewhereclauseselectsonlybookelementswithapriceelementwithavaluegreaterthan30.Theorderby
clausedefinesthesort-order.Willbesortedbythetitleelement.Thereturnclausespecifieswhatshouldbereturned.Hereitreturnsthetitleelements.XQueryFLWORExpressions
AnotherExampledoc("books.xml")/bookstore/book[price>30]/titleThefollowingFLWORexpressionwillselectexactlythesameasthepathexpressionabove:for$xindoc("books.xml")/bookstore/book
where$x/price>30
return$x/titleTheresultswillbe:
<titlelang="en">XQueryKickStart</title><titlelang="en">LearningXML</title>AnotherExampledo
AnXQueryExample(1/2)根元素:第2~9列的items是查詢結(jié)果XML文件的根元素,在本例執(zhí)行XQuery查詢結(jié)果的根元素都是items,如下所示:02:<items>03:{04:for$bookindoc("bookstore.xml")/bookstore/book05:where$book/price>60006:orderby$book/title07:return$book/title08:}09:</items>第3~8列的大括號(hào)之間才是真正的XQuery程式碼。AnXQueryExample(1/2)AnXQueryExample(2/2)FLWOR運(yùn)算式:第4~7列是XQuery的FLWOR運(yùn)算式,類似SQL語(yǔ)言Select/Where指令,屬於XQuery程式執(zhí)行查詢功能的程式區(qū)塊,如下所示:04:for$bookindoc("bookstore.xml“)/bookstore/book05:where$book/price>60006:orderby$book/title07:return$book/titleAnXQueryExample(2/2)FL
XQueryDataModelXQuery資料模型(DataModel)和XPath2.0相同,都是將XML文件視為一棵邏輯上的樹狀結(jié)構(gòu),將XML文件視為各種不同節(jié)點(diǎn)的集合。XQuery資料模型是節(jié)點(diǎn)(Nodes)、原子值(AtomicValues)和項(xiàng)目(Items)組成的類別架構(gòu)。XQueryDataModelXQueryFLWORExpressions
(1/4)原始文件:
<bib>
<books>
<book>
<title>TCP/IPIllustrated</title>
<author>Stevens</author>
<publisher>Addison-Wesley</publisher>
</book>
<book>
<title>AdvancedUnixProgramming</title>
<author>Stevens</author>
<publisher>Addison-Wesley</publisher>
</book>
<book>
<title>DataontheWeb</title>
<author>Abiteboul</author>
<author>Buneman</author>
<author>Suciu</author>
</book>
<books>
</bib>FLWORExpressions(1/4)原始文FLWORExpressions(2/4)XQuery:
<authlist>
{
for$booksindoc("bib.xml")//books
for$aindistinct-values($books//author)
orderby$a
return
<author>
<name>
{$a/text()}
</name>
<books>
{
for$bin$books//book[author=$a]
orderby$b/title
return$b/title
}
</books>
</author>
}
</authlist>FLWORExpressions(2/4)XQuFLWORExpressions(3/4)結(jié)果
(tobecontinued):
<authlist>
<author>
<name>Abiteboul</name>
<books>
<title>DataontheWeb</title>
</books>
</author>
<author>
<name>Buneman</name>
<books>
<title>DataontheWeb</title>
</books>
</author>
FLWORExpressions(3/4)結(jié)果FLWORExpressions(4/4)結(jié)果(續(xù)):
<author>
<name>Stevens</name>
<books>
<title>TCP/IPIllustrated</title>
<title>AdvancedUnixProgramming</title>
</books>
</author>
<author>
<name>Suciu</name>
<books>
<title>DataontheWeb</title>
</books>
</author>
</authlist>FLWORExpressions(4/4)結(jié)果(
XQuerySyntax–Rules(1/5)XQuery語(yǔ)言並不是使用XML語(yǔ)法,其基本語(yǔ)法規(guī)則,如下所示:XQuey語(yǔ)言區(qū)分英文字母大小寫,Items、items和ITEMS是不同的名稱,通常是使用小寫英文字母。XQuery程式碼的元素、屬性、函數(shù)和變數(shù)名稱必須是合法的XML名稱,可以加上名稱空間的字首。XQuerySyntax–Rules(
XQuerySynatex–DataType(2/5)XQuery的基本資料型態(tài)和XMLSchema內(nèi)建資料型態(tài)相同,如下所示:布林(Boolean)資料型態(tài):布林值true或false。數(shù)字(Number)資料型態(tài):包含整數(shù)和浮點(diǎn)數(shù)值。字串(Strings)資料型態(tài):使用單引號(hào)或雙引號(hào)括起的字元集合。日期時(shí)間資料型態(tài):代表日期、時(shí)間和期間。XML相關(guān)的資料型態(tài):例如:QName等資料型態(tài)。XQuerySynatex–DataTypXQuerySyntax–VariablesandLiterals(3/5)在XQuery程式碼使用的變數(shù)並不需事先宣告,變數(shù)是以「$」符號(hào)開始的合法XML名稱。
例如:XQuery變數(shù)範(fàn)例如下:$x、$y、$name、$bookXQuery的文字值(LiteralValues)主要分為三種型態(tài),如下所示:字串(Strings):XQuery的字串可以使用單引號(hào)或雙引號(hào)括起的字元集合。例如:"P679"。數(shù)字(Numbers):XQuery的數(shù)字可以是整數(shù)或浮點(diǎn)數(shù)。例如:1、2、34.5。建構(gòu)型態(tài):使用函數(shù)建構(gòu)的特殊型態(tài)。例如:日期xs:date("2006-12-31")。XQuerySyntax–Variablesand
XQuerySyntax–Comparators(4/5)XQuery1.0和XPath2.0使用相同的運(yùn)算子,XQuery的比較運(yùn)算子分為兩種,如下所示:一般比較(GeneralComparisons):一般比較運(yùn)算子有=、!=、<、<=、>、>=,可以用來(lái)比較多個(gè)項(xiàng)目。值比較(ValueComparisons):值比較運(yùn)算子有eq、ne、lt、le、gt、ge,和一般比較的差異在於它是比較單獨(dú)值,也就是說(shuō),運(yùn)算元一定是原子值(AtomicValue),或包含原子值的節(jié)點(diǎn)。XQuerySyntax–Comparators(
XQuerySyntax–Comments(5/5)在XQuery程式碼可以使用XQuery的註解文字,這是「(:」開頭到「:)」之間的字串,如下所示:(:程式範(fàn)例:Ch7-2-1.xq:)註解文字不可以出現(xiàn)在查詢結(jié)果的XML文件之中。因?yàn)閄Query查詢結(jié)果是一份XML文件或XHTML文件,所以也可以使用XML文件的註解,如下所示:<!--文件範(fàn)例:Bookstore.xml-->XQuerySyntax–Comments(letandreturnclause–letclauseFLWOR運(yùn)算式的let子句可以指定XQuery變數(shù)的值,return子句傳回查詢結(jié)果。FLWOR運(yùn)算式let子句指定XQuery變數(shù)的值可以是項(xiàng)目或循序,如下所示:let$book:=doc("bookstore.xml")//book[1]let$title:="Java2程式設(shè)計(jì)範(fàn)例教本"let$list:=(1to5)let$price:=650letandreturnclause–letcl
letandreturnclause–return(1/4)FLWOR運(yùn)算式return子句可以輸出查詢結(jié)果,如果是路徑運(yùn)算式,就是輸出選取的節(jié)點(diǎn)內(nèi)容,如下所示:let$book:=doc("bookstore.xml")//book[1]return$book/@code$book變數(shù)是第1個(gè)book元素節(jié)點(diǎn),return子句傳回book元素的code屬性節(jié)點(diǎn)。letandreturnclause–reletandreturnclause–return(2/4)如果輸出的XQuery變數(shù)是文字值,我們可以在return子句加上標(biāo)籤來(lái)建立
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 幼兒園員工聘用合同
- 流通經(jīng)銷合同8篇
- 《無(wú)機(jī)化學(xué)》課程標(biāo)準(zhǔn)
- 出口賠償合同范本
- 個(gè)人英語(yǔ)學(xué)習(xí)計(jì)劃
- 三年級(jí)語(yǔ)文下冊(cè)工作總結(jié)
- 南車 合同范本
- 雙方合作生產(chǎn)經(jīng)營(yíng)合同范例
- 廚房工作人員勞務(wù)合同范本
- 會(huì)計(jì)記賬報(bào)稅合同范本
- 2025年廣西職業(yè)院校技能大賽高職組(商務(wù)數(shù)據(jù)分析賽項(xiàng))參考試題庫(kù)資料及答案
- 2025年黑龍江農(nóng)墾職業(yè)學(xué)院?jiǎn)握新殬I(yè)傾向性測(cè)試題庫(kù)帶答案
- 年度得到 · 沈祖蕓全球教育報(bào)告(2024-2025)
- 2025年日歷表(A4版含農(nóng)歷可編輯)
- 南京旅游職業(yè)學(xué)院?jiǎn)握新殬I(yè)技能測(cè)試參考試題庫(kù)(含答案)
- 新視野大學(xué)英語(yǔ)(第四版)讀寫教程4(思政智慧版)課件 Unit1 Urban development Section A
- 2017年度項(xiàng)目生產(chǎn)部工作計(jì)劃推進(jìn)表甘特圖
- 地下室車庫(kù)綜合管線施工布置
- 采購(gòu)訂單模板
- 巴馬格紡絲控制系統(tǒng)軟件說(shuō)明書(共46頁(yè))
- 肺結(jié)核患者管理ppt課件
評(píng)論
0/150
提交評(píng)論