Functions
Boolean Functions
XPath boolean construction and logic functions
#Boolean Functions
XPath has four boolean functions. XPath defines "effective boolean value" rules. These rules convert other types to a boolean automatically in conditional contexts.
For C# developers: C# has no equivalent implicit conversion to boolean. XPath's effective boolean value rules convert an empty sequence, an empty string, or a zero value to
falseautomatically. XPath'strueandfalseare function calls, not literal keywords like C#'strue.
#boolean()
Converts a value to boolean using XPath's effective boolean value rules.
Signature: boolean($value as item()*) as xs:boolean
boolean("hello") => true (: non-empty string :)
boolean("") => false (: empty string :)
boolean(42) => true (: non-zero number :)
boolean(0) => false (: zero :)
boolean(()) => false (: empty sequence :)
boolean((1, 2)) => true (: non-empty sequence starting with a node or value :)
Effective boolean value rules:
|
Input |
Result |
|---|---|
|
Empty sequence |
|
|
Sequence starting with a node |
|
|
Single |
Its value |
|
Single |
|
|
Single number |
|
C# equivalent: None. See the callout above.
Why this matters: In XPath, you can write if (//error) then .... The sequence of error elements converts to true automatically when any exist. You do not need if (count(//error) > 0).
#true()
Returns true. Needed because XPath doesn't have boolean literals.
Signature: true() as xs:boolean
true() => true
Note: This is a function call with parentheses, not a literal value. XPath 4.0 also allows the keyword true without parentheses in some contexts.
#false()
Returns false.
Signature: false() as xs:boolean
false() => false
#not()
Logical negation.
Signature: not($value as item()*) as xs:boolean
not(true()) => false
not(false()) => true
not(()) => true (: empty sequence is "falsy" :)
not(//error) => true if no error elements exist
not(contains("abc", "x")) => true
C# equivalent: !value
Common pattern:
//book[not(@out-of-print)] (: books without the out-of-print attribute :)
//item[not(price > 100)] (: items not over $100 :)