Microsoft XML 2.5 SDK


 

Comparisons

[This is preliminary documentation and subject to change.]

The = sign is used for equality; != is used for inequality. Alternatively, $eq$ and $ne$ can be used for equality and inequality.

Two operands are supplied for a comparison operation: the vector or scalar that is to be compared (the left side value, or lvalue), and the scalar value (the right side value, or rvalue) against which to compare. The rvalue must either be a scalar or a value that can be cast at run time to a scalar.

If the lvalue of a comparison is a set, "any" semantics are used for the comparison operators. That is, the result of a comparison is true if any item in the set meets the condition.

The lvalue of an expression cannot be a literal. That is, "3" = a is not allowed. If the rvalue is an attribute, the text(lvalue) is compared to the text(rvalue).

All elements and attributes are strings, but are automatically cast as integer values for numeric comparisons. Literal numeric values are cast to long or double types during comparison operations as shown in the following table.

Literal type Comparison Example
String text(lvalue) op text(rvalue) a < foo
Integer (long) lvalue op (long) rvalue a < 3
Real (double) lvalue op (double) rvalue a < 3.1

Single or double quotation marks can be used for string delimiters in expressions. This makes it easier to construct and pass XSL patterns from within scripting languages.

When comparing element values, the value() method is implied. That is, last-name < "M" implies last-name!value() < "M".

There are also case-insensitive versions of the = ($eq$) and != ($ne$) operators: $ieq$ and $ine$.

Examples

Find all author elements whose last name is Bob:

author[last-name = "Bob"]
author[last-name $eq$ "Bob"]

Find all author elements where the first last-name is Bob:

author[last-name[0] = "Bob"]

Find all authors where the from attribute is not equal to "Harvard":

degree[@from != "Harvard"]
degree[@from $ne$ "Harvard"]

Find all authors where the last name is the same as the /guest/last-name element:

author[last-name = /guest/last-name]

Find all authors whose text is "Matthew Bob":

author[. = "Matthew Bob"]

Binary Comparison Operators

A set of binary comparison operators compares numbers and strings and returns Boolean results. $lt$, $le$, $gt$, and $ge$ are used for less than, less than or equal, greater than, and greater than or equal. These same operators are also available in a case-insensitive form: $ilt$, $ile$, $igt$, and $ige$.

Single or double quotation marks can be used for string delimiters in expressions. This makes it easier to construct and pass XSL patterns within scripting languages.

The Microsoft implementation supports the symbols <, <=, >, and >= as valid shortcuts for $lt$, $le$, $gt$, and $ge$, respectively.

Examples

Find all author elements whose last name is "Bob" and whose price is > 50:

author[last-name = "Bob" and price $gt$ 50]

Find all authors where the from attribute is not equal to "Harvard":

degree[@from != "Harvard"]

Find all authors whose last name begins with "M" or greater:

author[last-name $ge$ "M"]

Find all authors whose last name begins with "M", "m", or greater:

author[last-name $ige$ "M"]

Find the first three books (0, 1, 2):

book[index() $le$ 2]

See Also

Sample Data, XSL Pattern Examples