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 |
---|---|
|
Logical-and |
|
Logical-or |
|
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 |
|
Comparisons |
5 |
|
Comparisons |
6 |
|
Union |
7 |
|
Boolean not |
8 |
|
Boolean and |
9 |
|
Boolean or |
When the operators are used in an XML document, such as an XSLT style sheet, the <
and >
tokens must be escaped as <
and >
, 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 <= 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 >= 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 |
---|---|
|
All |
|
All |
Boolean not
The Boolean not
operator negates the value of an expression within a filter pattern.
Examples
Expression | Refers to |
---|---|
|
All |
author[not(degree or award) and publication] |
All |
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: