Learn Xquery - 10 Code Examples & CST Typing Practice Test
XQuery is a functional, expression-oriented language designed for querying, transforming, and manipulating XML data. It provides powerful tools for navigating hierarchical structures using XPath and is widely used in systems that store or exchange data in XML.
Learn XQUERY with Real Code Examples
Updated Nov 20, 2025
Code Sample Descriptions
XQuery Counter and Theme Toggle
declare variable $count := 0;
declare variable $isDark := false;
declare function local:updateUI() {
concat('Counter: ', $count, '
', 'Theme: ', if ($isDark) then 'Dark' else 'Light')
};
declare function local:increment() {
declare variable $count := $count + 1;
local:updateUI()
};
declare function local:decrement() {
declare variable $count := $count - 1;
local:updateUI()
};
declare function local:reset() {
declare variable $count := 0;
local:updateUI()
};
declare function local:toggleTheme() {
declare variable $isDark := not($isDark);
local:updateUI()
};
(: Simulate actions :)
local:updateUI();
local:increment();
local:increment();
local:toggleTheme();
local:decrement();
local:reset();
Demonstrates a simple counter with theme toggling using XQuery variables and functional expressions.
XQuery Fibonacci Sequence
declare function local:fib($n) {
if ($n = 0) then 0
else if ($n = 1) then 1
else local:fib($n - 1) + local:fib($n - 2)
};
for $i in 0 to 9 return local:fib($i)
Generates first 10 Fibonacci numbers using XQuery recursion.
XQuery Factorial
declare function local:fact($n) {
if ($n = 0) then 1 else $n * local:fact($n - 1)
};
local:fact(5)
Calculates factorial of a number using recursion.
XQuery Prime Checker
declare function local:isPrime($n) {
$n > 1 and every $i in 2 to $n - 1 satisfies ($n mod $i != 0)
};
local:isPrime(13)
Checks if a number is prime using XQuery sequence and every operator.
XQuery Reverse String
string-join(reverse(string-to-codepoints('HELLO'))!codepoints-to-string,'')
Reverses a string using XQuery string functions.
XQuery Multiplication Table
for $i in 1 to 10 return 5 * $i
Generates multiplication table for a number.
XQuery Celsius to Fahrenheit
let $c := 25 return $c * 9 div 5 + 32
Converts Celsius to Fahrenheit.
XQuery Simple Alarm
let $temp := 80 let $thresh := 75 return if ($temp > $thresh) then 'Alarm: Temp Too High!' else 'Temp Normal'
Simulates an alarm if threshold exceeded.
XQuery Random Walk Simulation
declare variable $steps := 10;
declare variable $pos := 0;
for $i in 1 to $steps return ($pos := $pos + (if (fn:random-number() < 0.5) then -1 else 1), $pos)
Simulates a 1D random walk using XQuery random numbers.
Frequently Asked Questions about Xquery
What is Xquery?
XQuery is a functional, expression-oriented language designed for querying, transforming, and manipulating XML data. It provides powerful tools for navigating hierarchical structures using XPath and is widely used in systems that store or exchange data in XML.
What are the primary use cases for Xquery?
Querying XML databases (e.g., BaseX, eXist-db, MarkLogic). Transforming XML documents. Integrating heterogeneous data sources. Building APIs and services that output XML/JSON. Metadata processing and document-driven workflows
What are the strengths of Xquery?
Excellent for complex XML transformations. Human-readable functional syntax. High performance with native XML databases. Supports both querying and document creation. W3C standard with wide ecosystem support
What are the limitations of Xquery?
Niche outside XML-heavy domains. Not ideal for general-purpose programming. Learning curve for FLWOR + XPath combination. Performance can vary on non-native engines. Limited community compared to mainstream languages
How can I practice Xquery typing speed?
CodeSpeedTest offers 10+ real Xquery code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.