XML — XQuery
What is XQuery?
XQuery is a query language designed for XML data, similar to SQL for databases.
FLWOR Expression
for $book in doc("books.xml")//book
where $book/price > 30
order by $book/title
return $book/title
FLWOR Components
| Keyword | Description |
|---|---|
| for | Iterate over nodes |
| where | Filter conditions |
| let | Assign variables |
| order by | Sort results |
| return | Output expression |
Basic Queries
// Return all book titles
doc("books.xml")//book/title
// Return books with price > 30
doc("books.xml")//book[price > 30]
// Count books
count(doc("books.xml")//book)
String Functions
contains(//title, "XML")
starts-with(//title, "Learning")
substring(//title, 1, 5)
Constructing XML
<results>
{
for $book in doc("books.xml")//book
return <book>
<title>{$book/title/text()}</title>
</book>
}
</results>
XQuery vs XPath
| Feature | XQuery | XPath |
|---|---|---|
| Complexity | Full language | Expression only |
| Logic | Conditional, loops | Limited |
| Construction | Can build XML | Can't build XML |
Mini Practice
- Write a FLWOR expression
- Filter and sort results
- Use string functions
- Construct XML output
Up Next
Continue with XSLT — transforming XML documents.
Related Topics
Frequently Asked Questions about XQuery
What is XQuery in XML?
XQuery is a fundamental concept in XML. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn XQuery?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn XQuery.
Why is XQuery important in XML?
XQuery is essential for XML development. Understanding this concept will help you write better code and solve real-world problems more effectively.