The XML type stores XML data in PolarDB for PostgreSQL (Compatible with Oracle). Unlike the text type, XML validates that input is well-formed and provides type-safe functions for working with XML data.
Prerequisites: The configure --with-libxml option must be configured when installing PolarDB for PostgreSQL (Compatible with Oracle).
What the XML type stores
The XML type stores two kinds of content:
| Content kind | Description |
|---|---|
| Well-formed document | An XML document conforming to the XML standard |
| Content fragment | Defined by reference to the document node of the XQuery and XPath data model; can have more than one top-level element or character node |
To check whether a value is a full document or a content fragment, use:
xmlvalue IS DOCUMENTGenerate XML values
Use XMLPARSE
XMLPARSE is the SQL-standard function for converting a string to an XML value:
XMLPARSE ( { DOCUMENT | CONTENT } value )Examples:
-- Parse a full XML document
XMLPARSE (DOCUMENT '<?xml version="1.0"?><book><title>Manual</title><chapter>...</chapter></book>')
-- Parse a content fragment
XMLPARSE (CONTENT 'abc<foo>bar</foo><bar>foo</bar>')Use PolarDB-specific cast syntax
PolarDB for PostgreSQL (Compatible with Oracle) also supports these shorthand forms:
xml '<foo>bar</foo>'
'<foo>bar</foo>'::xmlValidation behavior
The XML type does not validate input against a document type declaration (DTD), even if the input specifies one. There is no built-in support for validation against other XML schema languages such as XML Schema.
Serialize XML to strings
XMLSERIALIZE is the SQL-standard function for converting an XML value to a string:
XMLSERIALIZE ( { DOCUMENT | CONTENT } value AS type )type can be character, character varying, or text (or their aliases).
PolarDB for PostgreSQL (Compatible with Oracle) also allows simple casting as an alternative.
Control the DOCUMENT or CONTENT mode
When casting between strings and XML values without using XMLPARSE or XMLSERIALIZE, the XML option session parameter controls whether the value is treated as DOCUMENT or CONTENT. Set it using either command:
-- SQL-standard command
SET XML OPTION { DOCUMENT | CONTENT };
-- PolarDB-specific command
SET xmloption TO { DOCUMENT | CONTENT };The default is CONTENT, which accepts all forms of XML data.
Handle character encoding
Text mode (default)
In text mode, PolarDB converts all character data between the client and server to the destination's character encoding. This means:
Encoding declarations in XML data may become invalid after conversion.
The system ignores encoding declarations in character strings passed to the
XMLtype and assumes content is in the current server encoding.Send XML data in the current client encoding, or convert documents to the client encoding before sending.
On output, XML values carry no encoding declaration, and the client assumes all data is in the current client encoding.
Binary mode
In binary mode, no encoding conversion is performed. The system observes encoding declarations in the XML data. If no declaration is present, data is assumed to be in UTF-8, as required by the XML standard.
PolarDB for PostgreSQL (Compatible with Oracle) does not support UTF-16.
On output, data includes an encoding declaration specifying the client encoding — unless the client encoding is UTF-8, in which case the declaration is omitted.
Encoding recommendations
XML data processing is least error-prone and most efficient when the XML data encoding, client encoding, and server encoding are all the same. Computations are most efficient when the server encoding is UTF-8, because XML is processed internally in UTF-8.
Some XML-related functions may not work on non-ASCII data when the server encoding is not UTF-8. This is a known issue for xmltable() and xpath() in particular.
Query and index XML data
The XML type does not provide comparison operators, because no well-defined general comparison algorithm exists for XML data. As a result, you cannot retrieve rows by comparing an XML column directly against a search value. In most cases, XML values are accompanied by a separate key field such as an ID field. Alternatively, you can compare XML values by converting them to character strings first, though character string comparison is not ideal for XML comparison.
For fast XML queries, use one of the following workarounds:
| Workaround | Description |
|---|---|
| Index a string cast | Cast the XML expression to a string type and index that |
| Index an XPath expression | Index the XPath expression used in queries |
The query itself must use the same indexed expression.
The full-text search feature of PolarDB for PostgreSQL (Compatible with Oracle) can accelerate full-text searches of XML data, but the necessary preprocessing support is not included in the PolarDB for PostgreSQL (Compatible with Oracle) distribution.