2017-02-02 106 views
1

我正在使用版本PostgreSQL 8.3.12。將列XML數據轉換爲行

表的列名:的XMLTest

iddata

Data列包含看起來像這樣的XML數據:

<?xml version="1.0" encoding="utf-8"?> 
<ItemData> 
    <ReviewComments>&lt;?xml version="1.0" encoding="utf-8"?&gt; 
    </ReviewComments> 
    <SmartTextData RootProtect="True"> 
     <STI_Summary IID="d10c5cbf-f5cf-4478-9f33-4580c1930413" IR="True"> 
      <ObjectValue /> 
      <CP /> 
      <SIProps ST="4" PL="False" PLS="" /> 
      <STI_SummaryActiveProblemsField LIID="cdbd7044-ccde-11db-8cba-df0a56d89593" IID="37742a5f-7998-4715-8d43-0d7a19284d44" IR="True" RW="1"> 
      <HD Title="Active Problems" /> 
      <ObjectValue /> 
      <CP> 
       <PosReplace /> 
       <NegReplace /> 
      </CP> 
      <SIProps ST="4" PL="False" PLS="" /> 
      <STI_US> 
       <ObjectValue> 
        <TextValue> 
         <![CDATA[ 
]]> 
        </TextValue> 
       </ObjectValue> 
       <CP /> 
       <SIProps ST="1" SS=" " PL="False" PLS="" /> 
      </STI_US> 
      <STI_DxItem LIID="71194038-8ffb-488b-8af5-5f1f1a679115" IID="aaf2de4e-2f1f-409b-87b7-b7265bec37db" RW="1"> 
       <HD Title="Coronary artery disease " /> 
       <ObjectValue> 
        <Code> 
         <CodingSystem>ICD-9 CM</CodingSystem> 
         <Value>414.01</Value> 
        </Code> 
        <Code> 
         <CodingSystem>SWICPC</CodingSystem> 
         <Value>08.0.K76.CIR</Value> 
        </Code> 
       </ObjectValue> 
      </STI_DxItem > 
     </STI_Summary> 
    </SmartTextData> 
</ItemData> 

我想潑XML標籤IID和代碼數據,以各自的ID列。

預期輸出:

  ID LIID           Code_Value CodingSystem 
      1 d10c5cbf-f5cf-4478-9f33-4580c1930413  NULL 
      1 37742a5f-7998-4715-8d43-0d7a19284d44  NULL 
      1 aaf2de4e-2f1f-409b-87b7-b7265bec37db  414.01   IC CM 
      1 aaf2de4e-2f1f-409b-87b7-b7265bec37db  08.0.K76.CIR SWICPC 

注:我使用的版本的PostgreSQL 8.3.12與xmlpath中不工作的這一些新的語法。

只是我想要將XML數據轉換爲行列結構。

感謝您閱讀本文。

+0

文件不能被驗證,存在丟失標籤.http://www.xmlvalidation.com/ – McNets

+0

你的XML是不是正確的平衡,'STI_DxItem'不關閉。 – jcaron

+0

@jcaron代碼編輯。是否有任何WAT在這裏泄漏了所有的LLID。 –

回答

0

看着SO問題,我發現這一點:

ERROR: function unnest(integer[]) does not exist in postgresql

根據這個問題,你可以通過你自己實現unnest,在這樣的一維數組的解決方案:

CREATE OR REPLACE FUNCTION unnest2(anyarray) 
    RETURNS SETOF anyelement AS 
$BODY$ 
    SELECT $1[i] FROM generate_series(array_lower($1,1), array_upper($1,1)) i; 
$BODY$ LANGUAGE sql IMMUTABLE; 

這個功能應該允許用一個句子來查詢數據:

SELECT xpath('/ItemData/SmartTextData/STI_Summary/STI_DxItem/@IID', data, array[array['aaa','example.com']]) as IID, 
     unnest2(xpath('/ItemData/SmartTextData/STI_Summary/ObjectValue/Code/CodingSystem/text()', data, array[array['aaa','example.com']])) as Code, 
     unnest2(xpath('/ItemData/SmartTextData/STI_Summary/ObjectValue/Code/Value/text()', data, array[array['aaa','example.com']])) as Value 
from xmltest2; 

請注意,由於您的XML數據沒有模式,因此您在您的評論中指出了我使用的array[array['aaa','example.com']]

我已經在rextester中測試過它,它工作。

+--------------------------------------+----------+--------------+ 
|     iid     | code | value  | 
+--------------------------------------+----------+--------------+ 
| aaf2de4e-2f1f-409b-87b7-b7265bec37db | ICD-9 CM | 414.01  | 
+--------------------------------------+----------+--------------+ 
| aaf2de4e-2f1f-409b-87b7-b7265bec37db | SWICPC | 08.0.K76.CIR | 
+--------------------------------------+----------+--------------+ 

檢查在這裏:Rextester

+0

它沒有在8.3 vesrion上炒作。請幫忙。但在這裏找到onlt LIID的另一種方法是? –

+0

但unnest不 – McNets

+0

獲取錯誤'未知函數Xpath' –