Chapter 6. XQuery

Table of Contents

Basics of XQuery
Dynamic constructors
Iteration: FLWOR
Ordering
Variables
Quantified Expressions
Functions
Modifying XML documents

The XQuery for XML is like the SQL for the relational databases, could be used to query and modify data. It became a W3C recommendation in 2006. It is intended to query anything which could be represented as an XML data. Essentially, it contains a data model and the query operators working on it.

XQuery is a truly declarative (and not a procedural) language specifically designed for the purpose of querying XML data. While in procedural languages we need to describe the algorithm to solve a given problem, in declarative languages we need to define conditions and restrictions while the execution engine need to solve the problem. While declarative languages are higher in abstraction as procedural languages, in the case of XQuery we could seen a hybrid approach.

It has programming features like explicit iteration,variable binding, recursion and functions, and also has dynamic features like filtering, grouping and join. In XQuery, the query examines the document (or a part of it) using the restrictions ( as filtering, grouping, predicates) and returns all the nodes which are fulfilling the specified rules. That result is a subset of the original XML document in XML format. Technically, XQuery could be used everywhere XML could be. While XML tries to became an universally interpretable data source which is independent from any language or system, therefore XQuery tries to became an universal query language for it.

Both XPath and XQuery are using the same functions and operators on the same data model. Technically, an XQuery query may use XPath expressions but it can be extended with conditions, iterations , variables and functions.

Preliminaries

The most important fact that XQuery is not written in XML but could be used in XSL stylesheets and could be included in HTML files. It has seven different node types: element, attribute, namespace, processing instruction, comment and document.

Basics of XQuery

Some basic rules::

  • Like XML, XQuery is a case-sensitive language. Keywords in XQuery use lower-case characters and are not reserved—that is, names in XQuery expressions are allowed to be the same as language keywords with some exceptions.

  • The name of a node is a value of type xs:QName.

  • Variables can be defined by the let keyword and could be referenced by the $ sign, e.g.: let $variable.

  • Conditional expression has the following syntax:

    if(…)
    then
    else ...
  • General comparison is made by standard arithmetic operators, like =, !=, >, <=

  • Value comparison operators: eq, ne, gt, lt

    2 gt 1.0               => true()
    <x>42</x> eq <y>42</y> => true()
    (0,1) eq 0             => (type error)
  • Node comparison: is - identity, document order - <<, >>

    <x>42</x> eq <x>42</x> => true()
    <x>42</x> is <x>42</x> => false()
    let $a := <x><y/></x>
    return $a << $a/y      => true()
  • Arithmetic: normal operators as natural: +, -, *, div, idiv

    Note: the operator first normalizing the values of the operands and after that converts to the proper type. If any of the operands is an empty sequence, the result will be the same: ().

    <x>1</x> + 41 => 42.0
    () * 42       => ()
    (1,2) - (2,3) => (type error)
    x-42          => ./child::x-42 (use x - 42)
    x/y           => ./child::x/child::y (use x div y)

  • Data types could be checked with built-in operators: the instance of returns true if the element has the given type:

    patient[id=2]/year instance of xs:integer

    The typeswitch/case / default language tool could be used to make decisions based on types:

    let $str := "fertőző"
    let $patient := element {$str} {}
    // only works on element nodes, so the upper two statement creates it
    typeswitch($patient)
       case element(intenzív) return "sürgős"
       case element(fertőző) return "elkülönítendő"
       default return "paciens"