2016-06-21 15 views
1

私は、次のXMLコンテンツを持っている:のpostgresにXML列から属性を持つデータを選択する方法

体内
<?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コンテンツで

  • 、2個の入力タグがあります

  • postgresSQLでテーブル形式のデータを選択しようとしていますが、「ハウスホールド」と「アンケート名」が必要です。別の列として。

  • タグの属性を使用してデータを選択する方法はありません。

それは、これを達成するために、選択クエリがどうあるべきか、別の列

の下でデータを選択することは可能ですか?

答えて

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"という接頭辞を持つタグの場合、接頭辞を定義して使用しない場合、xpathクエリは何も返しません(空の配列) –

関連する問題