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)

<?xml version="1.0"?>
<test>

    <x a="1">
      <x a="2" b="B">
        <x>
          <y>y31</y>
          <y>y32</y>
        </x>
      </x>
    </x>

    <x a="2">
      <y>y2</y>
    </x>

    <x a="3">
      <y>y3</y>
    </x>

</test>

XSLT File (test.xsl)

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

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

<!-- suppress text nodes not covered in subsequent template rule -->
<xsl:template match="text()"/>

<xsl:template match="*">
   <xsl:element name="{name()}">
      <xsl:apply-templates select="*|@*"/>
      <xsl:if test="text()">
         <xsl:value-of select="."/>
      </xsl:if>
   </xsl:element>
</xsl:template>

<xsl:template match="@*">
   <xsl:attribute name="{name()}">
      <xsl:value-of select="."/>
   </xsl:attribute>
</xsl:template>

<xsl:template match="/test">
  <xsl:apply-templates select="//x[not(@*)] "/>
</xsl:template>

</xsl:stylesheet>

Output

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

<x>
   <y>y31</y>
   <y>y32</y>
</x>