6️⃣ 🔍 XQuery Tutorial – Learn XML Query Language with FLWOR & Examples
🧲 Introduction – Why Learn XQuery?
XQuery is a powerful language designed specifically for querying and manipulating XML data. Just like SQL works with relational databases, XQuery allows developers to extract, filter, transform, and output structured XML data from databases, files, or APIs. It’s an essential tool for working with large XML datasets, data integration, and backend systems that depend on structured markup.
🎯 In this guide, you’ll learn:
- What XQuery is and how it works
- FLWOR expressions (For, Let, Where, Order by, Return)
- XQuery syntax and common use cases
- HTML output generation with XQuery
- Practical examples and functions
📘 Topics Covered
🔢 Topic | 📄 Description |
---|---|
📖 XQuery Introduction | Overview and purpose of XQuery |
🧾 XQuery Example | Simple, practical query example using XML |
🔁 XQuery FLWOR | Core looping & filtering expression format (For, Let, Where, etc.) |
🌐 XQuery HTML | How to generate HTML from XML using XQuery |
🧠 XQuery Terms | Key concepts like sequences, expressions, and types |
✍️ XQuery Syntax | Standard syntax rules and structure |
➕ XQuery Add | Adding elements or values in an XML dataset |
🔍 XQuery Select | Retrieving specific elements from XML |
🔧 XQuery Functions | Built-in functions like string(), count(), and custom ones |
📖 XQuery Introduction
XQuery stands for XML Query Language and is designed by the W3C to query, extract, and manipulate XML data in a structured and efficient way.
✅ Uses of XQuery:
- Querying XML documents and databases
- Generating reports from XML data
- Transforming XML into different formats (HTML, CSV)
- Filtering or modifying parts of XML files
🧾 XQuery Example
XML Input (books.xml):
<books>
<book><title>XML Basics</title><price>20</price></book>
<book><title>XQuery Mastery</title><price>35</price></book>
</books>
XQuery Output:
for $b in doc("books.xml")//book
where $b/price > 25
return $b/title
✅ This returns only books priced above 25, outputting their titles.
🔁 XQuery FLWOR Expression
FLWOR is the foundation of most XQuery operations:
for $x in doc("data.xml")//item
let $price := $x/price
where $price > 50
order by $price
return $x/name
- For: Iterates over nodes
- Let: Declares variables
- Where: Filters results
- Order by: Sorts output
- Return: Defines the result structure
🌐 XQuery HTML Output
You can generate HTML dynamically from XML content:
for $book in doc("books.xml")//book
return
<div>
<h2>{ $book/title/text() }</h2>
<p>Price: { $book/price/text() }</p>
</div>
✅ This helps display structured data on web pages directly from XML sources.
🧠 XQuery Terms – Key Concepts
Term | Meaning |
---|---|
Sequence | A list of items (nodes, strings, numbers) |
Expression | A query statement that returns a value or result |
Node | Basic unit in XML (element, attribute, text, etc.) |
Path | Navigation path, similar to XPath |
Variable | Temporary storage (e.g., let $x := 10 ) |
✍️ XQuery Syntax Overview
✅ Key Rules:
- XQuery uses XPath expressions for path navigation
- Curly braces
{}
are used to insert dynamic values - You can nest multiple expressions within a return
- Case-sensitive and XML-aware
➕ XQuery Add – Inserting or Modifying
You can add new elements or transform values using:
for $x in doc("inventory.xml")//item
return
<product>
{ $x/name }
<discounted>{ $x/price * 0.9 }</discounted>
</product>
✅ Adds a new <discounted>
field based on price.
🔍 XQuery Select – Retrieving Data
Selecting specific XML nodes:
doc("data.xml")//employee[name="John"]
✅ Retrieves employee elements where name is “John”.
🔧 XQuery Functions
Some common built-in functions:
Function | Purpose |
---|---|
count() | Returns number of nodes |
string() | Converts node to string |
concat() | Concatenates multiple strings |
starts-with() | Checks string prefix |
substring() | Returns part of a string |
fn:avg() | Returns average of numeric values |
✅ You can also define your own functions:
declare function local:double($x) {
$x * 2
};
📌 Summary – Recap & Next Steps
XQuery is a feature-rich language for working with XML datasets. Its SQL-like structure, combined with XPath navigation and functional programming capabilities, makes it a perfect choice for XML processing in enterprise systems.
🔍 Key Takeaways:
- XQuery enables querying, transforming, and outputting XML
- FLWOR expressions are the backbone of XQuery logic
- Generates HTML or reports directly from XML
- Includes robust functions and supports custom logic
⚙️ Real-World Relevance:
XQuery is widely used in enterprise databases (MarkLogic, BaseX), APIs, and document-driven applications for generating reports, data transformation, and intelligent querying of large XML documents.
❓ FAQ – XQuery Tutorial
❓ Is XQuery like SQL?
✅ Yes, XQuery is designed to query XML similar to how SQL queries relational data, using expressions like for
, where
, and return
.
❓ Can XQuery update XML data?
✅ Basic XQuery is read-only, but XQuery Update Facility (an extension) allows you to modify XML content.
❓ Is XQuery still used in modern development?
✅ Yes, especially in enterprise data systems, XML-based APIs, and legacy integrations that use structured XML heavily.
❓ What’s the difference between XPath and XQuery?
✅ XPath is a navigation tool; XQuery is a full query language that includes XPath but adds logic, functions, and output formatting.
Share Now :