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>
ご覧のとおり、この関数の2番目のパラメーターはnull_handlingです:
- DROP_NULLS定数番号:=0; NULL要素のタグを省略します。
- NULL_ATTR定数番号:=1; (デフォルト)xsi:nil="true"を設定します。
- EMPTY_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>