Last updated
XQuery Tester Examples
The XQuery Tester lets you evaluate XQuery expressions against XML documents in real time. Below are practical examples covering FLWOR expressions, path queries, constructors, and multi-document joins.
Sample XML Document
Use this XML as input for the examples below:
<library>
<book id="1" genre="fiction">
<title>The Great Gatsby</title>
<author>F. Scott Fitzgerald</author>
<year>1925</year>
<price>12.99</price>
</book>
<book id="2" genre="science">
<title>A Brief History of Time</title>
<author>Stephen Hawking</author>
<year>1988</year>
<price>15.50</price>
</book>
<book id="3" genre="fiction">
<title>1984</title>
<author>George Orwell</author>
<year>1949</year>
<price>9.99</price>
</book>
<book id="4" genre="science">
<title>The Selfish Gene</title>
<author>Richard Dawkins</author>
<year>1976</year>
<price>11.00</price>
</book>
</library>
Basic FLWOR Expression
Select all book titles using a For-Let-Where-Order-Return expression:
for $book in /library/book
return $book/title/text()
Output:
The Great Gatsby
A Brief History of Time
1984
The Selfish Gene
FLWOR with Where Filter
Return only fiction books:
for $book in /library/book
where $book/@genre = "fiction"
return <result>{ $book/title/text() }</result>
Output:
<result>The Great Gatsby</result>
<result>1984</result>
FLWOR with Order By
Sort books by price ascending:
for $book in /library/book
order by xs:decimal($book/price)
return <book price="{ $book/price/text() }">{ $book/title/text() }</book>
Output:
<book price="9.99">1984</book>
<book price="11.00">The Selfish Gene</book>
<book price="12.99">The Great Gatsby</book>
<book price="15.50">A Brief History of Time</book>
Let Clause and Aggregation
Calculate the total price of all books:
let $books := /library/book
let $total := sum($books/price)
return <total>{ $total }</total>
Output:
<total>49.48</total>
Element Constructor
Build a new XML structure from the query results:
element catalog {
for $book in /library/book
return element entry {
attribute id { $book/@id },
element name { $book/title/text() },
element writer { $book/author/text() }
}
}
Output:
<catalog>
<entry id="1"><name>The Great Gatsby</name><writer>F. Scott Fitzgerald</writer></entry>
<entry id="2"><name>A Brief History of Time</name><writer>Stephen Hawking</writer></entry>
...
</catalog>
Local Function Definition
Define a reusable function and call it in the query:
declare function local:discount($price as xs:decimal) as xs:decimal {
$price * 0.9
};
for $book in /library/book
return <discounted title="{ $book/title/text() }" price="{ local:discount(xs:decimal($book/price)) }"/>
Namespace Declaration
Declare and use a namespace prefix in a query:
declare namespace lib = "http://example.com/library";
for $book in /library/book
return <lib:item>{ $book/title/text() }</lib:item>
XQuery Update — Insert Node
Insert a new element into an existing book node:
insert node <inStock>true</inStock>
into (/library/book[@id="1"])[1]
After the update, book id=1 will contain an additional <inStock>true</inStock> child element.
XQuery Update — Delete Node
Remove all books priced above 14:
for $book in /library/book
where xs:decimal($book/price) > 14
return delete node $book
Common Use Cases
- Querying XML configuration files for specific settings
- Transforming XML data into a different structure for downstream processing
- Aggregating values from XML reports (sums, counts, averages)
- Joining data from multiple XML documents in a single query
- Updating XML documents in place using XQuery Update Facility
- Learning XQuery syntax with immediate feedback before deploying to BaseX or eXist-db
- Debugging complex FLWOR expressions step by step
The XQuery Tester provides a full evaluation environment with real-time results, error reporting with line and column positions, and a library of starter examples — no XML database installation required.