Compartilhar via


Boolean, Comparison, and Set Expressions

Filter patterns can contain Boolean expressions, comparison expressions, and set expressions. Shortcuts listed in the following table represent alternative symbols that are provided in this XSL Transformations (XSLT) implementation. This documentation discusses these expression operators.

Operator Description

and

Logical-and

or

Logical-or

not()

Negation

=

Equality

!=

Not equal

< *

Less than

<= *

Less than or equal

> *

Greater than

<= *

Greater than or equal

|

Set operation; returns the union of two sets of nodes

* Extended XPath method

The World Wide Web Consortium (W3C) syntax for operator keywords uses white space and other separators rather than the dollar sign character ($) used in version 2.5. In the W3C syntax, a binary keyword of the form $xxx$ can be expressed as wsxxxws, where ws refers to a token terminator that can be white space, single quote characters ('), or double quote characters ("). Unary operators such as not() use functional notation. Although the Microsoft implementation supports both syntaxes, it is recommended that the W3C syntax be used for future compatibility.

Precedence order (from highest to lowest) for comparison operators and Boolean operators is shown in the following table.

Precedence Operators Description

1

( )

Grouping

2

[ ]

Filters

3

/

//

Path operations

4

< or &lt;

<= or &lt;=

> or &gt;

>= or &gt;=

Comparisons

5

=

!=

Comparisons

6

|

Union

7

not()

Boolean not

8

And

Boolean and

9

Or

Boolean or

When the operators are used in an XML document, such as an XSLT style sheet, the < and > tokens must be escaped as &lt; and &gt;, respectively. For example, the following XSLT instruction invokes an XSLT template rule on all <book> elements whose <price> element has a numeric value less than or equal to 10.

<xsl:apply-templates select="book[price &lt;= 10]"/>

When an XPath expression is used with DOM, the < and > operators need not to be escaped. For example, the following JScript statement selects all <book> elements whose <price> element has a numeric value less than or equal to 10.

var cheap_books = dom.selectNodes("book[price <= 10]");

Boolean expressions can match all nodes of a particular value or all nodes with nodes in particular ranges. The following is an example of a Boolean expression that returns false.

1 &gt;= 2

Operators are case-sensitive.

Logical-and and Logical-or

The Boolean operators and and or perform logical-and and logical-or operations, respectively. These operators, in conjunction with grouping parentheses, can be used to build sophisticated logical expressions.

Examples

Expression Refers to

author[degree and award]

All <author> elements that contain at least one <degree> element and at least one <award> element.

author[(degree or award) and publication]

All <author> elements that contain at least one <degree> or <award> element, and at least one <publication> element.

Boolean not

The Boolean not operator negates the value of an expression within a filter pattern.

Examples

Expression Refers to

author[degree and not(publication)]

All <author> elements that contain at least one <degree> element, but contain no <publication> elements

author[not(degree or award) and publication]

All <author> elements that contain at least one <publication> element, but do not contain any <degree> elements or <award> elements.

Example

XML File (test.xml)

XSLT File (test.xsl)

The following XSLT stylesheet selects all the <x> elements without any attributes.

Output

The transformation, when applied to the XML file given above yields the following result: