xml - 在 Postgres 中使用 xmltable
问题描述
有没有人有在 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
FROM data x,
XMLTABLE('conditions/Condition/questions/QandA'
PASSING val
COLUMNS
q text PATH 'question',
a text PATH 'answer' )
;
它抛出你提到的错误
ERROR: could not parse XML document
DETAIL: line 1: Document is empty
您应该更多地专注于理解/消除加入带有空 XML 字符串的行
推荐阅读
- bash - 当 shell 脚本使用 bash -c 调用自身时,包括 $@ 以传递所有命令行参数
- xml - 如何使用 Powershell 访问 CDATA 中的 XML 数据
- postgresql - 规范化 postgresql 中的人名
- c - 从内核模块中的挂载点识别可移动设备
- django - Django 自定义验证导致视图返回无
- javascript - 将 array[i].response 与通过函数参数传递的字符串进行比较
- python - 如何将 m1 OHLC 数据转换为 m15 或 pandas 中的任何时间范围 OHLC?
- python - Sublime Text 3 中的 Python:oslist 目录;仅列出特定的扩展名
- ios - 共享文件失败,因为文件名太长
- python - 我可以使用 Pillow 查找连接的组件吗?