Можете да използвате dbms_xmlgen с dbms_xmlgen.setNullHandling(qryCtx, dbms_xmlgen.EMPTY_TAG) или dbms_xmlgen.NULL_ATTR:
Например, създайте собствена функция
create or replace function f_get_xmltype_with_nulls (cur sys_refcursor, null_handling int default dbms_xmlgen.null_attr)
return xmltype
as
/* null_handling may be:
DROP_NULLS CONSTANT NUMBER:= 0; Leaves out the tag for NULL elements.
NULL_ATTR CONSTANT NUMBER:= 1; (Default) Sets xsi:nil="true".
EMPTY_TAG CONSTANT NUMBER:= 2; Sets, for example, <foo/>.
*/
res xmltype;
lc dbms_xmlgen.ctxhandle;
begin
lc:=dbms_xmlgen.newcontext(cur);
-- you can replace null_attr with empty_tag here:
dbms_xmlgen.setnullhandling(lc, null_handling);
res:=dbms_xmlgen.getxmltype(lc);
return res;
end;
/
тогава можете да го използвате в заявки:
SQL> select f_get_xmltype_with_nulls(cursor(select null x from dual connect by level<10)) x from dual;
X
------------------------------------------------------------------------
<ROWSET xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance">
<ROW>
<X xsi:nil = "true"/>
</ROW>
<ROW>
<X xsi:nil = "true"/>
</ROW>
<ROW>
<X xsi:nil = "true"/>
</ROW>
<ROW>
<X xsi:nil = "true"/>
</ROW>
<ROW>
<X xsi:nil = "true"/>
</ROW>
<ROW>
<X xsi:nil = "true"/>
</ROW>
<ROW>
<X xsi:nil = "true"/>
</ROW>
<ROW>
<X xsi:nil = "true"/>
</ROW>
<ROW>
<X xsi:nil = "true"/>
</ROW>
</ROWSET>
Както можете да видите, вторият параметър на тази функция е null_handling:
- DROP_NULLS ПОСТОЯННО ЧИСЛО:=0; Изпуска етикета за NULL елементи.
- NULL_ATTR КОНСТАНТНО НОМЕР:=1; (По подразбиране) Задава xsi:nil="true".
- ПРАЗЕН TAG ПОСТОЯНЕН НОМЕР:=2; Комплекти, например, .
Или дори можете да вградите функцията си в заявката:
with
function f_get_xmltype_with_nulls (cur sys_refcursor, null_handling int default dbms_xmlgen.null_attr)
return xmltype
as
/* null_handling may be:
DROP_NULLS CONSTANT NUMBER:= 0; Leaves out the tag for NULL elements.
NULL_ATTR CONSTANT NUMBER:= 1; (Default) Sets xsi:nil="true".
EMPTY_TAG CONSTANT NUMBER:= 2; Sets, for example, <foo/>.
*/
res xmltype;
lc dbms_xmlgen.ctxhandle;
begin
lc:=dbms_xmlgen.newcontext(cur);
-- you can replace null_attr with empty_tag here:
dbms_xmlgen.setnullhandling(lc, null_handling);
res:=dbms_xmlgen.getxmltype(lc);
return res;
end;
select
f_get_xmltype_with_nulls(cursor(select null as x from dual)) as xxx
from dual
/
Резултат с NULL_ATTR по подразбиране:
XXX
-----------------------------------------------------------------
<ROWSET xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance">
<ROW>
<X xsi:nil = "true"/>
</ROW>
</ROWSET>
Резултат с EMPTY_TAG по подразбиране:
select
f_get_xmltype_with_nulls(cursor(select null as x from dual),2) as xxx
from dual;
XXX
-------------------------------------
<ROWSET>
<ROW>
<X/>
</ROW>
</ROWSET>