Compartir a través de


Comparisons

A version of this page is also available for

Windows Embedded CE 6.0 R3

4/8/2010

The = sign is used for equality; != is used for 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. For example, "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 < GGG

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 patterns from within scripting languages.

Examples

Find all author elements whose last name is Bob.

author[last-name = "Bob"]

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

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

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

degree[@from != "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"]

Order of Precedence for Comparisons

Comparisons with regard to data types obey the order of precedence.

  • If at least one operand is a Boolean, each operand is first converted to a Boolean.
  • Otherwise, if at least one operand is a number, each operand is first converted to a number.
  • Otherwise, if at least one operand is a date, each operand is first converted to a date.
  • Otherwise, both operands are first converted to strings.

Comparing Dates

If one operand is a node-set and the other is a date, the comparison will be True if and only if there is a node in the node-set operand such that the result of performing the comparison on the date operand and on the result of converting the value of that node to a date using the msxslt:date() function is True.

Two dates are equal only if an unambiguous canonical format for the date (for example, GMT representation as a number) indicates that they are so.

A date is less than another date if it indicates a date/time earlier than the second date/time, and so on.

Binary Comparison Operators

A set of binary comparison operators compares numbers and strings and returns Boolean results. <, <=, >, and >= are used for less than, less than or equal, greater than, and greater than or equal. Single or double quotation marks can be used for string delimiters in expressions. This makes it easier to construct and pass patterns within scripting languages.

Examples

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

author[last-name = "Bob" and price > 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 >= "M"]

Find the first three books (1, 2, 3).

book[index() <= 3]

See Also

Concepts

Sample Data
XPath Examples