2016-03-31 8 views
1

In der folgenden SQL XML möchte ich die recordingInfo Tag nicht zeigen, wenn alle diese Felder null sind:Wie kann ich von unerwünschten leeren XML-Tags loswerden?

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

Dies ist meine aktuelle Abfrage ist:

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 

Dies erzeugt XML sowohl eine leere recordingInfo Tag, wenn die oben aufgeführten Spalten alle null sind:

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

Wie kann ich aufhören, einschließlich der leeren recordingInfo Tag, wenn es keine anderen Elemente erzeugt in ihm sind?

Antwort

2

können Sie einen verschachtelten XMLForest(.. as "recordingInfo") Aufruf anstelle den 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; 

mit Dummy-Daten Ihrer Beispielausgabe entsprechen, und einen XMLSerialise Wrapper um die Ausgabe zu beschönigen:

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; 

bekommt

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

Mit n on-Null-Daten in den späteren Spalten:

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; 

die zusätzliche Markierung erscheint nach wie vor:

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> 
+0

Correct wieder Alex Poole Sie sind truley der Experte für XML SQL .. vielen Dank für Ihre Hilfe , es hat das erste Mal damit in einer zusätzlichen XMLFOREST eingeschlossen –

Verwandte Themen