The predicate evaluation
The following pages are from the German book "XQuery – Grundlagen und fortgeschrittene Methoden" (XQuery – basics and advanced methods). The "Hochwaldklinik.xml" file serves as universal example scenario for the explanation of the presented XPath expressions. The file illustrates the administration of a hospital ("Hochwaldklinik") with all its institutions, employees and patients.
Predicate evaluation via context positions
If the predicate expression is an atomic value of a numerical data type, the expression is evaluated to true in case the position indication of the element correponds to the numerical value of the predicate. In the example scenario of the "Hochwaldklinik.xml" file, the following location step provides the entry of the third surgery in case the context node refers to a patient:
child::Surgery[3]
Predicate evaluation via the determination of the effective Boolean value
For all the other expressions formulating a predicate the effective Boolean value is determined for each node and the node is adopted in the result in case the value is true. Interestingly, each XQuery expression can be used as a predicate, whereby path expressions and functions are often used on sequences.
If a predicate is specified in a location step by a path expression, the path expression is evaluated as context node with the node to be currently tested. The following location step with a filter condition regarding the role of the doctor may serve as first example:
child::Doctor[attribute::Role = "Assistant"]
child::Doctor[@Role = "Assistant"]
For each Doctor the attribute:: axis (implicitly given by the abbreviation via the @ symbol) is searched for attributes with the name Role and the value is compared with the appropriate character string. The effective Boolean value results from the comparison with the character string. Path expressions in predicates of location steps can also be used for tests on the existence of attributes or elements from the point of view of the node currently to be processed. In the following, for example, the Doctor node is only included in the result of the current location step if a Role attribute exists:
child::Doctor[@Role]
Likewise, the testing for the existence of an element may be done as relative as well as absolute path expression. So, in the following example, Doctor nodes (as children of the context node) are only adopted in the result of the location step if there is an Anaesthesia entry in the respective procedure:
child::Doctor[parent::*/child::Anaesthesia]
child::Doctor[../Anaesthesia]
The testing for the existence of an element occurs very often when formulating queries and can be simulated by using axes and the successive evaluation of location steps. So, the following path expression provides all nodes with the name "PostalCode" of addresses of in-patients, with the addition that a node with the name "Name" must exist in parallel to the node with the name "Address".
/Patient/In-patient/Name/parent::*/Address/PostalCode
/Patient/In-patient/Name/../Address/PostalCode
This "detour" via the "Name" node realises a test on existence, since nodes with the name "Address" can only be found based on the results of the preceding location steps. If an in-patient has no "Name" node, the result of the appropriate location step is an empty node sequence and the related address information cannot be called up in the following steps.
Source: "XQuery – Grundlagen und fortgeschrittene Methoden", dpunkt-Verlag, Heidelberg (2004)
<< back | next >> |