在下面的SQL XML中,recordingInfo如果所有这些字段都为空,我不想显示标签:
recordingInfo
cc.source_album_title cc.product_album_promo_title cc.label cc.catalogue_no cc.isrc
这是我当前的查询:
SELECT XMLAGG(XMLElement("Cue" -- start level 5 tag for cue ,XMLFOREST( rownum as "cueId" ,cc.dn_ccst_status as "cueStatusType" ,decode(cc.dn_ccst_status,'5',cc.cup_code,NULL) as "cueCupType" ) ,XMLElement("musicWork" -- start level 6 tag for music title ,XMLFOREST(cc.title as "musicTitle") ,XMLElement("recordingInfo" -- start level 7 tag for music title ,XMLFOREST( cc.source_album_title as "albumTitle" ,cc.product_album_promo_title as "promoTitle" ,cc.label as "label" ,cc.catalogue_no as "catalogNumber" ,cc.isrc as "isrc") ) ) -- end level 6 tag for music title ) -- end level 5 tag cue ) FROM creation_components cc WHERE cc.prod_cre_surr_id = 22736214
recordingInfo如果上面列出的列都为空,这将生成带有两个空标记的XML :
<Cue> <cueId>1</cueId> <cueStatusType>5</cueStatusType> <cueCupType>W</cueCupType> <musicWork> <musicTitle>CLARE@RADIOWORKS.CO.UK</musicTitle> <recordingInfo></recordingInfo> </musicWork> </Cue>
recordingInfo如果其中没有其他元素生成,如何停止包含空标记的它?
您可以使用嵌套XMLForest(.. as "recordingInfo")调用代替XMLElement("recordingInfo"):
XMLForest(.. as "recordingInfo")
XMLElement("recordingInfo")
SELECT XMLAGG(XMLElement("Cue" -- start level 5 tag for cue ,XMLFOREST( rownum as "cueId" ,cc.dn_ccst_status as "cueStatusType" ,decode(cc.dn_ccst_status,'5',cc.cup_code,NULL) as "cueCupType" ) ,XMLElement("musicWork" -- start level 6 tag for music title ,XMLFOREST(cc.title as "musicTitle") ,XMLFOREST( XMLFOREST(cc.source_album_title as "albumTitle" ,cc.product_album_promo_title as "promoTitle" ,cc.label as "label" ,cc.catalogue_no as "catalogNumber" ,cc.isrc as "isrc") as "recordingInfo" -- start level 7 tag for music title ) ) -- end level 6 tag for music title ) -- end level 5 tag cue ) FROM creation_components cc WHERE cc.prod_cre_surr_id = 22736214;
使用伪数据来匹配您的样本输出,并使用XMLSerialise包装器来美化输出:
XMLSerialise
WITH creation_components (prod_cre_surr_id, dn_ccst_status, cup_code, title, source_album_title, product_album_promo_title, label, catalogue_no, isrc) as ( SELECT 22736214, 5, 'W', 'CLARE@RADIOWORKS.CO.UK', null, null, null, null, null from dual ) SELECT XMLSERIALIZE(Document XMLAGG(XMLElement("Cue" -- start level 5 tag for cue ,XMLFOREST( rownum as "cueId" ,cc.dn_ccst_status as "cueStatusType" ,decode(cc.dn_ccst_status,'5',cc.cup_code,NULL) as "cueCupType" ) ,XMLElement("musicWork" -- start level 6 tag for music title ,XMLFOREST(cc.title as "musicTitle") ,XMLFOREST( XMLFOREST(cc.source_album_title as "albumTitle" ,cc.product_album_promo_title as "promoTitle" ,cc.label as "label" ,cc.catalogue_no as "catalogNumber" ,cc.isrc as "isrc") as "recordingInfo" -- start level 7 tag for music title ) ) -- end level 6 tag for music title ) -- end level 5 tag cue ) as CLOB INDENT SIZE = 2 ) FROM creation_components cc WHERE cc.prod_cre_surr_id = 22736214;
得到
XMLSERIALIZE(DOCUMENTXMLAGG(XMLELEMENT("CUE"--STARTLEVEL5TAGFORCUE,XMLFOREST(ROW -------------------------------------------------------------------------------- <Cue> <cueId>1</cueId> <cueStatusType>5</cueStatusType> <cueCupType>W</cueCupType> <musicWork> <musicTitle>CLARE@RADIOWORKS.CO.UK</musicTitle> </musicWork> </Cue>
在后面的列中使用非空数据:
WITH creation_components (prod_cre_surr_id, dn_ccst_status, cup_code, title, source_album_title, product_album_promo_title, label, catalogue_no, isrc) as ( SELECT 22736214, 5, 'W', 'CLARE@RADIOWORKS.CO.UK', null, null, 'RadioWorks', null, null from dual ) SELECT XMLSERIALIZE(Document XMLAGG(XMLElement("Cue" -- start level 5 tag for cue ,XMLFOREST( rownum as "cueId" ,cc.dn_ccst_status as "cueStatusType" ,decode(cc.dn_ccst_status,'5',cc.cup_code,NULL) as "cueCupType" ) ,XMLElement("musicWork" -- start level 6 tag for music title ,XMLFOREST(cc.title as "musicTitle") ,XMLFOREST( XMLFOREST(cc.source_album_title as "albumTitle" ,cc.product_album_promo_title as "promoTitle" ,cc.label as "label" ,cc.catalogue_no as "catalogNumber" ,cc.isrc as "isrc") as "recordingInfo" -- start level 7 tag for music title ) ) -- end level 6 tag for music title ) -- end level 5 tag cue ) as CLOB INDENT SIZE = 2 ) FROM creation_components cc WHERE cc.prod_cre_surr_id = 22736214;
额外的标记仍会出现:
XMLSERIALIZE(DOCUMENTXMLAGG(XMLELEMENT("CUE"--STARTLEVEL5TAGFORCUE,XMLFOREST(ROW -------------------------------------------------------------------------------- <Cue> <cueId>1</cueId> <cueStatusType>5</cueStatusType> <cueCupType>W</cueCupType> <musicWork> <musicTitle>CLARE@RADIOWORKS.CO.UK</musicTitle> <recordingInfo> <label>RadioWorks</label> </recordingInfo> </musicWork> </Cue>