Easy Tutorial
❮ Xquery Reference Xquery Functions ❯

XQuery "Selection" and "Filtering"


XML Example Document

We will continue to use the "books.xml" document in the examples below (the same XML file used in the previous section).

View the "books.xml" file in your browser.


Selecting and Filtering Elements

As seen in the previous sections, we use path expressions or FLWOR expressions to select and filter elements.

Consider the following FLWOR expression:

for Statement

The for statement binds a variable to each item returned by the in expression. The for statement can cause iteration. Multiple for statements can exist within the same FLWOR expression.

To loop a specified number of times in a for statement, you can use the keyword to:

Result:

The keyword at can be used to count iterations:

Result:

Multiple in expressions are also allowed in a for statement. Use commas to separate each in expression:

Result:

let Statement

The let statement performs variable assignment and can avoid repeating the same expression multiple times. The let statement does not cause iteration.

Result:

where Statement

The where statement is used to set one or more conditions (criteria) for the result.

order by Statement

The order by statement is used to specify the order of the results. Here, we want to order the results by category and title:

Result:

return Statement:

The return statement specifies what to return.

Result:

❮ Xquery Reference Xquery Functions ❯