Du kan bruge dbms_xmlgen med dbms_xmlgen.setNullHandling(qryCtx, dbms_xmlgen.EMPTY_TAG) eller dbms_xmlgen.NULL_ATTR:
Opret f.eks. egen funktion
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;
/
så kan du bruge det i forespørgsler:
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>
Som du kan se, er den anden parameter i denne funktion null_handling:
- DROP_NULLS KONSTANT NUMMER:=0; Udelader tagget for NULL-elementer.
- NULL_ATTR KONSTANT NUMMER:=1; (Standard) Indstiller xsi:nil="true".
- EMPTY_TAG KONSTANT NUMMER:=2; Indstiller f.eks. .
Eller du kan endda inline din funktion i forespørgslen:
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
/
Resultat med standard NULL_ATTR:
XXX
-----------------------------------------------------------------
<ROWSET xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance">
<ROW>
<X xsi:nil = "true"/>
</ROW>
</ROWSET>
Resultat med standard 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>