Du kan använda dbms_xmlgen med dbms_xmlgen.setNullHandling(qryCtx, dbms_xmlgen.EMPTY_TAG) eller dbms_xmlgen.NULL_ATTR:
Skapa till exempel en 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;
/
sedan kan du använda den i frågor:
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 är den andra parametern för denna funktion null_handling:
- DROP_NULLS KONSTANTNUMMER:=0; Utelämnar taggen för NULL-element.
- NULL_ATTR KONSTANT NUMMER:=1; (Standard) Ställer in xsi:nil="true".
- EMPTY_TAG KONSTANT NUMMER:=2; Anger till exempel .
Eller så kan du till och med infoga din funktion i frågan:
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>