The XML column type
JSON pushed XML out of new designs, but the columns still exist: SQL Server and Oracle have real XML types with query support, the rest store text and hope.
- Family
- JSON & semi-structured
- Missing in
- MySQL, SQLite
- Gotchas
- 5 listed
Engine by engine
Real XML types (SQL Server, Oracle) validate and can be queried with XQuery; PostgreSQL's xml stores well-formed text; MySQL and SQLite store raw text with no checks.
| Engine | Typ | Speicher | Notizen |
|---|---|---|---|
| SQL Server |
XML
|
Variable, up to 2 GB | Untyped or typed against an XML SCHEMA COLLECTION; XQuery via .query/.value/.exist, and selective XML indexes (2012 SP1 onward) make path queries indexable. |
| PostgreSQL |
XML
|
Textual, variable | Checks well-formedness on insert and offers xpath(), XMLEXISTS and XMLTABLE, but XPath 1.0 only (no XQuery), and the server must be built with libxml. |
| MySQL |
none (TEXT)
Workaround
|
TEXT limits | No XML type. Store in TEXT; ExtractValue() and UpdateXML() handle a subset of XPath 1.0, and malformed XML returns NULL with a warning, not an error. |
| SQLite |
none (TEXT)
Workaround
|
TEXT limits | No XML type or functions. Store text; parse in the application. |
| Oracle |
XMLType
|
Variable | The heavyweight: object-relational or binary storage, registered schemas, XMLQuery/XMLTable/XDB features, and index options down to structured storage of elements. |
Gotchas
Where the engines disagree. These tend to surface as bugs during a migration or a port.
- PostgreSQL's xml type only enforces well-formedness (one root element, balanced tags), not validity against a schema; and if the server lacks libxml, the XML functions error out while the type still stores text.
- MySQL's ExtractValue() returns an empty string when the path matches nothing and NULL (plus warning 1525) when the XML is malformed, so validate documents before they are written.
- SQL Server typed XML (bound to a schema collection) validates on write, untyped XML only checks well-formedness; switching a column to typed with ALTER COLUMN revalidates every existing row and fails on the first one that does not conform.
- Oracle XMLType in object-relational storage can be extremely fast and extremely rigid: adding an element to a registered schema means schema evolution work nobody budgeted.
- Namespaces are the classic trap: a document with a default xmlns returns nothing for //item in SQL Server, PostgreSQL and Oracle until the query declares the namespace.
Common questions
Is there any reason to use an XML column today?
Legacy documents, SOAP-era integrations, and office formats (docx, xlsx are zipped XML) that arrive as-is. For new application data, JSON is the default; keep XML when the data already is XML.
Does MySQL validate XML in a TEXT column?
No. It stores any text, and ExtractValue() on malformed XML returns NULL with a warning. There is no XML validation function to put in a CHECK constraint.
What is the difference between typed and untyped XML in SQL Server?
Typed XML is bound to an XML SCHEMA COLLECTION and validated against it on insert; untyped accepts any well-formed fragment. Typed gives you validation and better storage inference at the cost of schema rigidity.
Related on SQLly
Map a type to the other four engines
The free type converter maps a type from one engine to its closest equivalent in the others and says where storage, precision or behaviour differ. The desktop app does the same mapping while you edit.