exists function

The fn:exists function can check for the existence of many different types of items, such as elements, attributes, text nodes, atomic values (for example, an integer) or XML documents. If the XQuery expression specified as its argument, sequence-expression, produces an empty result (the empty sequence), then fn:exists returns false. If the argument returns anything but the empty sequence, then fn:exists returns true.

Syntax

Read syntax diagramSkip visual syntax diagramfn:exists( sequence-expression)
sequence-expression
A sequence of any data type, or the empty sequence

Returned value

The returned value is true if sequence-expression is not the empty sequence. If sequence-expression produces the empty sequence, the returned value is false.

Examples

The following example uses the exists function to determine whether the sequence in variable $seq is not the empty sequence.

let $seq := (5, 10)
return fn:exists($seq)

The value true is returned.

The next example checks whether there is an element, customer, with a child element, phone. If there is, the fn:exists function returns true:
fn:exists($info/customer/phone)
The following example returns true if there is an element, customer, which has an attribute, Cid:
fn:exists($info/customer/@Cid)
The next example checks whether the element, comment, has a text node. In this example, if the comment element is an empty element it has no text node, so fn:exists returns false. Also, if there is no comment element at all, fn:exists returns false:
fn:exists($info/customer/comment/text())
The final example checks whether there is any XML document in the XML column INFO of the CUSTOMER table:
fn:exists( db2-fn:xmlcolumn("CUSTOMER.INFO") )