7 Functions and operators on Boolean values

This section defines functions and operators on the xs:boolean datatype.

7.1 Boolean constant functions

Since no literals are defined in XPath to reference the constant boolean values true and false, two functions are provided for the purpose.

Function Meaning
fn:true Returns the xs:boolean value true.
fn:false Returns the xs:boolean value false.

7.1.1 fn:true

Summary

Returns the xs:boolean value true.

Signature
fn:true() as xs:boolean
Properties

This function is ·deterministic·, ·context-independent·, and ·focus-independent·.

Rules

The result is equivalent to xs:boolean("1").

Examples

The expression fn:true() returns xs:boolean(1).

7.1.2 fn:false

Summary

Returns the xs:boolean value false.

Signature
fn:false() as xs:boolean
Properties

This function is ·deterministic·, ·context-independent·, and ·focus-independent·.

Rules

The result is equivalent to xs:boolean("0").

Examples

The expression fn:false() returns xs:boolean(0).

7.2 Operators on Boolean values

The following functions define the semantics of operators on boolean values in [XQuery 4.1: An XML Query Language] and [XML Path Language (XPath) 4.0]:

Function Meaning
op:boolean-equal Returns true if the two arguments are the same boolean value.
op:boolean-less-than Returns true if the first argument is false and the second is true.

The ordering operator op:boolean-less-than is provided for application purposes and for compatibility with [XML Path Language (XPath) Version 1.0]. The [XML Schema Part 2: Datatypes Second Edition] datatype xs:boolean is not ordered.

7.2.1 op:boolean-equal

Summary

Returns true if the two arguments are the same boolean value.

Operator Mapping

Defines the semantics of the "eq" operator when applied to two xs:boolean values.

Signature
op:boolean-equal(
$value1 as xs:boolean,
$value2 as xs:boolean
) as xs:boolean
Rules

The function returns true if both arguments are true or if both arguments are false. It returns false if one of the arguments is true and the other argument is false.

7.2.2 op:boolean-less-than

Summary

Returns true if the first argument is false and the second is true.

Operator Mapping

Defines the semantics of the "lt" operator when applied to two xs:boolean values. Also used in the definition of the "ge" operator.

Signature
op:boolean-less-than(
$arg1 as xs:boolean,
$arg2 as xs:boolean
) as xs:boolean
Rules

The function returns true if $arg1 is false and $arg2 is true. Otherwise, it returns false.

7.3 Functions on Boolean values

The following functions are defined on boolean values:

Function Meaning
fn:boolean Computes the effective boolean value of the sequence $input.
fn:not Returns true if the effective boolean value of $input is false, or false if it is true.

7.3.1 fn:boolean

Summary

Computes the effective boolean value of the sequence $input.

Signature
fn:boolean(
$input as item()*
) as xs:boolean
Rules

The function computes the effective boolean value of a sequence, defined according to the following rules. See also Section 2.4.3 Effective Boolean Value XP31.

  • If $input is the empty sequence, fn:boolean returns false.

  • If $input is a sequence whose first item is a node, fn:boolean returns true.

  • If $input is a singleton value of type xs:boolean or a derived from xs:boolean, fn:boolean returns $input.

  • If $input is a singleton value of type xs:string or a type derived from xs:string, xs:anyURI or a type derived from xs:anyURI, or xs:untypedAtomic, fn:boolean returns false if the operand value has zero length; otherwise it returns true.

  • If $input is a singleton value of any numeric type or a type derived from a numeric type, fn:boolean returns false if the operand value is NaN or is numerically equal to zero; otherwise it returns true.

Error Conditions

In all cases other than those listed above, fn:boolean raises a type error [err:FORG0006].

Notes

The result of this function is not necessarily the same as $input cast as xs:boolean. For example, fn:boolean("false") returns the value true whereas "false" cast as xs:boolean (which can also be written xs:boolean("false")) returns false.

Examples
let $abc := ("a", "b", "")

fn:boolean($abc) raises a type error [err:FORG0006].

The expression fn:boolean($abc[1]) returns true().

The expression fn:boolean($abc[0]) returns false().

The expression fn:boolean($abc[3]) returns false().

fn:boolean([]) raises a type error [err:FORG0006].

7.3.2 fn:not

Summary

Returns true if the effective boolean value of $input is false, or false if it is true.

Signature
fn:not(
$input as item()*
) as xs:boolean
Properties

This function is ·deterministic·, ·context-independent·, and ·focus-independent·.

Rules

The value of $input is first reduced to an effective boolean value by applying the fn:boolean() function. The function returns true if the effective boolean value is false, or false if the effective boolean value is true.

Examples

The expression fn:not(fn:true()) returns false().

The expression fn:not(()) returns true().

The expression fn:not("false") returns false().

fn:not(1 to 10) raises a type error [err:FORG0006].