Purpose
XMLTABLE() maps the result returned based on an XPath into relational rows and columns. You can query the result returned by the function as a virtual relational table using SQL.
XMLTABLE() provides the following key features:
Data extraction: You can specify an XPath expression in
XMLTABLE()to accurately extract the desired parts of data from a complex XML document.Data structuring:
XMLTABLE()can map specific parts of XML data to relational rows and columns and thereby convert semi-structured XML data into structured tabular form, making SQL queries and analysis easier.Table conversion: You can use structured data generated by
XMLTABLE()to create virtual tables and use these table data to generate new tables or views for further processing in 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 | The namespace prefix and URI for referencing an element or attribute with a namespace in XML queries. The clause contains a set of XML namespace declarations that are referenced by the XPath expression in the PATH clause of XML_table_column. The expression evaluates 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 | The XQuery expression that selects data to be converted from the XML data. XQuery is an XML query language for extracting data from XML documents and implementing complex query operations. This parameter allows you to use the XQuery syntax to define instructions that specify elements or attributes to be extracted from the XML data.
NoticeThe current version of OceanBase Database does not support XQuery. |
| XML_passing_clause | Specifies the XML data to be passed to the XQuery expression by using the PASSING keyword. You can configure to pass the XML data by value or reference. The details are as follows:
|
| COLUMNS | Defines the columns of the virtual table to be created by XMLTable(). This clause is optional. If you omit the COLUMNS clause, XMLTable() returns a row with a single XMLType pseudocolumn named COLUMN_VALUE. |
| XML_table_column | Defines a table column to be generated, which can be a column of a common data type or an XMLType column. You can also specify the XPath and default value for the column. FOR ORDINALITY is used to generate row numbers. The details are as follows:
|
Return type
The return type depends on the data type of the column specified in the XMLTable() function.
Examples
Call XMLTABLE() to convert XML data into an SQL table with columns of the employee ID, name, position, and salary. The details are as follows:
Define the default namespace
http://www.example.com/employeesto match the namespace in the XML data.Define the XQuery expression
'/employees/employee'to select allemployeeelements.Specify the
PASSINGclause to pass the XML document to the XQuery expression. This example uses theXMLTYPE()constructor to encapsulate the entire XML document.XMLTYPE()converts XML data in the string form intoXMLTypedata that the database can process.In the
COLUMNSclause, define four columns that hold the data extracted from the XML document. The mappings are as follows:- The
employee_idcolumn is mapped toemp_idunder eachemployeeelement. - The
employee_namecolumn is mapped tonameunder eachemployeeelement. - The
positioncolumn is mapped topositionunder eachemployeeelement. - The
salarycolumn is mapped tosalaryunder eachemployeeelement.
The
PATHclause after each column defines where to extract the corresponding values.- 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 return result is as follows:
+-------------+---------------+-----------+--------+
| EMPLOYEE_ID | EMPLOYEE_NAME | POSITION | SALARY |
+-------------+---------------+-----------+--------+
| 1 | John Doe | Developer | 5000 |
| 2 | Jane Smith | Manager | 6500 |
+-------------+---------------+-----------+--------+
2 rows in set