2016-03-21 9 views
0

XML SQLクエリを作成しましたが、XMLヘッダーを追加する際の解決策を見つけるのは難しいです(下記参照)。返されたデータベーステーブルのすべての生産レコードについて、親ヘッダ< メッセージ>タグとそのメッセージタグ内にある必要があります。< messageSequenceId>レコード数(最初の生産レコードの場合は1、2番目の生産レコードの場合は1)生産記録など...)。SQL XML XMLELEMENT - ルートヘッダーをより多く手に入れよう

配信タグ< 配信>は先頭に一度だけ表示され、あまりにも< /デリバリー>タグトップでフィニッシュしています。

また、出力の最後にタグと終了タグを持つ< AV-XMLがあります。

次の出力の例を参照してください。私はプログラムを終了するためにこれらを必要とします。

私はどんな返事でも前もってありがとうございます。 SQLクエリは、以下の通りです

<?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> 

、データベースのバージョンは11gリリース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> 
+0

このデータは、XMLを作成した瞬間に入手できますか?現時点で使用しているクエリを表示して、クエリが出てきたXMLを正確に表示してください。 – Shnugo

+0

あなたのRDBMS(製品とバージョン)を指定してくださいXMLは**実際のソフトウェア(SQL Server、Oracle、MySql ...)に非常に依存します** – Shnugo

+0

私はクエリ、データベースバージョン、クエリ –

答えて

0

MessageProductionための外側要素であり、そしてmessageSequenceIdは、メイン・クエリのためのちょうどrownumであるように見えます - あなたは、内側のクエリでcueIDを取得しているのと同じように。

したがって、プロダクションではなくメッセージのXMLAggに対してクエリを変更できます。次に、新しい配信要素を含む最上位の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からのオフセットのタイムゾーンを取得する)ことになっているの両方が、あなたが持つかもしれないと推測しましたそれらを生成する別の方法。

+0

素晴らしい、それは初めて働いた.... Alex Poole。あなたは星です、あなたの助けをありがとうございました。 –

+0

@ShaunKinnairおそらく、XMLForestを使用してさらにテーブルを結合したり、おそらくdbms_xmlgenにすることもできます。名前空間情報を生成するための良い方法であるはずです(または、ノードのどれもそれを使用しないので省略します)。それはあなたのために働くことがうれしいです。 –

+0

XMLForestでimpovingと言い、テーブルに加わるとはどういう意味ですか?私はSQL XMLを初めて使っているので、XMLForestについて詳しくは分かりません。 –

関連する問題