如何在Oracle PLSQL中获得自动关闭的xml标记? [英] How to get a self-closing xml tag in Oracle PLSQL?
问题描述
请求的输出应为:
<Consignment id="123" date="2017-06-08">
<Box id="321" />
</Consignment>
<box>
标签应如上所述自动关闭.
where the <box>
tag should be self-closing as above.
我正在使用以下代码:
SELECT XMLELEMENT( "Consignment", XMLATTRIBUTES('123' AS "id",sysdate AS "date" ),
XMLELEMENT( "Box", xmlattributes( '321' as "id" ))
).getstringval() as xxx FROM DUAL;
,但它总是返回以下结果(标记<box>
具有单独的结束标记</box>
):
but it always return the following results (where the tag <box>
has a separated closing tag </box>
):
<Consignment id="123" date="2017-06-08">
<Box id="321"></Box>
</Consignment>
如何自动关闭上述<box>
标签?
how to get the above <box>
tag self-closed?
推荐答案
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>
或不格式化:
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.
根据您对getStringVal
的使用,我已经将VARCHAR2
用于数据类型,如果您知道大小,则可以将其减小-如果您不知道它,则切换至CLOB
,或者不知道它对于VARCHAR2
来说可能太大了.
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
.
这篇关于如何在Oracle PLSQL中获得自动关闭的xml标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!