postgres复杂xml解析
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了postgres复杂xml解析相关的知识,希望对你有一定的参考价值。
我在xml下面,员工可以有多个报告人,每个报告人可以有多个报告人,但是层次结构最多可以有4个内部报告人。我需要此XML的结果像下面所示的表格。
我正在使用以下postgres查询,我的计划是拥有4个CTE,但是它失败并显示错误could not parse XML document
我在这里做错了。
<department>
<head>
<employee>emp 1</employee>
<cell col="2">M-y</cell>
<cell col="3">T-y</cell>
<cell col="4">W-n</cell>
<cell col="5">T-y</cell>
<cell col="6">F-n</cell>
<reportees level="1">
<employee>emp 11</employee>
<cell col="2">M-n</cell>
<cell col="3">T-n</cell>
<cell col="4">W-n</cell>
<cell col="5">T-y</cell>
<cell col="6">F-n</cell>
<reportees level="2">
<employee>emp 111</employee>
<cell col="2">M-n</cell>
<cell col="3">T-n</cell>
<cell col="4">W-n</cell>
<cell col="5">T-y</cell>
<cell col="6">F-n</cell>
</reportees>
<reportees level="2">
<employee>emp 10034</employee>
<cell col="2">M-n</cell>
<cell col="3">T-n</cell>
<cell col="4">W-n</cell>
<cell col="5">T-y</cell>
<cell col="6">F-n</cell>
</reportees>
</reportees>
<reportees level="1">
<employee>emp 12</employee>
<cell col="2">M-n</cell>
<cell col="3">T-n</cell>
<cell col="4">W-n</cell>
<cell col="5">T-y</cell>
<cell col="6">F-n</cell>
</reportees>
<reportees level="1">
<employee>emp 13</employee>
<cell col="2">M-n</cell>
<cell col="3">T-n</cell>
<cell col="4">W-n</cell>
<cell col="5">T-y</cell>
<cell col="6">F-n</cell>
<reportees level="2">
<employee>emp 131</employee>
<cell col="2">M-n</cell>
<cell col="3">T-n</cell>
<cell col="4">W-n</cell>
<cell col="5">T-y</cell>
<cell col="6">F-n</cell>
<reportees level="3">
<employee>emp 1311</employee>
<cell col="2">M-n</cell>
<cell col="3">T-n</cell>
<cell col="4">W-n</cell>
<cell col="5">T-y</cell>
<cell col="6">F-n</cell>
</reportees>
</reportees>
</reportees>
</head>
</department>
</company>
WITH CTE AS (
SELECT xmltable.*
FROM xmldata,
XMLTABLE('//company/department/head'
PASSING data COLUMNS
employee text PATH 'employee'
, monday text PATH 'cell[1]'
, tuesday text PATH 'cell[2]'
, wednesday text PATH 'cell[3]'
, thuresday text PATH 'cell[4]'
, friday text PATH 'cell[5]'
, reportees XML PATH 'reportees'))
SELECT *
FROM CTE
LEFT JOIN LATERAL XMLTABLE ('reportees' PASSING reportees COLUMNS
employee text PATH 'employee'
, monday text PATH 'cell[1]'
, tuesday text PATH 'cell[2]'
, wednesday text PATH 'cell[3]'
, thuresday text PATH 'cell[4]'
, friday text PATH 'cell[5]'
, reportees XML PATH 'reportees') ON TRUE;
<table border='1'>
<thead>
<tr>
<td>Employee</td>
<td>Monday</td>
<td>Tuesday</td>
<td>Wednesday</td>
<td>Thuresday</td>
<td>Friday</td>
<td>Employee_1</td>
<td>Monday_1</td>
<td>Tuesday_1</td>
<td>Wednesday_1</td>
<td>Thuresday_1</td>
<td>Friday_1</td>
<td>Employee_2</td>
<td>Monday_2</td>
<td>Tuesday_2</td>
<td>Wednesday_2</td>
<td>Thuresday_2</td>
<td>Friday_2</td>
<td>Employee_3</td>
<td>Monday_3</td>
<td>Tuesday_3</td>
<td>Wednesday_3</td>
<td>Thuresday_3</td>
<td>Friday_3</td>
</tr>
</thead>
<tbody>
<tr>
<td>emp 1</td>
<td>M-y</td>
<td>T-y</td>
<td>W-n</td>
<td>T-y</td>
<td>F-n</td>
<td>emp 11</td>
<td>M-n</td>
<td>T-n</td>
<td>W-n</td>
<td>T-y</td>
<td>F-n</td>
<td>emp 111</td>
<td>M-n</td>
<td>T-n</td>
<td>W-n</td>
<td>T-y</td>
<td>F-n</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>emp 1</td>
<td>M-y</td>
<td>T-y</td>
<td>W-n</td>
<td>T-y</td>
<td>F-n</td>
<td>emp 11</td>
<td>M-n</td>
<td>T-n</td>
<td>W-n</td>
<td>T-y</td>
<td>F-n</td>
<td>emp 10034</td>
<td>M-n</td>
<td>T-n</td>
<td>W-n</td>
<td>T-y</td>
<td>F-n</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>emp 1</td>
<td>M-y</td>
<td>T-y</td>
<td>W-n</td>
<td>T-y</td>
<td>F-n</td>
<td>emp 12</td>
<td>M-n</td>
<td>T-n</td>
<td>W-n</td>
<td>T-y</td>
<td>F-n</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>emp 1</td>
<td>M-y</td>
<td>T-y</td>
<td>W-n</td>
<td>T-y</td>
<td>F-n</td>
<td>emp 13</td>
<td>M-n</td>
<td>T-n</td>
<td>W-n</td>
<td>T-y</td>
<td>F-n</td>
<td>emp 131</td>
<td>M-n</td>
<td>T-n</td>
<td>W-n</td>
<td>T-y</td>
<td>F-n</td>
<td>emp 1311</td>
<td>M-n</td>
<td>T-n</td>
<td>W-n</td>
<td>T-y</td>
<td>F-n</td>
</tr>
</tbody>
</table>
答案
xml树开头缺少<company>
标记。最好的祝福,Bjarni
以上是关于postgres复杂xml解析的主要内容,如果未能解决你的问题,请参考以下文章