XQuery

From BC$ MobileTV Wiki
Revision as of 06:18, 16 May 2013 by Joe (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

XML Query language (commonly abbreviated XQuery) is a widely implemented, W3C standardized query language for combining documents, databases, Web pages and almost anything else. It is professed to be powerful and easy to learn.

XQuery is replacing proprietary middleware languages and Web Application development languages for processing XML data sources. You can replace a complex Java or C++ XML parser program with a few lines of code. XQuery is simpler to work with and easier to maintain than many other alternatives.


Philosophy

"Do more with less."



Specifications



Example

The acronym FLWOR was given to XQuery statements because of its main keywords:

  • for: describes a document or element set to perform a loop/operation set on (i.e. for $x in doc("books.xml"))
  • let: uses a combination of $ to denote a variable and = as an assignment operator (i.e. let $maxCredit := 3000)
  • where: (optional) conditional statement to say which range or scope of results to return (i.e. where $video/year = 1999)
  • order by: (optional) sequence or arrangement in which to present/list the results (i.e. order by $video/year)
  • return: specifies the values to output as a result set (i.e. return $video/title)

Given the following XML:

<bookstore>
 <book category="COOKING">
   <title lang="en">Everyday Italian</title>
   <author>Giada De Laurentiis</author>
   <year>2005</year>
   <price>30.00</price>
 </book>
 <book category="CHILDREN">
   <title lang="en">Harry Potter</title>
   <author>J K. Rowling</author>
   <year>2005</year>
   <price>29.99</price>
 </book>
 <book category="WEB">
   <title lang="en">XQuery Kick Start</title>
   <author>James McGovern</author>
   <author>Per Bothner</author>
   <author>Kurt Cagle</author>
   <author>James Linn</author>
   <author>Vaidyanathan Nagarajan</author>
   <year>2003</year>
   <price>49.99</price>
 </book>
 <book category="WEB">
   <title lang="en">Learning XML</title>
   <author>Erik T. Ray</author>
   <year>2003</year>
   <price>39.95</price>
 </book>
</bookstore> 

We can get a list of all book titles for books with a price of 30 dollars or less:

for $x in doc("books.xml")/bookstore/book
  where $x/price<30
    order by $x/title
return $x/title


Which results in the following:

 Harry Potter



Tools


Resources


Tutorials


External Links


See Also

XML | XPath