Retrieving Objects from the Database Using XQuery

Once you have loaded objects into the database, you can retrieve the objects using XQuery statements. XQuery is the W3C standard query language for structured documents. Tamino allows you to use XQuery for performing queries on XML (and also non-XML) objects. XQuery allows you not only to retrieve database contents but also to compose your query result using constructors.

In addition to XQuery, Tamino still supports the query language X-Query (written with a hyphen) used in former versions of Tamino. See the X-Query User Guide for detailed information.

In the following, some simple examples of query expressions with XQuery are provided to familiarize you with the basic retrieval process. For the examples, use the data you loaded into the database in the preceding sections (Atkins and Bloggs).

The easiest way to query data is to use the XQuery Tool. You can open it from the Tamino X-Plorer.

Start of instruction setTo open the XQuery Tool from the Tamino X-Plorer

  1. In the navigation tree, select the doctype patient.

  2. From the Tools menu, choose Query > XQuery.

    The Tamino XQuery Tool is displayed, and the XQuery code required to return all instances of the selected doctype patient is provided automatically:

    graphics/xquerytool1.png

    The XQuery window contains all information that is required to execute a query: server, database, collection and the query code itself.

    Before you try the examples, please delete the predefined query code, since we will use our own examples.

  3. Under Tools > Options > Module in the XQuery tool, ensure that the box Module management mode is not checked.

Start of instruction setTo find all elements of the type "patient" in the current collection:

  • Enter the following in the XQuery field:

    input()/patient

    Choose the Execute XML Query button in the toolbar.

    The query response is returned by Tamino as an XML object in the result window of the Tamino XQuery Tool. You should get a query result, listing all patients (in this case patients Atkins and Bloggs; to display more information about the patients, expand the plus-sign):

    graphics/xquery1.png

The path operator (/) in the query indicates that the selection starts at the document node (the first node in the tree). The expression "input()" denotes the current collection and returns documents stored in the collection Hospital whose document node is patient.

Start of instruction setTo find all patients born after a certain date, using data type xs:integer:

  • Enter the following in the XQuery field:

    for $a in input()/patient
    where $a/ born > 1960
    return $a

    Choose the Execute XML Query button.

    In the output window, you should get the data from patients born after 1960 (in this case patient Atkins).

    Remember that in the first part of this Getting Started, we put a standard search index on the born element and defined the data type xs:integer for it, so that a quick search and comparison can be performed on integer values. (However, a search would also be performed without the index).

You can construct new elements (in the following example, the element senior-patient is constructed) and combine your results with the following query:

for $a in input()/patient
where $a/born < 1951
return
<senior-patient>
{ $a/name }
{ $a/surname }
</senior-patient>

You should get the following result:

graphics/query3.png

In the return clause, an element constructor is used simply by writing the appropriate tags. As child elements, two expressions are enclosed in braces, which means they need to be evaluated. This query returns a list of senior patients with name and surname (in this case patient Bloggs).

See the XQuery documentation for more information and examples about the XQuery language. For detailed information about the XQuery tool, see the documentation about the Tamino X-Plorer, section Using the Tamino X-Query Tool.