Key XQuery Terms

Welcome to The Coding College! If you’re exploring XQuery, understanding the essential terms is crucial to harness its power for querying and transforming XML data. In this guide, we’ll break down the most important XQuery concepts and terminologies, making it easy for you to build a solid foundation.

1. XQuery

XQuery is a query language designed to query, extract, and transform data from XML documents. It is widely used for databases, web services, and applications that rely on structured XML data.

2. FLWOR Expression

The backbone of XQuery, FLWOR stands for:

  • For: Iterates over a sequence.
  • Let: Assigns values to variables.
  • Where: Filters data based on conditions.
  • Order by: Sorts data.
  • Return: Specifies the output.

Example:

for $book in doc("books.xml")/library/book
where $book/price < 30
return $book/title

3. XPath

XPath is a language for navigating and selecting specific parts of an XML document. XQuery uses XPath expressions extensively.

Example XPath Expression:

/library/book/title

This selects all <title> elements under <book> in the <library> node.

4. Node

In XQuery, an XML document is treated as a tree of nodes. These nodes represent various parts of the XML document:

  • Element Node: XML elements like <book>.
  • Attribute Node: Attributes like id="123".
  • Text Node: Content inside elements.

5. Sequence

A sequence in XQuery is an ordered collection of items. Items can include nodes, atomic values (like strings or numbers), or other sequences.

Key Properties of Sequences:

  • Sequences can be empty: ()
  • A single item is also considered a sequence.
  • Multiple items are separated by commas: (1, 2, 3)

6. Variable

Variables in XQuery store data and are prefixed with a $ symbol.

Example:

let $price := 29.99
return $price

7. Context Item

The context item (referred to as .) represents the current node or value in a sequence.

Example:

doc("books.xml")/library/book[.//price < 30]

Here, . refers to the current <book> node being processed.

8. XPath Axes

Axes are used in XPath to navigate through an XML tree. Some common axes are:

  • child: Selects child nodes.
  • parent: Selects the parent node.
  • descendant: Selects all descendant nodes.
  • ancestor: Selects all ancestor nodes.
  • following-sibling: Selects all sibling nodes after the current node.

9. Expression

An expression in XQuery performs an operation and evaluates to a value. Expressions can include FLWOR expressions, XPath, functions, and operators.

Example:

$book/price * 0.9

This multiplies the price by 0.9 (e.g., applying a discount).

10. Function

XQuery includes many built-in functions for working with sequences, strings, numbers, dates, and more. You can also define your own functions.

Example of Built-in Function:

count(doc("books.xml")/library/book)

This counts the number of <book> elements in the XML document.

Example of User-Defined Function:

declare function local:discount($price as xs:decimal) as xs:decimal {
  $price * 0.9
};

11. Namespaces

XML documents often use namespaces to avoid element name conflicts. XQuery can work with namespace-prefixed elements.

Example:

declare namespace ns = "http://example.com/library";
doc("books.xml")/ns:library/ns:book

12. Predicate

A predicate is an expression inside square brackets ([]) used to filter nodes or values.

Example:

/library/book[price < 30]

This selects all <book> elements with a <price> less than 30.

13. Path Expression

A path expression specifies a path through an XML document to select nodes. It can use absolute or relative paths.

Examples:

  • Absolute Path: /library/book
  • Relative Path: book/price

14. XML Data Model (XDM)

XQuery operates on the XML Data Model (XDM), which represents XML documents as trees of nodes and sequences of values.

15. Atomic Values

Atomic values are single, indivisible data values like numbers, strings, or dates.

Example:

  • String: "Hello World"
  • Number: 42
  • Date: 2024-12-27

16. Constructor

Constructors create new XML nodes in XQuery.

Example:

<book>
  <title>Learning XML</title>
</book>

17. Types

XQuery supports XML Schema data types, such as:

  • xs:string
  • xs:integer
  • xs:decimal
  • xs:date

18. Output Formats

XQuery can generate various output formats:

  • XML: Standard output.
  • HTML: For web applications.
  • Text: Plain text.

19. Prolog

The prolog appears at the beginning of an XQuery script and contains declarations like namespaces, variables, and functions.

Example:

declare namespace ns = "http://example.com/library";
declare variable $discount := 0.1;

20. Module

XQuery scripts can be organized into reusable modules. Modules store functions, variables, and namespaces.

Example:

module namespace lib = "http://example.com/library";
declare function lib:discount($price as xs:decimal) as xs:decimal {
  $price * 0.9
};

Conclusion

By mastering these XQuery terms, you’ll be better equipped to query and manipulate XML data effectively. For more tutorials, real-world examples, and deep dives into XQuery, visit The Coding College!

Leave a Comment