2016-06-21 72 views
1

我有以下XML內容:如何從XML列在Postgres的一個屬性選擇數據

<?xml version="1.0" encoding="utf-8"?> 
<h:html xmlns="http://www.w3.org/2002/xforms" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:jr="http://openrosa.org/javarosa" xmlns:orx="http://openrosa.org/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <h:head> 
    <h:title>Demo12</h:title> 
    <model> 
     <instance> 
     <uploaded_form_bpdwls id="Demo12"> 
      <formhub> 
      <uuid/> 
      </formhub> 
      <Household_Number/> 
      <Survey_Name/> 
      <start/> 
      <end/> 
      <meta> 
      <instanceID/> 
      </meta> 
     </uploaded_form_bpdwls> 
     </instance> 
    </model> 
    </h:head> 
    <h:body> 
    <input ref="/uploaded_form_bpdwls/Household_Number"> 
     <label>Household Number</label> 
    </input> 
    <input ref="/uploaded_form_bpdwls/Survey_Name"> 
     <label>Survey Name</label> 
    </input> 
    </h:body> 
</h:html> 

在上面的XML內容,

  • 體內,有兩個輸入標籤不同的屬性(即@ref =「/ uploaded_form_bpdwls/Household_Number」)

  • 我想通過postgresSQL選擇表格格式的數據我想要「House Hold」和「Survey Name」作爲單獨的列。

  • 我不知道如何使用標籤的屬性來選擇數據。

是否有可能在單獨的列

選擇數據應該是什麼選擇查詢來實現這一目標?

回答

1

是你想要的嗎? :

with table1 as (
    select $$<?xml version="1.0" encoding="utf-8"?> 
<h:html xmlns="http://www.w3.org/2002/xforms" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:jr="http://openrosa.org/javarosa" xmlns:orx="http://openrosa.org/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<h:head> 
<h:title>Demo12</h:title> 
<model> 
    <instance> 
    <uploaded_form_bpdwls id="Demo12"> 
     <formhub> 
     <uuid/> 
     </formhub> 
     <Household_Number/> 
     <Survey_Name/> 
     <start/> 
     <end/> 
     <meta> 
     <instanceID/> 
     </meta> 
    </uploaded_form_bpdwls> 
    </instance> 
</model> 
</h:head> 
<h:body> 
<input ref="/uploaded_form_bpdwls/Household_Number"> 
    <label>Household Number</label> 
</input> 
<input ref="/uploaded_form_bpdwls/Survey_Name"> 
    <label>Survey Name</label> 
</input> 
</h:body> 
</h:html>$$::xml as xml_content 

) 

select myarray[1] val1,myarray[2] val2 from (
    select xpath('/h:html/h:body/i:input/i:label/text()',xml_content,ARRAY[ARRAY['h','http://www.w3.org/1999/xhtml'],ARRAY['i','http://www.w3.org/2002/xforms']]) myarray from table1 
) a 

對於多層次試試這個:

 with table1 as (
     select $$<?xml version="1.0" encoding="utf-8"?> 
     <h:html xmlns="http://www.w3.org/2002/xforms" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:jr="http://openrosa.org/javarosa" xmlns:orx="http://openrosa.org/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
      <h:head> 
      <h:title>Demo12</h:title> 
      <model> 
       <instance> 
       <uploaded_form_bpdwls id="Demo12"> 
        <formhub> 
        <uuid/> 
        </formhub> 
        <Household_Number/> 
        <Survey_Name/> 
        <start/> 
        <end/> 
        <meta> 
        <instanceID/> 
        </meta> 
       </uploaded_form_bpdwls> 
       </instance> 
      </model> 
      </h:head> 
      <h:body> 
      <div> 
      <input ref="/uploaded_form_bpdwls/Household_Number"> 
       <label>Household Number</label> 
      </input> 
      <input ref="/uploaded_form_bpdwls/Survey_Name"> 
       <label>Survey Name</label> 
      </input> 
      </div> 
      <div> 
      <input ref="/uploaded_form_bpdwls/Household_Number"> 
       <label>Household Number2</label> 
      </input> 
      <input ref="/uploaded_form_bpdwls/Survey_Name"> 
       <label>Survey Name2</label> 
      </input> 
      </div> 
      </h:body> 
     </h:html>$$::xml as xml_content 

     ) 

     select myarray[1] val1,myarray[2] val2 from (
      select xpath('/i:div/i:input/i:label/text()',xml_content,ARRAY[ARRAY['h','http://www.w3.org/1999/xhtml'],ARRAY['i','http://www.w3.org/2002/xforms']]) myarray from 
       (
       select unnest(xpath('/h:html/h:body/i:div',xml_content,ARRAY[ARRAY['h','http://www.w3.org/1999/xhtml'],ARRAY['i','http://www.w3.org/2002/xforms']])) xml_content from table1 
      ) div 
      ) a 
+0

感謝雷米......這正是我需要大約... –

+0

我需要您提供更多的幫助:請解釋爲什麼你已經在xpath中使用了前綴...以及它如何用於多級數據... –

+0

因爲在你的xml中,命名空間被定義爲:xmlns =「http://www.w3.org/2002/xforms」沒有前綴和xmlns:h =「http://www.w3.org/1999/xhtml」,前綴爲「h」,如果你沒有定義和使用前綴xpath查詢返回什麼(空數組) –