BetweenExpr

Select nodes based on a value range.


Syntax

BetweenExpr

SequenceExpr | SequenceExpr (between | betw) Number, Number |  SequenceExpr (between | betw) Literal, Literal

Description

You can select nodes based on a value range, with the limiting values separated by a comma. The range is inclusive of the limiting values. If the first value is larger than the second, the values are implicitly exchanged. The comparison depends on the datatype of the node. If you compare a string value (Literal) with a string range, then a lexical comparison is performed using the lexicographical order. If you compare a string value with a numeric range or a numeric value with a string range, then any string values are implicitly converted to numbers and a numeric comparison takes place. If the string cannot be converted to a numeric value, then the conversion yields the special value "NaN" and the comparison fails.

You can think of BetweenExpr as a convenient abbreviation for logical conjunctions of RelationalExpr, see the compatibility note for an example.

You can use betw as a shorthand for between. However, both variants must be input in lower case.

Compatibility

There is no BetweenExpr defined in XPath. For numeric values, you can simulate this by using relational expressions. The second example below could be rewritten as:

//doctor[@pager >= 2 and @pager <= 5]

As RelationalExpr restricts comparison to numeric values, you cannot in general formulate an equivalent XPath expression for the first example.

Number and Literal correspond to the respective XPath expressions, as defined in the Section 3.7, Rules 30 and 29.

Examples

  • Select all doctors whose pager numbers start with "2", "3", "4" (starting from the root node select all descendant doctor nodes that satisfy the following predicate expression: the string value of the pager attribute is lexicographically ordered between "2" and "5"):

    //doctor[@pager between '2','5']
  • Select all doctors whose pager numbers are between 2 and 5 (all doctor nodes that satisfy the following predicate expression: the string value of the pager attribute implicitly converted to a number falls into the interval [2,5]). With our example data, this yields an empty node-set.

    //doctor[@pager between 2,5]
  • Select all doctors whose pager numbers are between 2 and 5 (all doctor nodes that satisfy the following predicate expression: the string value of the pager attribute explicitly converted to a number falls into the interval [2,5]). As before, this yields an empty node-set.

    //doctor[number(@pager) between 2,5]
  • Select all patients born in the 1950's (all patient nodes that satisfy the following predicate expression: there is an immediate child element born whose numeric value is between 1950 and 1959):

    /patient[born between 1959,1950]

Tip:
Use BetweenExpr instead of AndExpr where possible. Usually a query such as //patient[born between 1950, 1953] is much more efficient than the equivalent query expression //patient[born > 1949 and born < 1954].

Related Expressions

SequenceExpr