The not_an_xml_document error (SQLSTATE 2200L) occurs when PostgreSQL expects a valid XML value but receives text that does not comply with XML syntax.
not_an_xml_document is a PostgreSQL error (SQLSTATE 2200L) that signals the supplied value is not well-formed XML. Cast the input to XML only after validating or correcting the markup, or skip the cast entirely. Loading clean XML into the column resolves the issue.
not_an_xml_document
PostgreSQL returns error code 2200L with message not_an_xml_document when it tries to cast or validate a value as XML and the supplied text is not well-formed XML markup.
The failure halts the current statement, preventing invalid XML from entering XML-typed columns or functions.
Fixing it quickly is vital because partial or silent data corruption can occur if malformed XML is stored.
PostgreSQL checks that every value assigned to the XML data type or passed to XML functions follows the W3C rules for well-formed documents. Missing root elements, unescaped characters, and mismatched tags trigger the check and raise not_an_xml_document.
The error also appears when casting text or varchar columns to XML inside SELECT queries, triggers, or views.
Any null bytes or invalid Unicode code points cause the same failure.
Validate the source string with an XML parser before inserting or casting. Correct broken markup, close tags, and escape ampersands. If the value is not intended to be XML, avoid the explicit ::xml cast or change the column type to text.
For bulk loads, run a pre-processing script that rejects malformed rows and logs them for review.
In transactional code, wrap casts inside TRY...CATCH in PL/pgSQL to give user-friendly feedback.
Scenario: importing XML feeds. Solution: stage data in a staging_text column, run xpath('//root', staging_text::xml) only after validation.
Scenario: reading JSON stored in text but accidentally casting to XML in a view. Solution: remove ::xml cast or convert JSON properly.
Store raw feeds in text first, then migrate to XML after validation.
Use CHECK constraints with the xml_is_well_formed() extension when possible.
Automate unit tests on functions that cast to XML. Galaxy’s AI copilot can scan migration scripts and highlight risky casts before deployment.
invalid_xml_content: raised when the document is well-formed but violates the XML content rules.
invalid_xml_comment: appears if comment syntax is broken. Fix by removing or escaping illegal comment sequences.
.
Characters like & and < inside text nodes must be escaped (&, <). Unescaped characters make the string not well-formed.
A valid XML document needs exactly one top-level element. Two sibling roots or none at all trigger the error.
Opening and closing tags must pair correctly and respect hierarchy.
Tag mismatches immediately fail XML validation.
Non-UTF-8 bytes or control characters like NULL (0x00) are forbidden in XML and cause PostgreSQL to raise the error.
.
Yes, PostgreSQL cancels the statement, so data is neither written nor returned. Fix before retrying.
No setting disables validation. Store data in text or bytea if you need to keep malformed XML.
The error exists in all supported versions. Newer versions may add stricter checks but behavior stays consistent.
Galaxy’s editor highlights failed casts in real time and its AI copilot suggests proper escapes, preventing the error before execution.