Product SiteDocumentation Site

5.2.6. Logics

OrExpr
Figure 5.22. OrExpr

AndExpr
Figure 5.23. AndExpr

NotExpr
Figure 5.24. NotExpr

JSONiq logics support is based on two-valued logics: just true and false.
Non-boolean operands get automatically converted to either true or false, or an error is raised. The boolean() function performs a manual conversion.
JSONiq supports the most famous three boolean operations: conjunction, disjunction and negation. Negation has the highest precedence, then conjunction, then disjunction. Parentheses can override.
Example 5.49. Logics with booleans
true and ( true or not true )
Result (run with Zorba):
true

Example 5.50. Logics with comparing operands
1 + 1 eq 2 or 1 + 1 eq 3
Result (run with Zorba):
true

Example 5.51. Conversion of the empty sequence to false
boolean(())
Result (run with Zorba):
false

Example 5.52. Conversion of null to false
boolean(null)
Result (run with Zorba):
false

Example 5.53. Conversion of a string to true
boolean("foo"), boolean("")
Result (run with Zorba):
true false

Example 5.54. Conversion of a number to false
0 and true, not (not 1e42)
Result (run with Zorba):
false true

Example 5.55. Conversion of an object to a boolean (not implemented in Zorba at this point)
{ "foo" : "bar" } or false
Result (run with Zorba):
true

If the input sequence has more than one item, and the first item is not an object or array, an error is raised.
Example 5.56. Error upon conversion of a sequence of more than one item, not beginning with a JSON item, to a boolean
( 1, 2, 3 ) or false
Result (run with Zorba):
An error was raised: invalid argument type for function fn:boolean(): effective boolean value not defined for sequence of more than one item that starts with "xs:integer"

Unlike in C++ or Java, you cannot rely on the order of evaluation of the operands of a boolean operation. The following query may return true or may return an error.
Example 5.57. Non-determinism in presence of errors.
true or (1 div 0)
Result (run with Zorba):
true

QuantifiedExpr
Figure 5.25. QuantifiedExpr

It is possible to perform a conjunction or a disjunction on a predicate for each item in a sequence.
Example 5.58. Universal quantifier
every $i in 1 to 10 satisfies $i gt 0
Result (run with Zorba):
true

Example 5.59. Existential quantifier on several variables
some $i in -5 to 5, $j in 1 to 10 satisfies $i eq $j
Result (run with Zorba):
true

Variables can be annotated with a type. If no type is specified, item* is assumed. If the type does not match, an error is raised.
Example 5.60. Existential quantifier with type checking
some $i as integer in -5 to 5, $j as integer in 1 to 10 satisfies $i eq $j
Result (run with Zorba):
true