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.
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)
<?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>