# XQuery 3 Training

PreviousHomeNextThis 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](../xpath3.md) 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

/images/training/xquery/person_table_db.pngSELECT name
FROM person
WHERE age > 30;'john'
'peter'

- This example demonstrates how an **SQL statement** is used to query data in a database table.
- The image above shows the contents of a **database table** named **person** which consists of columns named **id**, **name** and **age**.
- The **SQL Select Statement** selects the value in the **name** column from those records in the **person** table where **age** is greater than **30**.

example: XPath FLWOR Expression - query XML data

/images/training/xquery/people.pngfor $i in /people/person
where $i/age > 30
return data($i/name)'john'
'peter'

- This first XQuery example demonstrates how XQuery is used to **query** XML data.
- The image above shows the contents of an XML file named **people.xml** which consists of a sequence of **person** elements.
- The XQuery FLWOR expression selects the value of the **name** child element from those **person** elements where **age** is greater than **30**.

example: XPath FLWOR Expression - query and transform XML data

/images/training/xquery/people.png<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>

- This second XQuery example demonstrates how XQuery can be used to both **query and transform** XML data.
- In this example **direct element constructors** are used to create the html tags (elements) which appear in the result.
- The content of the **td** element is generated by evaluating the **expression** contained within curly brackets i.e. **{** and **}**.
- An expression contained within **{** and **}** is known as an **enclosed expression**.

There are three versions of XQuery: 

- XQuery 1.0 became a W3C Recommendation on January 23, 2007.
- XQuery 3.0 became a W3C Recommendation on April 8, 2014.
- XQuery 3.1 became a W3C Recommendation on December 18, 2014.

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: 

- [XQuery 3.1: An XML Query Language](http://www.w3.org/TR/xquery-31/)
- [XQuery and XPath Data Model 3.1](http://www.w3.org/TR/xpath-datamodel-31/)
- [XPath and XQuery Functions and Operators 3.1](http://www.w3.org/TR/xpath-functions-31/)

PreviousHomeNext