XQuery is a language for XML data.XQuery syntax includes and extends XPaths 2.0 expressions and makes it possible to perform complex queries against on XML data source.
An XQuery path is based on the XPath language and describes the location of a node in an XML document. Paths can be absolute (describing the location of the node by traversing the XML tree from the root element) or relative (describing the location of a node relative to a previously identified node). The examples in the following table show some simple XQuery paths.
Example path |
Description |
|
|
/InvoiceList/Invoice |
All <Invoice> elements immediately contained within the root <InvoiceList> element |
(/InvoiceList/Invoice) [2] |
The second <Invoice> element within the root <InvoiceList> element |
(InvoiceList/Invoice/@InvoiceNo) [1] |
The InvoiceNo attribute of the first <Invoice> element in the root <InvoiceList> element |
(InvoiceList/Invoice/Customer/text())[1] |
The text of the first <Customer> element in an <Invoice> element in the <InvoiceList> root element |
/InvoiceList/Invoice[@InvoiceNo=1000] |
All <Invoice> elements in the <InvoiceList> element that have an InvoiceNo attribute with the value 1000 |
FLOWR statements |
The XQuery language specification includes for, let, order by, where, and return statements, commonly known as FLOWR (pronounced “flower”) statements. SQL Server 2005 supports the for, where, and return statements, which are described in the following table.
Statement |
Description |
|
|
for |
Used to iterate through a group of nodes at the same level in an XML document. |
where |
Used to apply filtering criteria to the node iteration. The XQuery language includes functions such as count that can be used with the where statement. |
return |
Used to specify the XML returned from within an iteration. |