If you pass your generated XML fragment/document through the XMLSerialize()
function, and specify either INDENT
or NO INDENT
, then empty tags will be transformed to be self-closed. (Not specifying either leaves them untouched).
SELECT XMLSerialize(CONTENT
XMLELEMENT( "Consignment", XMLATTRIBUTES('123' AS "id",sysdate AS "date" ),
XMLELEMENT( "Box", xmlattributes( '321' as "id" ))
) as VARCHAR2(4000) INDENT) as xxx FROM DUAL;
XXX
--------------------------------------------------------------------------------
<Consignment id="123" date="2017-06-08">
<Box id="321"/>
</Consignment>
or without formatting:
SELECT XMLSerialize(CONTENT
XMLELEMENT( "Consignment", XMLATTRIBUTES('123' AS "id",sysdate AS "date" ),
XMLELEMENT( "Box", xmlattributes( '321' as "id" ))
) as VARCHAR2(4000) NO INDENT) as xxx FROM DUAL;
XXX
--------------------------------------------------------------------------------
<Consignment id="123" date="2017-06-08"><Box id="321"/></Consignment>
Your question shows the output as indented, but the code you provided does not indent it, so not really sure which of those you actually want.
I've used VARCHAR2
for the data type based on your use of getStringVal
, and you can make that smaller if you know the size - or switch to CLOB
if you don't, or know it might be too big for VARCHAR2
.