问题描述
有没有人有在 postgres 中使用 XMLTABLE 函数的经验,他们可以帮助我。我在包含 XML 数据的表中有一个文本列,我想将这些数据转换为单独的记录。数据示例如下所示。
<conditions><Condition><name>Patellar luxation</name><VeNom>19808</VeNom><PRF>False</PRF><RUWF>False</RUWF><MaintenanceCosts>1</MaintenanceCosts><ConsequentialLoss>2.5</ConsequentialLoss><GroupMCS>1</GroupMCS><GroupCLS>2.5</GroupCLS><score>2.5</score><id>125</id><GroupId>1</GroupId><Groupscore>2.5</Groupscore><exclusionType>None</exclusionType><questions><QandA><question>Has your pet had an operation for this condition?</question><answer>No</answer></QandA><QandA><question>Does your pet have any other bone or joint problems?</question><answer>No</answer></QandA><QandA><question>Has the vet ever recommended that your pet lose weight?</question><answer>No</answer></QandA></questions><LinkedConditions><LinkedCondition Name="Complications with the other patellar / knee" VeNom="" Type="D"/><LinkedCondition Name="Cruciate ligament disease" VeNom="" Type="D"/></LinkedConditions></Condition></conditions>
我想将 QandA 分成这样的单个记录:-
Question Answer
--------- -------
Has your pet .... No
Does your pet have any other bone ... No
Has the vet ever recommended ... No
阅读了一些关于此的在线资料后,我想出了这个 sql,但收到一个错误
with data as
(
select questionsandanswers::xml as qa
from mytable
)
SELECT *
FROM data x,XMLTABLE('conditions/Condition/questions/QandA'
PASSING qa
COLUMNS
q text PATH 'question',a text PATH 'answer' )
ERROR: Could not parse XML document
DETAIL: line 1: Document is empty
^
sql state: 2200M
任何帮助将不胜感激。我使用的是 Postgresql V 11.9
解决方法
你的代码看起来是正确的,如果你直接在字符串上尝试
with data as (
select '<conditions><Condition><name>Patellar luxation</name><VeNom>19808</VeNom><PRF>False</PRF><RUWF>False</RUWF><MaintenanceCosts>1</MaintenanceCosts><ConsequentialLoss>2.5</ConsequentialLoss><GroupMCS>1</GroupMCS><GroupCLS>2.5</GroupCLS><Score>2.5</Score><id>125</id><GroupId>1</GroupId><GroupScore>2.5</GroupScore><exclusionType>None</exclusionType><questions><QandA><question>Has your pet had an operation for this condition?</question><answer>No</answer></QandA><QandA><question>Does your pet have any other bone or joint problems?</question><answer>No</answer></QandA><QandA><question>Has the vet ever recommended that your pet lose weight?</question><answer>No</answer></QandA></questions><LinkedConditions><LinkedCondition Name="Complications with the other patellar / knee" VeNom="" Type="D"/><LinkedCondition Name="Cruciate ligament disease" VeNom="" Type="D"/></LinkedConditions></Condition></conditions>'::xml val)
SELECT q,a
FROM data x,XMLTABLE('conditions/Condition/questions/QandA'
PASSING val
COLUMNS
q text PATH 'question',a text PATH 'answer' )
;
提供预期的输出
q | a
---------------------------------------------------------+----
Has your pet had an operation for this condition? | No
Does your pet have any other bone or joint problems? | No
Has the vet ever recommended that your pet lose weight? | No
(3 rows)
但是,如果针对如下所示的空字符串运行相同的代码
with data as (
select ''::xml val)
SELECT q,a text PATH 'answer' )
;
它抛出你提到的错误
ERROR: could not parse XML document
DETAIL: line 1: Document is empty
您应该更多地关注理解/消除带有空 XML 字符串的行的连接
,好的,看来我的语法是正确的,但我的文件中有一些空数据 问答栏目。全部整理完毕