Shaun Kinnair
Shaun Kinnair

Reputation: 535

How can I get rid of unwanted empty XML tags?

In the below SQL XML I want to not show the recordingInfo tag if all these fields are null:

cc.source_album_title
cc.product_album_promo_title
cc.label
cc.catalogue_no
cc.isrc

This is my current query:

 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

This generates XML with both an empty recordingInfo tag if the columns listed above are all null:

<Cue>
   <cueId>1</cueId>
   <cueStatusType>5</cueStatusType>
   <cueCupType>W</cueCupType>
   <musicWork>
       <musicTitle>[email protected]</musicTitle>
       <recordingInfo></recordingInfo>
   </musicWork>
</Cue>

How can I stop it including the empty recordingInfo tag if there are no other elements generated within it?

Upvotes: 2

Views: 3236

Answers (1)

Alex Poole
Alex Poole

Reputation: 191235

You can use a nested XMLForest(.. as "recordingInfo") call instead of the 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;

With dummy data to match your sample output, and an XMLSerialise wrapper to prettify the output:

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', '[email protected]',
    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;

gets

XMLSERIALIZE(DOCUMENTXMLAGG(XMLELEMENT("CUE"--STARTLEVEL5TAGFORCUE,XMLFOREST(ROW
--------------------------------------------------------------------------------
<Cue>
  <cueId>1</cueId>
  <cueStatusType>5</cueStatusType>
  <cueCupType>W</cueCupType>
  <musicWork>
    <musicTitle>[email protected]</musicTitle>
  </musicWork>
</Cue>

With non-null data in the later columns:

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', '[email protected]',
    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;

the extra tag still appears:

XMLSERIALIZE(DOCUMENTXMLAGG(XMLELEMENT("CUE"--STARTLEVEL5TAGFORCUE,XMLFOREST(ROW
--------------------------------------------------------------------------------
<Cue>
  <cueId>1</cueId>
  <cueStatusType>5</cueStatusType>
  <cueCupType>W</cueCupType>
  <musicWork>
    <musicTitle>[email protected]</musicTitle>
    <recordingInfo>
      <label>RadioWorks</label>
    </recordingInfo>
  </musicWork>
</Cue>

Upvotes: 2

Related Questions