postgres复杂xml解析

postgres复杂xml解析,xml,postgresql,Xml,Postgresql,我有下面的xml,一个员工可以有几个报告对象,每个报告对象可以有几个报告内容,但是层次结构最多可以有4个内部报告内容。 我需要这个XML的结果如下表所示 我正在使用下面的postgres查询,我的计划是有4个CTE,但它失败了,错误是无法解析XML文档我在这里做错了什么 <department> <head> <employee>emp 1</employee> <c

我有下面的xml,一个员工可以有几个报告对象,每个报告对象可以有几个报告内容,但是层次结构最多可以有4个内部报告内容。 我需要这个XML的结果如下表所示

我正在使用下面的postgres查询,我的计划是有4个CTE,但它失败了,错误是无法解析XML文档我在这里做错了什么

    <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;
受雇者 星期一 星期二 星期三 星期四 星期五 雇员1 星期一1 星期二1 星期三1 星期四1 星期五1 雇员2 星期一2 星期二2 星期三2 星期四2 星期五2 雇员3 星期一(三) 星期二(星期三) 星期三 星期四3 星期五3 环境管理计划1 M-y T-y W-n T-y F-n 环境管理计划11 M-n T-n W-n T-y F-n emp 111 M-n T-n W-n T-y F-n 环境管理计划1 M-y T-y W-n T-y F-n 环境管理计划11 M-n T-n W-n T-y F-n emp 10034 M-n T-n W-n T-y F-n 环境管理计划1 M-y T-y W-n T-y F-n 环境管理计划12 M-n T-n W-n T-y F-n 环境管理计划1 M-y T-y W-n T-y F-n 环境管理计划13 M-n T-n W-n T-y F-n 电磁脉冲131 M-n T-n W-n T-y F-n 电磁脉冲1311 M-n T-n W-n T-y F-n xml树的开头缺少标记。 顺致敬意, 比亚尼