PolarDB for PostgreSQL (Compatible with Oracle) supports the XMLType composite type for storing and manipulating XML data. This topic describes its definition, features, and common operations.
Prerequisites
XMLType is supported in the following versions of PolarDB for PostgreSQL (Compatible with Oracle):
Oracle syntax compatibility 2.0 (minor engine version 2.0.14.17.32.0 or later)
You can view the minor engine version in the console or by running the SHOW polardb_version; statement. If the minor engine version does not meet the requirements, upgrade the minor engine version.
Static functions and constructors
Static function
createXML: This function creates anXMLTypeobject from a string. The function prototypes are as follows:STATIC FUNCTION createXML(xmlData IN CLOB) RETURN XMLType STATIC FUNCTION createXML(xmlData IN VARCHAR2) RETURN XMLType STATIC FUNCTION createXML(xmlData IN CLOB, schema IN VARCHAR2, validated IN NUMBER := 0, wellFormed IN NUMBER := 0) RETURN XMLType STATIC FUNCTION createXML(xmlData IN VARCHAR2, schema IN VARCHAR2, validated IN NUMBER := 0, wellFormed IN NUMBER := 0) RETURN XMLTypeConstructor
XMLType: Similar to thecreateXMLstatic function, this constructor creates anXMLTypeobject from a string. The function prototypes are as follows:CONSTRUCTOR FUNCTION XMLType(xmlData IN CLOB, schema IN VARCHAR2 := NULL, validated IN NUMBER := 0, wellFormed IN NUMBER := 0) RETURN SELF AS RESULT CONSTRUCTOR FUNCTION XMLType(xmlData IN VARCHAR2, schema IN VARCHAR2 := NULL, validated IN NUMBER := 0, wellFormed IN NUMBER := 0) RETURN SELF AS RESULT
The following describes the parameters in the preceding function prototypes:
The
xmlDataparameter is the input XML string.The
schemaparameter is not supported and must be NULL.The
validatedparameter is not supported and must be 0.The
wellFormedparameter controls the XML format check. IfwellFormedis 0 or NULL,xmlDatamust be a valid XML document. IfwellFormedis set to any other value, the XML format check is skipped.
Examples
Static function
createXMLDECLARE xml_doc XMLType; BEGIN xml_doc := XMLType.createXML('<root><name>Alice</name><age>30</age></root>'); DBMS_OUTPUT.PUT_LINE(xml_doc.getStringVal()); END; /The following result is returned:
<root><name>Alice</name><age>30</age></root>Constructor
XMLTypeDECLARE xml_doc XMLType; BEGIN xml_doc := XMLType('<root><name>Alice</name><age>30</age></root>'); DBMS_OUTPUT.PUT_LINE(xml_doc.getStringVal()); END; /The following result is returned:
<root><name>Alice</name><age>30</age></root>
Type conversion functions
The XMLType composite type provides several functions to convert XMLType data to other data types. The return value is of the target data type. The functions are as follows:
getStringVal: Converts
XMLTypedata to aVARCHAR2string.MEMBER FUNCTION getStringVal() RETURN VARCHAR2getClobVal: Converts
XMLTypedata to aCLOBstring.MEMBER FUNCTION getClobVal() RETURN CLOBgetNumberVal: Converts
XMLTypedata to aNUMBERvalue.MEMBER FUNCTION getNumberVal() RETURN NUMBER
Example
DECLARE
xml_doc XMLType;
xml_clob CLOB;
BEGIN
xml_doc := XMLType('<root><name>Alice</name><age>30</age></root>');
xml_clob := xml_doc.getClobVal();
DBMS_OUTPUT.PUT_LINE(xml_clob);
END;
/The following result is returned:
<root><name>Alice</name><age>30</age></root>The extract function
The extract function extracts a portion of data from an XML document and returns it as an XMLType object. The function prototype is as follows:
MEMBER FUNCTION extract(xpath IN VARCHAR2) RETURN XMLTypeExample
DECLARE
xml_obj XMLType := XMLType.createXML('<root><name>Alice</name><age>30</age></root>');
result XMLType;
BEGIN
result := xml_obj.extract('/root/name');
DBMS_OUTPUT.PUT_LINE(result.getStringVal()); -- Outputs: <name>Alice</name>
END;
/The following result is returned:
<name>Alice</name>