如何从postgres中的xml列中选择具有属性的数据
我有以下xml内容:如何从postgres中的xml列中选择具有属性的数据,xml,postgresql,Xml,Postgresql,我有以下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://ope
<?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=/upload\u form\u bpdwls/househouse\u Number
我试图通过postgresSQL选择表格式的数据。我希望房屋保有和调查名称作为单独的列
我不知道如何使用标记的属性选择数据
是否可以在单独的列下选择数据
要实现此目的,select查询应该是什么?这是您想要的吗
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
我需要您的更多帮助:请解释一下为什么在xpath中使用前缀。。。以及如何将其用于多级数据……因为在xml中,名称空间是定义的:xmlns=用于不带前缀的标记,xmlns:h=用于带前缀h的标记,如果不定义和使用前缀,xpath查询将不返回空arrayRemy,请看一下: