What is XQuery?
Estimated study time: 13 minutes.
XQuery is a query language purpose-built for extracting and constructing data from XML documents — often described as "SQL for XML." It builds on top of XPath, adding the ability to combine data from multiple sources and construct new XML output directly.
A Simple Example
for $course in doc("courses.xml")//course
where $course/@level = "beginner"
return $course/title
This reads every course element from courses.xml, filters for beginner-level courses, and returns their titles — a pattern that will feel familiar if you've used SQL's SELECT/WHERE structure.
Why XQuery Exists
XPath alone is great for selecting nodes, but it can't easily combine, restructure, or generate new XML. XQuery fills that gap — letting you query across multiple documents and build a brand-new XML (or HTML) structure from the results.
Core Capabilities
- Query data from one or more XML documents at once.
- Filter, sort, and group results.
- Construct new XML elements directly in the query.
- Perform calculations and aggregations over selected data.
Where XQuery Is Used
XQuery shows up in XML databases, content management systems, and data-integration pipelines where structured XML data needs to be queried the way a relational database would be queried with SQL.