🧪 XQuery Example – Basic Queries to Extract XML Data
🧲 Introduction – Why Learn Through XQuery Examples?
XQuery is best understood by doing. It may look like XML or SQL at first, but its true power comes from seeing how you can navigate, filter, and return specific data from XML documents. In this article, we’ll explore practical XQuery examples that show how to retrieve, format, and output XML content—perfect for beginners getting hands-on with their first queries.
🎯 In this guide, you’ll learn:
- How to write basic XQuery statements
- How to use
for
,where
, andreturn
- Examples of data filtering, value extraction, and formatting
- Real outputs from real XML files
📄 Sample XML: books.xml
<catalog>
<book id="101">
<title>Learn XQuery</title>
<author>Jane Doe</author>
<price>499</price>
</book>
<book id="102">
<title>Advanced XML</title>
<author>John Smith</author>
<price>699</price>
</book>
</catalog>
🧾 Example 1 – Get All Book Titles
for $b in doc("books.xml")//book
return $b/title
✅ Output:
<title>Learn XQuery</title>
<title>Advanced XML</title>
🔍 Example 2 – Filter Books with Price > 500
for $b in doc("books.xml")//book
where $b/price > 500
return $b/title
✅ Output:
<title>Advanced XML</title>
💬 Example 3 – Return Titles as Plain Text
for $b in doc("books.xml")//book
return string($b/title)
✅ Output:
Learn XQuery
Advanced XML
📝 Example 4 – Format Custom Output with Author and Price
for $b in doc("books.xml")//book
return
<info>
<title>{$b/title/text()}</title>
<author>{$b/author/text()}</author>
<price>{$b/price + 100}</price>
</info>
✅ Output:
<info>
<title>Learn XQuery</title>
<author>Jane Doe</author>
<price>599</price>
</info>
🔁 Example 5 – Use let
for Local Variables
for $b in doc("books.xml")//book
let $t := $b/title/text()
let $p := $b/price
return <summary>{$t} costs ₹{$p}</summary>
✅ Output:
<summary>Learn XQuery costs ₹499</summary>
<summary>Advanced XML costs ₹699</summary>
🔤 Example 6 – Sort Books Alphabetically by Title
for $b in doc("books.xml")//book
order by $b/title
return $b/title
✅ Output:
<title>Advanced XML</title>
<title>Learn XQuery</title>
🧠 Example 7 – Condition-Based Output
for $b in doc("books.xml")//book
return
if ($b/price > 500) then
<premium>{$b/title}</premium>
else
<budget>{$b/title}</budget>
✅ Output:
<budget>Learn XQuery</budget>
<premium>Advanced XML</premium>
✅ Best Practices
- ✔️ Use
for
,where
, andreturn
to build FLWOR expressions - ✔️ Wrap output in XML tags for structured returns
- ✔️ Use
let
to store intermediate variables - ✔️ Use XPath within XQuery to drill down into XML nodes
- ❌ Don’t forget
doc("file.xml")
when querying external documents
📌 Summary – Recap & Next Steps
These XQuery examples show how easy it is to query and manipulate XML. With just a few lines, you can extract values, format output, and even perform calculations or apply conditions.
🔍 Key Takeaways:
- XQuery reads and processes XML using familiar looping and filtering logic
- You can return XML, plain text, or dynamic data
- Perfect for working with catalogs, APIs, feeds, and content systems
⚙️ Real-world relevance: Used in dashboards, reporting tools, XML APIs, publishing pipelines, and enterprise data platforms.
❓ FAQs – XQuery Examples
❓ How do I access an XML file in XQuery?
✅ Use doc("filename.xml")
to load the XML content.
❓ Can I return both XML and text?
✅ Yes. XQuery can mix both as output, but XML is the default.
❓ Can I perform math in XQuery?
✅ Absolutely. Use operators like +
, -
, div
, mod
.
❓ What’s the difference between string()
and .text()
?
✅ string()
converts to a string; .text()
extracts text node content.
❓ Can XQuery work across multiple XML files?
✅ Yes. Use multiple doc()
calls or a collection.
Share Now :