如何摆脱不需要的空XML标签?

时间:2016-03-31 17:38:31

标签: sql xml oracle

在下面的SQL XML中,如果所有这些字段都为空,我想不显示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

如果上面列出的列都为null,则生成带有空recordingInfo标记的XML:

<Cue>
   <cueId>1</cueId>
   <cueStatusType>5</cueStatusType>
   <cueCupType>W</cueCupType>
   <musicWork>
       <musicTitle>CLARE@RADIOWORKS.CO.UK</musicTitle>
       <recordingInfo></recordingInfo>
   </musicWork>
</Cue>

如果其中没有生成其他元素,我该如何阻止它包括空recordingInfo标记?

1 个答案:

答案 0 :(得分:2)

您可以使用嵌套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包装来美化输出:

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>