YogeshR
YogeshR

Reputation: 1736

Read XML attributes in postgres

I am using XML first timer in Postgres and facing an issue. I have below xml in a variable named XMLCONTENT

<?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>

I want to extract details from this XML and I am using below query

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

but i am getting wrong output as shown below

enter image description here

MessageID is linked with the wrong actionID. What is the correct way to traverse this XML?

Upvotes: 2

Views: 2637

Answers (1)

user330315
user330315

Reputation:

The reason your query is not working is the use of unnest() in the select list: each unnest() call adds a new row to the result.

You need to use unnest in the from clause to create one row for each <Action> element:

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);

Returns:

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

In the select list, you know that each '//@ActionID' only returns a single element, so there is no need to use unnest at that level any more.

Online example: https://rextester.com/MWBCEN37238


If you were using Postgres 10 or later, this would be a bit simpler with XMLTABLE:

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

Online example: https://dbfiddle.uk/?rdbms=postgres_10&fiddle=1e70be54c25a42db5ebff9a996423920


Upvotes: 4

Related Questions