SQL XML XMLELEMENT - 将更多内容放入根 header

SQL XML XMLELEMENT - puuting more into root header

我创建了一个 XML SQL 查询,但我发现很难找到在 XML 的 header 中添加更多内容的解决方案(请参阅以下)。对于从数据库 table 带回的每条生产记录,必须有一个 parent header <message> 标记,并且在该消息标记内 <messageSequenceId> 有一个记录计数(即数字,第一个生产记录为 1,第二个生产记录为 2,依此类推...)。

交付标签 <delivery> 只在顶部出现一次,并且在顶部也有一个完成标签 </delivery> .

还有一个<AV-XML>,它在输出的开始处有一个标签,在输出的结尾处有一个结束标签。

请参阅下面的输出示例。我只需要这些来完成程序。

提前感谢我收到的任何回复。

 <?xml version="1.0" encoding="UTF-8"?>
 <AV-XML xmlns="http://www.iceservices.com/0.7/AV-XML" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.iceservices.com/0.7/AV-XML AV-XML.xsd">
 <delivery>  
    <supplierType>ADMIN-AGENCY</supplierType>   -- this is hard coded
    <supplierId>GEMA</supplierId>   -- this is hard coded
    <supplierName>Gesellschaft für musikalische Aufführungs- und mechanische</supplierName>   -- this is hard coded
    <supplierContactPerson>Jens Kindermann</supplierContactPerson>   -- this is hard coded
    <recieverContactPerson>n.n.</recieverContactPerson>  -- this is hard coded
    <format>AV-XML</format>   -- this is hard coded
    <versionId>0.7</versionId>    -- this is hard coded
    <creationDateTime>2016-03-09T12:24:46+01:00</creationDateTime> 
    <deliveryNo>201603091224460100</deliveryNo> 
    <otherInformation>MIGRATION FIRST DELIVERY</otherInformation>    -- this is hard coded
</delivery>
<message>  -- a message tag for each production record
    <messageSequenceId>1</messageSequenceId>
    <messageType>NEW</messageType>        -- this is hard coded
    <otherInformation>MIGRATION FIRST DELIVERY</otherInformation>  -- this is hard coded
    <production>
          production XML records information that I have already done in my query----------
     <production>
</message>
</AV-XML>

SQL查询如下,数据库版本为11g Release 1.

  select XMLROOT(    
           xmlagg(XMLElement("Production"  -- start level 1 tag for production
                                 ,XMLElement("prodCategoryType",prod.production_type)  
                                 ,XMLElement("prodStatusType",prod.dn_prst_status )  
                                 ,XMLElement("prodActive",'true')  
                                 ,XMLElement("prodCueStatusType",decode(prod.music_content_indicator,'Y','YES','NO')) 
                                 ,(select        XMLAGG(XMLElement("Cue"  -- start level 2 tag cue
                                                               ,XMLElement("cueId", rownum)
                                                                ,XMLElement("cueStatusType", cc.dn_ccst_status) 
                                                                ,XMLElement("cueCupType", 'NULL')
                                                                 ,XMLElement("cueType", 'NULL')
                                                                 ,XMLElement("cueOriginType", cc.gc_origin_of_cue_code) 
                                                                  ) 
                                                         )    -- end level 2 tag cue
                                    from prblk.creation_components cc  
                                    where cc.prod_cre_surr_id = prod.cre_surr_id
                                  ) ,
                                    XMLElement("endofproduction", prod.production_source) 
                              )  -- end level 1 tag for production
                  )  -- xmllag for production                                 
         , version '1.0'
         ) --  XMLROOT
  AS XMLRESULT   
  FROM prblk.productions prod 
  where prod.cre_surr_id in (1753959927,1753959929)

当前输出如下。

<?xml version="1.0"?>
  <Production>
  <prodCategoryType>EPI</prodCategoryType>
  <prodStatusType>5</prodStatusType>
  <prodActive>true</prodActive>
  <prodCueStatusType>YES</prodCueStatusType>
  <Cue>
     <cueId>1</cueId>
     <cueStatusType>2a</cueStatusType>
     <cueCupType>NULL</cueCupType>
     <cueType>NULL</cueType>
     <cueOriginType/>
   </Cue>
   <Cue>
     <cueId>2</cueId>
     <cueStatusType>2a</cueStatusType>
     <cueCupType>NULL</cueCupType>
     <cueType>NULL</cueType>
     <cueOriginType/>
   </Cue>
   <Cue>
     <cueId>3</cueId>
     <cueStatusType>2a</cueStatusType>
     <cueCupType>NULL</cueCupType>
     <cueType>NULL</cueType>
     <cueOriginType/>
   </Cue>
   <endofproduction>AP</endofproduction>
 </Production>
 <Production>
   <prodCategoryType>EPI</prodCategoryType>
   <prodStatusType>5</prodStatusType>
   <prodActive>true</prodActive>
   <prodCueStatusType>YES</prodCueStatusType>
   <Cue>
     <cueId>1</cueId>
     <cueStatusType>2a</cueStatusType>
     <cueCupType>NULL</cueCupType>
     <cueType>NULL</cueType>
     <cueOriginType/>
   </Cue>
   <Cue>
    <cueId>2</cueId>
    <cueStatusType>2a</cueStatusType>
    <cueCupType>NULL</cueCupType>
    <cueType>NULL</cueType>
    <cueOriginType/>
   </Cue>
   <endofproduction>AP</endofproduction>
</Production>

看起来 MessageProduction 的外部元素,而 messageSequenceId 只是主查询的 rownum - 类似于您的方式在内部查询中重新获得 cueID

因此您可以将查询修改为 XML消息上的 Agg 而不是生产;然后有一个顶级 AV-XML 节点,其中包含一个新的传递元素和消息聚合。

SELECT XMLRoot(
  XMLElement("AV-XML"  -- start level 1 tag for AV-XML
    ,XMLAttributes('http://www.iceservices.com/0.7/AV-XML' as "xmlns"
      ,'http://www.w3.org/2001/XMLSchema' as "xmlns:xs"
      ,'http://www.w3.org/2005/xpath-functions' as "xmlns:fn"
      ,'http://www.w3.org/2001/XMLSchema-instance' as "xmlns:xsi"
      ,'http://www.iceservices.com/0.7/AV-XML AV-XML.xsd' as "xsi:schemaLocation")
    ,XMLElement("Delivery"  -- -- start level 2 tag for delivery
      ,XMLElement("supplierType", 'ADMIN-AGENCY')
      ,XMLElement("supplierId", 'GEMA')
      ,XMLElement("supplierName"
        ,'Gesellschaft für musikalische Aufführungs- und mechanische')
      ,XMLElement("supplierContactPerson", 'Jens Kindermann')
      ,XMLElement("recieverContactPerson", 'n.n.')
      ,XMLElement("format", 'AV-XML')
      ,XMLElement("versionId", '0.7')
      ,XMLElement("creationDateTime"
        ,to_char(systimestamp, 'YYYY-MM-DD"T"HH24:MI:SSTZHTZM'))
      ,XMLElement("deliveryNo"
        ,replace(to_char(systimestamp, 'YYYYMMDDHH24MISSTZHTZM'), '+', null))
      ,XMLElement("otherInformation", 'MIGRATION FIRST DELIVERY')
    )
    ,XMLAgg(  -- start XMLAgg for messages
      XMLElement("Message"  -- start level 2 tag for message
        ,XMLElement("messageSequenceId", rownum)
        ,XMLElement("messageType", 'NEW')
        ,XMLElement("otherInformation", 'MIGRATION FIRST DELIVERY')
        ,XMLElement("Production"  -- start level 3 tag for production
          ,XMLElement("prodId", rownum)
          ,XMLElement("prodCategoryType",prod.production_type)
          ,XMLElement("prodStatusType",prod.dn_prst_status )
          ,XMLElement("prodActive",'true')
          ,XMLElement("prodCueStatusType"
            ,decode(prod.music_content_indicator,'Y','YES','NO'))
          ,(
            SELECT XMLAgg(  -- start XMLAgg for cues
              XMLElement("Cue"  -- start level 4 tag for cue
                ,XMLElement("cueId", rownum)
                ,XMLElement("cueStatusType", cc.dn_ccst_status)
                ,XMLElement("cueCupType", 'NULL')
                ,XMLElement("cueType", 'NULL')
                ,XMLElement("cueOriginType", cc.gc_origin_of_cue_code)
              )  -- end level 4 tag cue
            )  -- end XMLAgg for cues
            FROM creation_components cc
            WHERE cc.prod_cre_surr_id = prod.cre_surr_id
          )
          ,XMLElement("endofproduction", prod.production_source)
        )  -- end level 3 tag for production
      )  -- end level 2 tag for message
    )  -- end XMLAgg for messages
  ) -- end AV-XML
  , version '1.0'
) --  XMLROOT
AS XMLRESULT
FROM productions prod
WHERE prod.cre_surr_id in (1753959927,1753959929);

你还没有说 creationDateTimedeliveryNo 来自哪里,所以我猜他们都应该是提取时间(从 systimestamp 得到时区偏移量),但您可能有另一种方法来生成它们。