This chapter explains what XQuery is and introduces you to the heart of XQuery: the FLWOR expression.
XQuery is a superset of XPath. If you are not already familiar with XPath (in particular XPath 3.x), it is recommended to first read through the XPath 3.0 and 3.1 Training before proceeding with this training.
XQuery stands for XML Query Language.
The 'X' in XQuery comes from its roots in XML, the eXtensible Markup Language.
The 'Query' in XQuery comes from the fact that XQuery is used to both query and transform XML data.
XQuery is a superset of XPath, meaning that every valid XPath expression is also a valid XQuery expression.
Because XQuery is a superset of XPath it can also do more, including on the fly manipulation and construction of XML documents.
The heart of XPath is the FLWOR expression (pronounced 'FLOWER') . To those not familiar with XQuery, a FLWOR expression is very similar to an SQL Select statement.
The three examples below demonstrate the similarities between SQL and XQuery, and show how XQuery is used to both query and transform XML data.
example: SQL SELECT Statement - query database data
SELECT name FROM person WHERE age > 30;
'john' 'peter'
example: XPath FLWOR Expression - query XML data
for $i in /people/person where $i/age > 30 return data($i/name)
'john' 'peter'
example: XPath FLWOR Expression - query and transform XML data
<html> <body> <table> { for $i in /people/person where $i/age > 30 return <tr><td> { data($i/name) } </td></tr> } </table> </body> </html>
<html> <body> <table> <tr> <td>john</td> </tr> <tr> <td>peter</td> </tr> </table> </body> </html>
There are three versions of XQuery:
There was no XQuery version 2 . XQuery version 3.0 followed XQuery 1 so that the version number of XQuery would remain aligned with the version number of the XPath specification (which was already version 3.0 at the time).
The latest incarnation of the XQuery specification XQuery 3.1 is comprised of three documents which specify the syntax, data model, and operators and functions respectively: