2015-03-05 2 views
1

29i имеют этот XML-строку:Как конфертировать из XML в таблицу?

<CP> 
    <V PV="1.29" PT="1.29" PB="1.29" ML="0.0" OB="Reg" /> 
    <V PV="0.77" PT="1.29" PB="1.29" ML="0.6" OB="Reg" /> 
    <V PV="0.77" PT="1.29" PB="0.65" ML="0.645" OB="Reg" /> 
</CP> 

и мне нужно создать таблицу (или набор записей), как это:

PV  PT  PB  ML  OB 
numeric numeric numeric numeric text 
-------- ------- -------- ------- ---- 
1.29  1.29 1.29  0.0  Reg 
0.77  1.29 1.29  0.6  Reg 
0.77  1.29 0.65  0.645 Reg 

Postgres 9.x

+0

Вы уже что-то пробовали? – frlan

+0

Да, я пытаюсь xpath, но не знаю, как получить значение атрибута. – Christian

ответ

1

Понял с помощью xpath с @ для получения значения атрибута и unnest:

select cast(cast((xpath('/V/@PV', node))[1] as text) AS numeric) as PV, 
     cast(cast((xpath('/V/@PT', node))[1] as text) AS numeric) as PT, 
     cast(cast((xpath('/V/@PB', node))[1] as text) AS numeric) as PB, 
     cast(cast((xpath('/V/@ML', node))[1] as text) AS numeric) as ML, 
     cast(cast((xpath('/V/@OB', node))[1] as text) AS text ) as OB 
from unnest(xpath('/CP/V', 
'<CP><V PV="1.29" PT="1.29" PB="1.29" ML="0.0" OB="Reg" /><V PV="0.77" PT="1.29" PB="1.29" ML="0.6" OB="Reg" /><V PV="0.77" PT="1.29" PB="0.65" ML="0.645" OB="Reg" /></CP>' 
)) node 
Смежные вопросы