Expressions on sequences
Alternatively to path expressions, expressions on sequences are often used in order to realise a filtering.
|provides the current position of the context node within the node sequence currently to be processed|
|provides the number of nodes within the node sequence currently to be processed|
The testing of the position refers to the status of the dynamic context and may be done by the fn:position() function, so that the two following expressions are equivalent:
child::Surgery[fn:position() = 3]
However, the use of functions enables the formulation of mighty predicates, for example the selection of the first two and the last two surgeries of a patient:
child::Surgery[fn:position() < 3 or fn:position() > fn:last()-2]
The fn:last() function indicates the position of the last node in the sequence currently processed.
Often, a test regarding a list of positions is also applied. For example, the following predicate reduces the amount of child nodes on the first, the third and the second last surgery:
child::Surgery[fn:position() = (1, 3 to 5, fn:last()-1)]
Please note here that – contrary to previous versions of the XQuery specification – no implicit comparison test of the ordinal number exists. In the current version, the following expression does not perfom any more what is required:
child::Surgery[(1, 3 to 5, fn:last()-1)]
Because of the determination of the Boolean value of the sequence of atomic values, this expression is equivalent to:
Combination of predicates
In general, a location step may have no predicate at all, a single predicate or a list of predicates which are processed successively. The order of the evaluation of the attributes is not to be neglected since the evaluation of the nth predicate refers to the result of the evaluation of the n-1-th predicate. The following example illustrates this aspect:
/Patient//Surgery[Transplantation][fn:position() = (1 to 5)]
/Patient//Surgery[fn:position() = (1 to 5)][Transplantation]
Whereas the first expression returns the first five surgeries with a transplantation, in the second expression only the first five surgeries are generally taken into account which, in addition, have to include a transplantation in order to be adopted into the result.
Source: "XQuery – Grundlagen und fortgeschrittene Methoden", dpunkt-Verlag, Heidelberg (2004)
|<< back||next >>|