Welcome to The Coding College! In this tutorial, we’ll explore the syntax of XQuery, the query language designed for querying and transforming XML data. Understanding XQuery’s syntax is the first step toward building powerful queries that extract, manipulate, and present XML content.
Basic XQuery Syntax Structure
An XQuery script typically follows this structure:
- Prolog: Optional declarations (e.g., namespaces, variables, functions).
- Query Body: The main logic for querying or transforming data.
Example:
xquery version "3.1"; (: Optional version declaration :)
declare namespace ns = "http://example.com/library"; (: Prolog :)
declare variable $discount := 0.1;
for $book in doc("books.xml")/library/book (: Query Body :)
where $book/price > 20
return
<discounted-book>
<title>{ $book/title }</title>
<discounted-price>{ $book/price * (1 - $discount) }</discounted-price>
</discounted-book>
Key Syntax Elements in XQuery
1. Comments
XQuery supports comments for better readability.
- Single-line comments:
(: Comment :)
Example:
(: This is a comment :)
for $item in doc("items.xml")/store/item
return $item/name
2. Variables
Variables are defined with a $
prefix and can store nodes, sequences, or atomic values.
Example:
let $price := 29.99
return $price
3. FLWOR Expressions
The FLWOR (For, Let, Where, Order by, Return) structure is the core of XQuery.
Syntax:
for $variable in sequence
let $variable := expression
where condition
order by expression
return result
Example:
for $book in doc("books.xml")/library/book
where $book/price > 20
return $book/title
4. XPath Expressions
XQuery uses XPath to navigate XML documents.
Common XPath Syntax:
/
– Select from the root.//
– Select nodes anywhere in the document.[]
– Filter conditions.@
– Access attributes.
Example:
doc("books.xml")/library/book[@category="Fiction"]
5. Constructing XML
XQuery allows you to create new XML structures.
Example:
<book>
<title>Learning XML</title>
<price>29.99</price>
</book>
6. Operators
XQuery supports a variety of operators:
- Arithmetic Operators:
+
,-
,*
,div
,mod
. - Comparison Operators:
=
,!=
,<
,<=
,>
,>=
. - Logical Operators:
and
,or
,not()
.
Example:
for $book in doc("books.xml")/library/book
where $book/price > 20 and $book/author = "John Doe"
return $book
7. Functions
You can use built-in functions or define your own.
Example of Built-In Function:
count(doc("books.xml")/library/book)
Example of User-Defined Function:
declare function local:discount($price as xs:decimal) as xs:decimal {
$price * 0.9
};
for $book in doc("books.xml")/library/book
return local:discount($book/price)
8. Sequence
Sequences are ordered collections of items.
Example:
(1, 2, 3) (: A sequence of numbers :)
Using Sequences in XQuery:
for $num in (1, 2, 3)
return $num * 2
9. Conditional Expressions
XQuery supports if-then-else
for conditional logic.
Example:
for $book in doc("books.xml")/library/book
return
if ($book/price > 20)
then <expensive>{ $book/title }</expensive>
else <cheap>{ $book/title }</cheap>
10. Sorting with order by
You can sort query results using order by
.
Example:
for $book in doc("books.xml")/library/book
order by $book/price descending
return $book/title
11. Namespace Declarations
Use namespaces to avoid conflicts with XML elements.
Example:
declare namespace lib = "http://example.com/library";
doc("books.xml")/lib:library/lib:book
Common Errors to Avoid
- Incorrect XPath Expressions: Ensure paths are valid and match your XML structure.
- Mismatched Braces: Properly close curly braces
{}
in XQuery expressions. - Invalid FLWOR Structure: Ensure all clauses (e.g.,
for
,return
) are used correctly.
Tools for Practicing XQuery Syntax
Here are some tools to help you write and execute XQuery:
- BaseX: A lightweight XML database with an easy-to-use XQuery editor.
- eXist-db: An XML database with built-in support for XQuery.
- Saxon: A popular XQuery processor with robust support for advanced features.
- Altova XMLSpy: An IDE for XML with XQuery support.
Conclusion
Understanding the syntax of XQuery is essential for working with XML data. With its intuitive structure and powerful querying capabilities, you can extract, transform, and present XML data in countless ways.
For more tutorials and resources on XQuery, visit The Coding College!