XQuery Terms

Understanding the fundamental terminology of XQuery

📚 What are XQuery Terms?

XQuery terms are the fundamental building blocks and vocabulary used in XQuery language. Understanding these terms helps you write effective queries to extract and manipulate XML data efficiently.


(: Simple XQuery expression :)
for $book in doc("books.xml")//book
return $book/title
                                    

Essential XQuery Terms

XQuery uses specific terminology to describe its components and operations. These terms form the foundation of understanding how XQuery works with XML documents.

📝

Expression

A unit of code that returns a value

5 + 3
🔍

Path Expression

Navigates through XML structure

//book/title
🔄

FLWOR

For, Let, Where, Order by, Return

for $x in //book
return $x
🎯

Predicate

Filters nodes based on conditions

//book[@price < 30]

🔹 Node Terms

XQuery works with different types of nodes in XML documents:

<!-- Sample XML -->
<book id="101">
    <title>XQuery Guide</title>
    <author>John Doe</author>
</book>
  • Element Node: <book>, <title>, <author>
  • Attribute Node: id="101"
  • Text Node: "XQuery Guide", "John Doe"
  • Document Node: The root of the XML tree

🔹 Sequence Terms

XQuery operates on sequences of items:

(: A sequence of numbers :)
(1, 2, 3, 4, 5)

(: A sequence of strings :)
("apple", "banana", "cherry")

(: Empty sequence :)
()

Sequence: An ordered collection of zero or more items. Items can be nodes or atomic values like numbers and strings.

🔹 Variable Terms

Variables store values for reuse in queries:

(: Declaring variables :)
let $price := 29.99
let $quantity := 5
let $total := $price * $quantity
return $total

(: Output: 149.95 :)

Variable: Starts with $ symbol and stores values. Use let to bind values to variables.

🔹 Function Terms

Functions perform operations and return results:

(: Built-in functions :)
count(//book)
sum(//book/@price)
string-length("Hello")

(: Custom function :)
declare function local:greet($name) {
  concat("Hello, ", $name)
};

local:greet("World")

🔹 Axis Terms

Axes define node relationships in XML tree:

(: Different axes :)
child::book          (: Direct children :)
descendant::title    (: All descendants :)
parent::catalog      (: Parent node :)
ancestor::*          (: All ancestors :)
following-sibling::* (: Next siblings :)

🧠 Test Your Knowledge

What does FLWOR stand for in XQuery?