Oracle
 sql >> Baza danych >  >> RDS >> Oracle

Jak pozbyć się niechcianych pustych tagów XML?

Możesz użyć zagnieżdżonego XMLForest(.. as "recordingInfo") wywołanie zamiast 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;

Z fikcyjnymi danymi, które pasują do przykładowych danych wyjściowych, oraz XMLSerialise wrapper do upiększania wyjścia:

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;

dostaje

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

Z danymi innymi niż null w późniejszych kolumnach:

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;

dodatkowy tag nadal się pojawia:

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>



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Czy SELECT FOR UPDATE zapobiega wstawianiu innych połączeń, gdy wiersz nie jest obecny?

  2. Nie można skompilować PL/SQL za pomocą BULK COLLECT i FORALL

  3. ORA-02287:numer sekwencji jest tutaj niedozwolony

  4. nie można zapisać danych o dużym rozmiarze za pomocą UTL_FILE.PUT_LINE

  5. Wyświetlaj nazwy wszystkich ograniczeń dla tabeli w Oracle SQL