Purpose
This function is used to map the results returned by XPath to rows and columns of a relational table. You can use the results returned by SQL queries as virtual relational tables.
The key capabilities of the XMLTABLE function include:
Data extraction: By specifying appropriate XPath expressions in
XMLTABLE, you can precisely extract the required data from complex XML documents.Data structuring:
XMLTABLEcan map specific parts of XML data to rows and columns of relational tables, converting semi-structured XML data into structured table form, making it easier to perform SQL queries and analysis.Table conversion: You can use the structured data generated by
XMLTABLEto create virtual tables and use these table data to generate new tables or views, allowing you to save or further process the data 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
| Field | 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 subclause 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 subclause, 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 implementing complex query operations from XML documents. This parameter allows you to define instructions for extracting specific elements or attributes from XML data using XQuery syntax.
NoticeOceanBase Database does not support XQuery in the current version. |
| XML_passing_clause | Specifies the XML data to be passed to the XQuery expression using the PASSING keyword. You can specify passing by value and the alias for the passed data. 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 pseudo-column named COLUMN_VALUE of type XMLType. |
| 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. Details are as follows:
|
Return type
The return type depends on the data type 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. Here's how:
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 allemployeeelements.Use the
PASSINGclause to specify the XML document to be 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 mapping is 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 where the corresponding values are extracted.
- 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