xml_exists

Tests if the result of a query is empty.

Signature

xml_exists(
    STRING query, 
    { STRING | STRUCT } bindings
  ) as BOOLEAN

Description

query

An XQuery or XPath expression.

bindings

The input that the query processes. The value can be an XML STRING or a STRUCT of variable values:

  • STRING: The string is bound to the initial context item of the query as XML.

  • STRUCT: A STRUCT with an even number of fields. Each pair of fields defines a variable binding (name, value) for the query. The name fields must be type STRING, and the value fields can be any supported primitive. See "About Data Type Conversions."

Return Value

true if the result of the query is not empty, and false otherwise.

false if the query raises a dynamic error.

Notes

The first dynamic error raised by a query is logged, but subsequent errors are suppressed.

Examples

Example 1   STRING Binding

This example parses and binds the input XML string to the initial context item of the query "x/y":

> SELECT xml_exists("x/y", "<x><y>123</y></x>") FROM src LIMIT 1;
true
Example 2   STRUCT Binding

This example defines two query variables, $data and $value.

> SELECT xml_exists(
      "parse-xml($data)/x/y[@id = $value]",
      struct(
         "data", "<x><y id='1'/><y id='2'/></x>",
         "value", 2
      )
   ) FROM src LIMIT 1;
true
Example 3   Error Logging

In this example, an error is written to the log, because the input XML is invalid.

> SELECT xml_exists("x/y", "<x><y>123</invalid></x>") FROM src LIMIT 1;
false