The XMLSEQUENCE operator is used to split multi-value results from XMLTYPE queries into multiple rows. The XMLSequence has two forms:
The first form takes as input an XMLType instance and returns a varray of the top-level nodes in the XMLType.
The second form takes as input a REFCURSOR instance, with an optional instance of the XMLFormat object, and returns as an XMLSequence type an XML document for each row of the cursor.
Example 1:
Output
Example 2:
Output
Example 3:
Output
Example 4:
Output
The first form takes as input an XMLType instance and returns a varray of the top-level nodes in the XMLType.
The second form takes as input a REFCURSOR instance, with an optional instance of the XMLFormat object, and returns as an XMLSequence type an XML document for each row of the cursor.
Example 1:
- SELECT ROWNUM AS ID
- , EXTRACTVALUE(COLUMN_VALUE,'/ROW/DEPTNO') DEPARTMENT_NO
- , EXTRACTVALUE(COLUMN_VALUE,'/ROW/DNAME') DEPARTMENT_NAME
- FROM TABLE(XMLSEQUENCE(extract(xmltype(DBMS_XMLGEN.GETXML('SELECT * FROM DEPT')),'/ROWSET/ROW')));
Output
- NO DEPARTMENT_NO DEPARTMENT_NAME
- ------------------------------------------------------------
- 1 10 ACCOUNTING
- 2 20 RESEARCH
- 3 30 SALES
- 4 40 OPERATIONS
Example 2:
- SELECT ROWNUM AS ID, EXTRACTVALUE(COLUMN_VALUE,'/ROW/DEPTNO') DEPARTMENT_NO,EXTRACTVALUE(COLUMN_VALUE,'/ROW/DNAME')
- DEPARTMENT_NAME
- FROM
- TABLE(XMLSEQUENCE(CURSOR(SELECT * FROM DEPT)));
Output
- NO DEPARTMENT_NO DEPARTMENT_NAME
- ------------------------------------------------------------
- 1 10 ACCOUNTING
- 2 20 RESEARCH
- 3 30 SALES
- 4 40 OPERATIONS
Example 3:
- SELECT rownum AS ID,EXTRACTVALUE(b.COLUMN_VALUE,'/ROW/DEPTNO') DEPARTMENT_NO
- , EXTRACTVALUE(b.COLUMN_VALUE,'/ROW/DNAME') DEPARTMENT_NAME
- FROM (
- SELECT XMLType(dbms_xmlgen.getXML('SELECT * FROM dept')) AS xml_data FROM dual ) a
- , TABLE(XMLSequence(EXTRACT(a.xml_data,'/ROWSET/ROW'))) b;
Output
- NO DEPARTMENT_NO DEPARTMENT_NAME
- ------------------------------------------------------------
- 1 10 ACCOUNTING
- 2 20 RESEARCH
- 3 30 SALES
- 4 40 OPERATIONS
Example 4:
- WITH xml_data AS (
- SELECT XMLType(dbms_xmlgen.getXML('SELECT * FROM dept')) AS xml FROM dual
- ) , parsed_xml_data AS (
- SELECT ROWNUM AS ID
- --, x_seq.*
- , extractvalue(x_seq.column_value,'/ROW/DEPTNO') AS DEPARTMENT_NO
- , extractvalue(x_seq.column_value,'/ROW/DNAME') AS DEPARTMENT_NAME
- FROM xml_data x,TABLE(XMLSequence(extract(x.xml,'/ROWSET/ROW'))) x_seq
- )
- SELECT * FROM parsed_xml_data;
Output
- NO DEPARTMENT_NO DEPARTMENT_NAME
- ------------------------------------------------------------
- 1 10 ACCOUNTING
- 2 20 RESEARCH
- 3 30 SALES
- 4 40 OPERATIONS