Как насчет dbms_xmlgen.newContextFromHierarchy
?
Предполагая, что ddls из вашего вопроса выглядит так:
create table table1 ( table1_id number, table1_name varchar2(30));
create table table2 (child_id number, parent_id number);
С образцами данных:
insert into table1 values( 1, 'X');
insert into table1 values( 2, 'Y');
insert into table1 values( 3, 'Z');
insert into table1 values( 4, 'W');
insert into table1 values( 5, 'K');
insert into table2 values (1, null);
insert into table2 values (2, 1);
insert into table2 values (3, 1);
insert into table2 values (4, 3);
insert into table2 values (5, 3);
Следующий запрос:
select dbms_xmlgen.newContextFromHierarchy('
select
level,
xmlelement("TABLE1",
xmlelement("NAME",t1.table1_name)
)
from table1 t1
join table2 t2 on t2.child_id = t1.table1_id
start with t2.parent_id is null
connect by prior t2.child_id = t2.parent_id
')
from dual;
Должен вернуться:
<?xml version="1.0"?>
<TABLE1>
<NAME>X</NAME>
<TABLE1>
<NAME>Y</NAME>
</TABLE1>
<TABLE1>
<NAME>Z</NAME>
<TABLE1>
<NAME>W</NAME>
</TABLE1>
<TABLE1>
<NAME>K</NAME>
</TABLE1>
</TABLE1>
</TABLE1>