XMLROOT is an XML-constructor function that returns a new XML value identical to the supplied XML except for its XML declaration. It is primarily used when you need the XML prolog to explicitly include or omit the version number and the standalone flag. Internally, the function does not modify element content; it only prepends or alters the declaration ''. If VERSION is supplied, the existing declaration is replaced or inserted. The SPECIAL token NO VERSION (PostgreSQL) preserves any existing version or omits the declaration entirely. The STANDALONE clause can be set to YES, NO, or omitted (meaning unchanged/absent). XMLROOT is deterministic and operates on an XML or XMLTYPE value, returning the same type. Unlike XMLSERIALIZE, it does not convert to text; it keeps the datatype. Be careful: XMLROOT will throw an error if the source is NULL, and some databases (like PostgreSQL) only allow the function inside queries, not in DDL.
xml_value
(XML) - The input XML document or fragment on which to operate.VERSION
(text) - Quoted string specifying the version attribute (e.g., '1.0').NO VERSION
(keyword) - Keeps existing declaration or omits it if none.STANDALONE
(YES/NO) - Sets the standalone attribute; omit to leave unchanged.XMLPARSE, XMLSERIALIZE, XMLELEMENT, XMLTYPE, XMLTABLE
ISO SQL:2006 XML feature; first implemented in PostgreSQL 8.4
Only the input XML value is mandatory. VERSION and STANDALONE are optional modifiers.
Yes. In PostgreSQL, use the NO VERSION keyword without specifying STANDALONE to strip the declaration.
It returns the native XML/XMLTYPE data type, not plain text. Use XMLSERIALIZE if you need a string.
XMLROOT is part of the ISO SQL XML feature set introduced in SQL:2006, but support varies by database.