Querying XML Sample Documents

This document provides some examples of common queries using a small example Patient database. In the section Getting Started, you can find a step-by-step description of how to create and load this database in Tamino. To compare your query results with those presented here, load the data for the second patient as described in the section Loading Objects into the Database.

To execute a Tamino query, you can use the Tamino Interactive Interface, or you can enter an HTTP request directly in your browser's address line. If, for example, you have defined a database "mydb-test" on your local computer, and that database contains the collection "Hospital", which in turn contains the schema "patient", you could type the following URL in your browser's address line:

http://localhost/tamino/mydb-test/Hospital/patient?_XQL=/patient/name[surname~='At*']

to return all documents that belong to the schema 'patient' and satisfy the node-selection condition expressed by the query /patient/name[surname~='At*'].

Tamino queries can also be executed using any application environment that can handle HTTP requests, for example scripting languages. For more information, see the list of programming interfaces (APIs) in the Tamino documentation overview page.

The query response is returned by Tamino as an XML object. See the section Syntax of XML Responses in the X-Machine Programming documentation for a complete description.

The following table contains some typical query expressions for the Patient schema. The first column contains the query expression, the second column contains a description of the syntax used in the query, the third column describes the result of the query in general terms, and the fourth column is the query result based on the existing instances of the Patient schema.

Query Expression Description General Retrieval Result Specific Retrieval Result
/patient The path operator / at the beginning of a query indicates that selection starts at the root node. All patient children of the root node. 2 patient nodes ("Atkins" and "Bloggs")
//therapy The path operator // at the beginning of a query indicates selection of a node regardless of its location within the XML object. All therapy descendants of the root node. 2 therapy nodes (for "Atkins" and "Bloggs")
/patient/name/firstname The path operator / indicates the next level in the hierarchy. All firstname nodes that are children of name nodes that in turn are children of a patient node. 2 firstname nodes ("Paul" and "Fred")
/patient/therapy//type The path operator // indicates any number of intervening hierarchy levels. All type nodes that are descendants of therapy nodes that in turn are children of a patient node. 1 type node (for "Bloggs")
/patient[//surname='Atkins'] Equality and relational operators (=, !=, <, >, <=, >=) can be used for comparison operations. All patient nodes that have a descendant surname node with the content "Atkins". 1 patient node ("Atkins")
//therapy/medication/type [@form = 'tablet'] The predicate expression inside [] filters the set of nodes to its left based on the conditions inside the brackets. @ selects the attribute node attached to an element node. All type nodes below a medication node whose attached attribute node form has the string value "tablet". 1 type node (for "Bloggs")
/patient[born < 1960 and //city='Bradford'] Boolean operators (and, or) can be used to express multiple conditions. All patient nodes that have a value less than 1960 for the born element and a value of "Bradford" for any city element below the root node. 1 patient node ("Bloggs")

The following table contains some typical query expressions using X-Query's text retrieval and sorting expressions:

Query Expression Description General Retrieval Result Specific Retrieval Result
//occupation [.~='Professional'] The contains operator ~= performs a selection based on word content. The specified word (case insensitive) is found irrespective of its location within the text node. All occupation nodes that contain the word value "Professional". 1 occupation node (for "Atkins")
/patient/name [surname~='At*'] The contains operator ~= can be used to perform selection using the wildcard character *. All name nodes that have a surname child node whose value contains a word beginning with "At". 1 name node (for "Atkins")
//remarks after therapy The operators before and after can be used to perform selection based on sibling positioning. All descendant remarks nodes that follow a therapy node. 1 remarks node (for "Bloggs")
/patient[born between 1950,1953] The between operator can be used to perform selection based on a value range. All patient nodes that have a born child node whose value is between 1950 and 1953. 1 patient node (for "Bloggs")
/patient[occupation ~= 'professional' adj 'diver'] The adj operator can be used to perform selection based on adjacent value location. All patient nodes that have a child occupation node that contains a word "Professional" immediately followed by the value "diver". 1 patient node ("Atkins")
/patient[occupation ~= 'professional' near 'diver'] The near operator can be used to perform selection based on proximity value location. All patient nodes that have an occupation element that contains a word "professional" immediately followed by the value "diver" (or vice versa). 1 patient node ("Atkins")
//firstname sortby (.) sortby can be used to sort the query results to be sorted in a certain order. All firstname elements sorted by firstname within each document. The firstname elements of the first document ("Atkins") are returned in sorted order, followed by the sorted firstname elements of the second document ("Bloggs").