Categories:

Semi-structured and structured data functions (Parsing)

CHECK_XML¶

Checks the validity of an XML document. If the input string is NULL or a valid XML document, the output is NULL. In case of an XML parsing error, the output string contains the error message.

See also:

PARSE_XML

Syntax¶

CHECK_XML( <string_containing_xml> [ , <disable_auto_convert> ] )
Copy
CHECK_XML( STR => <string_containing_xml>
  [ , DISABLE_AUTO_CONVERT => <disable_auto_convert> ] )
Copy

Arguments¶

Required:

string_containing_xml . OR . STR => string_containing_xml

This expression should evaluate to a VARCHAR. The VARCHAR should contain valid XML.

Optional:

disable_auto_convert . OR . DISABLE_AUTO_CONVERT => disable_auto_convert

Specify the same value that you pass to the PARSE_XML function.

Default: FALSE

Returns¶

The data type of the returned value is VARCHAR.

Usage notes¶

  • You must either specify all arguments by name or by position. You cannot specify some of the arguments by name and other arguments by position.

    When specifying an argument by name, you cannot use double quotes around the argument name.

Examples¶

Show the output of the function when the XML is valid¶

SELECT CHECK_XML('<name> Valid </name>');
+-----------------------------------+
| CHECK_XML('<NAME> VALID </NAME>') |
|-----------------------------------|
| NULL                              |
+-----------------------------------+
Copy

Show the output of the function when the XML is invalid¶

SELECT CHECK_XML('<name> Invalid </WRONG_CLOSING_TAG>');
+--------------------------------------------------+
| CHECK_XML('<NAME> INVALID </WRONG_CLOSING_TAG>') |
|--------------------------------------------------|
| no opening tag for </WRONG_CLOSING_TAG>, pos 35  |
+--------------------------------------------------+
Copy

Locate records with invalid XML¶

SELECT xml_str, CHECK_XML(xml_str)
    FROM my_table
    WHERE CHECK_XML(xml_str) IS NOT NULL;
Copy