Purpose
The XMLTABLE function is used to map the results returned by an XPath expression to rows and columns in a relational table. You can use SQL queries to retrieve the results of the XMLTABLE function as a virtual relational table.
The key capabilities of the XMLTABLE function include:
Data extraction: By specifying an appropriate XPath expression in the
XMLTABLEfunction, you can precisely extract the required data from complex XML documents.Data structuring: The
XMLTABLEfunction can map specific parts of XML data to rows and columns in a relational table, converting semi-structured XML data into a structured table format, making it easier to query and analyze with SQL.Table conversion: You can use the structured data generated by the
XMLTABLEfunction to create virtual tables. These tables can then be used to generate new tables or views, allowing you to store or further process the data within the database.
Syntax
XMLTABLE( [ XMLnamespaces_clause , ] XQuery_string XMLTABLE_options )
XMLnamespaces_clause:
XMLNAMESPACES( { string AS identifier } | { DEFAULT string } [, { string AS identifier } | { DEFAULT string } ]...)
XMLTABLE_options:
[ XML_passing_clause ]
[ COLUMNS XML_table_column [, XML_table_column]...]
XML_passing_clause:
PASSING [ BY VALUE ] expr [ AS identifier ]
XML_table_column:
column { FOR ORDINALITY
| { datatype | XMLTYPE } [ PATH string ] [ DEFAULT expr ]
}
Parameters
| Parameter | Description |
|---|---|
| XMLnamespaces_clause | Defines namespace prefixes and URIs for referencing elements and attributes with namespaces in XML queries. This clause contains a set of XML namespace declarations referenced by XPath expressions in the PATH clause of XML_table_column, which compute the columns of the entire XMLTable function. If you want to use qualified names in the PATH expressions of the COLUMNS clause, you need to specify this clause. |
| XQuery_string | Specifies an XQuery expression for selecting data to be converted from XML data. XQuery is an XML query language for extracting data and performing complex query operations from XML documents. This parameter allows you to use XQuery syntax to define instructions for extracting specific elements or attributes from XML data.
NoticeCurrently, OceanBase Database does not support XQuery. |
| XML_passing_clause | Specifies the XML data to be passed to the XQuery expression using the PASSING keyword. You can specify passing data by value and provide an alias for the data. The details are as follows:
|
| COLUMNS | An optional COLUMNS clause defines the columns of the virtual table created by XMLTable. If you omit the COLUMNS clause, XMLTable returns a single row with a single XMLType pseudo-column named COLUMN_VALUE. |
| XML_table_column | Defines the columns to be generated, which can be regular data type columns or XMLTYPE columns. You can also specify the XPath path and default value for the column values. FOR ORDINALITY is used to generate row numbers. The details are as follows:
|
Return type
The return type depends on the data types specified for the columns in the XMLTable function.
Examples
Use the XMLTABLE function to convert XML data into an SQL table containing employee IDs, names, positions, and salaries. The steps are as follows:
Define the default namespace as
http://www.example.com/employeesto match the namespace in the XML data.Define the XQuery expression as
'/employees/employee'to select all employee (employee) elements.Use the
PASSINGclause to specify the XML document passed to the XQuery expression. In this example, the entire XML document is encapsulated using the XMLTYPE constructor. XMLTYPE converts the XML data from string format into an XML type that the database can process.In the
COLUMNSclause, define four columns to map the extracted data from the XML. The mappings are as follows:- The
employee_idcolumn maps to theemp_idunder eachemployeeelement. - The
employee_namecolumn maps to thenameunder eachemployeeelement. - The
positioncolumn maps to thepositionunder eachemployeeelement. - The
salarycolumn maps to thesalaryunder eachemployeeelement.
The PATH clause after each column defines the source of the corresponding value.
- The
SELECT *
FROM XMLTABLE(
XMLNAMESPACES(DEFAULT 'http://www.example.com/employees'),
'/employees/employee'
PASSING XMLTYPE('
<employees xmlns="http://www.example.com/employees">
<employee>
<emp_id>1</emp_id>
<name>John Doe</name>
<position>Developer</position>
<salary>5000</salary>
</employee>
<employee>
<emp_id>2</emp_id>
<name>Jane Smith</name>
<position>Manager</position>
<salary>6500</salary>
</employee>
</employees>'
)
COLUMNS
employee_id INT PATH 'emp_id',
employee_name VARCHAR2(100) PATH 'name',
position VARCHAR2(50) PATH 'position',
salary NUMBER PATH 'salary'
) AS emp_tab;
The result is as follows:
+-------------+---------------+-----------+--------+
| EMPLOYEE_ID | EMPLOYEE_NAME | POSITION | SALARY |
+-------------+---------------+-----------+--------+
| 1 | John Doe | Developer | 5000 |
| 2 | Jane Smith | Manager | 6500 |
+-------------+---------------+-----------+--------+
2 rows in set
