PG
PRO
2200LERRORTier 2 — Caution✅ HIGH confidence

not an XML document

Category: Data ExceptionVersions: All Postgres versions (XML support must be compiled in)

What this means

SQLSTATE 2200L is raised when an XML-related function or cast receives input that is not a well-formed XML document. A valid XML document must have exactly one root element.

Why it happens

  1. 1Passing a string with multiple root elements or no root element to an XML function that requires a document
  2. 2Passing XML content that is well-formed as a fragment but not as a standalone document

How to reproduce

Casting a string with multiple root elements to XML.

trigger — this will ERROR
SELECT XMLPARSE(DOCUMENT '<a/><b/>'); -- two root elements
ERROR: invalid XML document

Fix 1: Wrap content fragments in a single root element

When the input is a fragment rather than a full document.

fix
SELECT XMLPARSE(DOCUMENT '<root><a/><b/></root>');

Why this works

Wrapping in a root element produces a valid XML document with exactly one root.

Fix 2: Use XMLPARSE(CONTENT ...) for XML fragments

When the input is a well-formed fragment but not a full document.

fix
SELECT XMLPARSE(CONTENT '<a/><b/>');

Why this works

CONTENT mode accepts XML fragments without requiring a single root element.

Sources

📚 Official docs: https://www.postgresql.org/docs/current/errcodes-appendix.html

🔧 Source ref: Class 22 — Data Exception

Confidence assessment

✅ HIGH confidence

Standard SQLSTATE for XML document validation. Stable across versions.

See also

📄 Reference pages

XML FunctionsXMLPARSE
⚙️ This error reference was generated with AI assistance and reviewed for accuracy. Examples are provided to illustrate common scenarios and may not cover every case. Always test fixes in a development environment before applying to production. Spotted an error? Suggest a correction →