XMLPARSE is part of the SQL/XML standard (first added in SQL:2003). It takes a VARCHAR, CHAR, CLOB, or similar character string and validates it as either a full XML document or an XML content fragment, returning a value of the XML data type. Choosing DOCUMENT enforces exactly one top-level element, while CONTENT allows multiple top-level nodes such as comments or processing instructions. Optional whitespace handling lets you keep or trim insignificant whitespace. If the supplied string is not well-formed according to the chosen mode, the statement raises an error. The result can be stored in XML columns or passed to other XML functions such as XMLQUERY or XMLTABLE. Because parsing is done at run time, performance can be affected by very large strings, so indexes on XML columns or pre-validated storage are recommended in high-volume systems.
mode
(DOCUMENT | CONTENT) - keyword that defines validation rulesstring_expression
(string) - the character string containing XML textwhitespace
(optional) - PRESERVE WHITESPACE keeps all insignificant whitespace, STRIP WHITESPACE removes itXMLSERIALIZE, XMLQUERY, XMLTABLE, XMLEXISTS, CAST
SQL:2003 (SQL/XML)
XMLPARSE converts text to the XML type, while XMLSERIALIZE converts an XML value back to text.
No. The function validates the string. If it is not well-formed according to the chosen mode, the statement fails with a parse error.
Parsing large XML documents can be CPU intensive. Consider storing pre-validated XML or using indexes on XML columns to speed up downstream queries.
Check that you are using the correct mode (DOCUMENT vs CONTENT) and that the encoding matches the string's actual encoding. Also verify that special characters are properly escaped.