如何在 PostgreSQL 中查询来自 XML 的兄弟节点
How do I query sibling nodes from XML in PostgreSQL
我在 PostgreSQLmy_table 的 settings 列中存储了一些 XML ]. XML 类似于:
<Dictionary>
<ExportValues>
<ReplacementSet>
<type>TEST_CODE</type>
<ReplacementPair>
<Input>A1</Input>
<Output>One</Output>
</ReplacementPair>
<ReplacementPair>
<Input>A2</Input>
<Output>Two</Output>
</ReplacementPair>
<ReplacementPair>
<Input>A3</Input>
<Output>Three</Output>
</ReplacementPair>
</ReplacementSet>
<ReplacementSet>
<type>TEST_TYPE</type>
<ReplacementPair>
<Input>MTL</Input>
<Output>Metal</Output>
</ReplacementPair>
<ReplacementPair>
<Input>LQD</Input>
<Output>Liquid</Output>
</ReplacementPair>
<ReplacementPair>
<Input>SLD</Input>
<Output>Solid</Output>
</ReplacementPair>
</ReplacementSet>
</ExportValues>
</Dictionary>
我正在尝试获得以下输出:
type, Input, Output
TEST_CODE, A1, One
TEST_CODE, A2, Two
TEST_CODE, A3, Three
TEST_TYPE, MTL, Metal
TEST_TYPE, LQD, Liquid
TEST_TYPE, SLD, Solid
我可以使用以下 SQL:
从 type 节点获取值
select xxx.*
from xmltable('/Dictionary/ExportValues/ReplacementSet'
passing xml((select settings
from my_table
limit 1))
columns replacement_value_type text path 'type') xxx
而且我能够从 Input 和 Output 节点获取值,其中 SQL:
select xxx.*
from xmltable('/Dictionary/ExportValues/ReplacementSet/ReplacementPair'
passing xml((select settings
from web_service
limit 1))
columns our_value text path 'OurValue',
their_value text path 'TheirValue') xxx
但是,我无法弄清楚如何 select 来自各个 type 节点的值以及所有 Input 和 Output 节点值在 ReplacementSet 节点中。
我尝试的所有内容都出错了,包括 type 值,但 Input 和 为空值Output,或 type 的空值以及 Input 和 Output 节点的值.
这不是问题,但您必须明确指定 "type" 列的 XPath:
select x.*
from my_table,
xmltable('/Dictionary/ExportValues/ReplacementSet/ReplacementPair'
passing settings
columns
type text path '../type',
input text path 'Input',
output text path 'Output') x;
+-----------+-------+--------+
| type | input | output |
+-----------+-------+--------+
| TEST_CODE | A1 | One |
| TEST_CODE | A2 | Two |
| TEST_CODE | A3 | Three |
| TEST_TYPE | MTL | Metal |
| TEST_TYPE | LQD | Liquid |
| TEST_TYPE | SLD | Solid |
+-----------+-------+--------+
(6 rows)
我在 PostgreSQLmy_table 的 settings 列中存储了一些 XML ]. XML 类似于:
<Dictionary>
<ExportValues>
<ReplacementSet>
<type>TEST_CODE</type>
<ReplacementPair>
<Input>A1</Input>
<Output>One</Output>
</ReplacementPair>
<ReplacementPair>
<Input>A2</Input>
<Output>Two</Output>
</ReplacementPair>
<ReplacementPair>
<Input>A3</Input>
<Output>Three</Output>
</ReplacementPair>
</ReplacementSet>
<ReplacementSet>
<type>TEST_TYPE</type>
<ReplacementPair>
<Input>MTL</Input>
<Output>Metal</Output>
</ReplacementPair>
<ReplacementPair>
<Input>LQD</Input>
<Output>Liquid</Output>
</ReplacementPair>
<ReplacementPair>
<Input>SLD</Input>
<Output>Solid</Output>
</ReplacementPair>
</ReplacementSet>
</ExportValues>
</Dictionary>
我正在尝试获得以下输出:
type, Input, Output
TEST_CODE, A1, One
TEST_CODE, A2, Two
TEST_CODE, A3, Three
TEST_TYPE, MTL, Metal
TEST_TYPE, LQD, Liquid
TEST_TYPE, SLD, Solid
我可以使用以下 SQL:
从 type 节点获取值select xxx.*
from xmltable('/Dictionary/ExportValues/ReplacementSet'
passing xml((select settings
from my_table
limit 1))
columns replacement_value_type text path 'type') xxx
而且我能够从 Input 和 Output 节点获取值,其中 SQL:
select xxx.*
from xmltable('/Dictionary/ExportValues/ReplacementSet/ReplacementPair'
passing xml((select settings
from web_service
limit 1))
columns our_value text path 'OurValue',
their_value text path 'TheirValue') xxx
但是,我无法弄清楚如何 select 来自各个 type 节点的值以及所有 Input 和 Output 节点值在 ReplacementSet 节点中。
我尝试的所有内容都出错了,包括 type 值,但 Input 和 为空值Output,或 type 的空值以及 Input 和 Output 节点的值.
这不是问题,但您必须明确指定 "type" 列的 XPath:
select x.*
from my_table,
xmltable('/Dictionary/ExportValues/ReplacementSet/ReplacementPair'
passing settings
columns
type text path '../type',
input text path 'Input',
output text path 'Output') x;
+-----------+-------+--------+
| type | input | output |
+-----------+-------+--------+
| TEST_CODE | A1 | One |
| TEST_CODE | A2 | Two |
| TEST_CODE | A3 | Three |
| TEST_TYPE | MTL | Metal |
| TEST_TYPE | LQD | Liquid |
| TEST_TYPE | SLD | Solid |
+-----------+-------+--------+
(6 rows)