在postgres中读取XML属性

问题描述 投票:1回答:1

我在Postgres中使用XML第一个计时器并面临一个问题。我在名为XMLCONTENT的变量中有xml以下

<?xml version="1.0" encoding="UTF-8"?>
<Actions>
   <Action ActionID="90e0dbef-c23a-4fcd-bfa8-75d8bfa2c9e2" />
   <Action ActionID="6a1998e1-70f1-4611-992a-7a27e2834c35" />
   <Action ActionID="43dd9a91-c6d3-4980-b211-9b3780f04305" />
   <Action ActionID="cdf01821-ac28-45a9-abf8-a7d7c9426518" />
   <Action ActionID="e86fac8a-84e3-41ba-8bee-c7ffd1ac8ee5" />
   <Action ActionID="a68dd878-ba1e-4fd9-b436-cdc15eccffb6" />
   <Action ActionID="cd863a5a-83e9-489e-b24d-ff6638c5b190" />
   <Action ActionID="720ba9c7-b797-4b2e-913e-11ac3ecd7b7f" />
   <Action ActionID="b6b35d0d-938e-45d3-96d1-0c8ca3ad59f3" MessageID="42f40c3a-4426-4506-86c5-222fb03c2114" />
</Actions>

我想从这个XML中提取细节,我正在使用下面的查询

Select  
    Unnest(xpath('//@ActionID',XMLCONTENT)) as ID,
    Unnest(xpath('//@MessageID',XMLCONTENT)) as MessageID,
    Unnest(xpath('//@Operator',XMLCONTENT)) as Operator

但我得到错误的输出,如下所示

enter image description here

MessageID与错误的actionID链接。遍历此XML的正确方法是什么?

sql xml postgresql postgresql-9.6
1个回答
2
投票

您的查询不起作用的原因是在选择列表中使用unnest():每个unnest()调用都会在结果中添加一个新行。

您需要在from子句中使用unnest为每个<Action>元素创建一行:

with data (xmlcontent) as (
  values ('
  <Actions>
     <Action ActionID="90e0dbef-c23a-4fcd-bfa8-75d8bfa2c9e2" />
     <Action ActionID="6a1998e1-70f1-4611-992a-7a27e2834c35" />
     <Action ActionID="43dd9a91-c6d3-4980-b211-9b3780f04305" />
     <Action ActionID="cdf01821-ac28-45a9-abf8-a7d7c9426518" />
     <Action ActionID="e86fac8a-84e3-41ba-8bee-c7ffd1ac8ee5" />
     <Action ActionID="a68dd878-ba1e-4fd9-b436-cdc15eccffb6" />
     <Action ActionID="cd863a5a-83e9-489e-b24d-ff6638c5b190" />
     <Action ActionID="720ba9c7-b797-4b2e-913e-11ac3ecd7b7f" />
     <Action ActionID="b6b35d0d-938e-45d3-96d1-0c8ca3ad59f3" 
             MessageID="42f40c3a-4426-4506-86c5-222fb03c2114" />
  </Actions>'::xml)
)
select (xpath('//@ActionID', xt.action))[1] as id, 
       (xpath('//@MessageID', xt.action))[1] as message_id
from data
  cross join unnest(xpath('/Actions/Action', xmlcontent)) as xt(action);

返回:

id                                   | message_id                          
-------------------------------------+-------------------------------------
90e0dbef-c23a-4fcd-bfa8-75d8bfa2c9e2 |                                     
6a1998e1-70f1-4611-992a-7a27e2834c35 |                                     
43dd9a91-c6d3-4980-b211-9b3780f04305 |                                     
cdf01821-ac28-45a9-abf8-a7d7c9426518 |                                     
e86fac8a-84e3-41ba-8bee-c7ffd1ac8ee5 |                                     
a68dd878-ba1e-4fd9-b436-cdc15eccffb6 |                                     
cd863a5a-83e9-489e-b24d-ff6638c5b190 |                                     
720ba9c7-b797-4b2e-913e-11ac3ecd7b7f |                                     
b6b35d0d-938e-45d3-96d1-0c8ca3ad59f3 | 42f40c3a-4426-4506-86c5-222fb03c2114

在选择列表中,您知道每个'//@ActionID'只返回一个元素,因此不再需要在该级别使用unexst。

在线示例:https://rextester.com/MWBCEN37238


如果您使用的是Postgres 10或更高版本,那么使用XMLTABLE会更简单:

select xt.*
from data 
     cross join xmltable ('/Actions/Action'
               passing xmlcontent
               columns id         uuid path '@ActionID', 
                       message_id uuid path '@MessageID'
              ) as xt;

在线示例:https://dbfiddle.uk/?rdbms=postgres_10&fiddle=1e70be54c25a42db5ebff9a996423920


© www.soinside.com 2019 - 2024. All rights reserved.