我需要从 postgres 中的 xml 列中提取三列数据,以便将 xml 扩展为适当的列。这些列中的一列需要是 xml 的一个嵌套级别的属性,而其他列是下一层嵌套的属性。应重复来自更高级别的数据。这可能吗?有关具体内容,请参见下面的示例。
谢谢,--sw
考虑以下查询:
with x as (select
'<catalog catalog-id="manufacturer-catalog-id">
<category-assignment category-id="category1" product-id="product1"/>
<category-assignment category-id="category1" product-id="product2"/>
<category-assignment category-id="category2" product-id="product3"/>
</catalog>'::xml as t
)
(
select
xpath('/catalog/@catalog-id', catalog_xml) catalog_id,
xpath('//@category-id', catalog_xml) category_assignment_category_id,
xpath('//@product-id', catalog_xml) category_assignment_product_id
from (select unnest(xpath('/catalog', t)) catalog_xml from x) q
)
此查询返回以下数据:
"{manufacturer-catalog-id}";"{category1,category1,category2}";"{product1,product2,product3}"
这个查询:
with x as (select
'<catalog catalog-id="manufacturer-catalog-id">
<category-assignment category-id="category1" product-id="product1"/>
<category-assignment category-id="category1" product-id="product2"/>
<category-assignment category-id="category2" product-id="product3"/>
</catalog>'::xml as t
)
(
select
xpath('/catalog/@catalog-id', catalog_xml) catalog_id,
xpath('//@category-id', catalog_xml) category_assignment_category_id,
xpath('//@product-id', catalog_xml) category_assignment_product_id
from (select unnest(xpath('/catalog/category-assignment', t)) catalog_xml from x) q
)
---已编辑---
返回此数据:
"{}";"{category1}";"{product1}"
"{}";"{category1}";"{product2}"
"{}";"{category2}";"{product3}"
我需要这些数据:
"{manufacturer-catalog-id}";"{category1}";"{product1}"
"{manufacturer-catalog-id}";"{category1}";"{product2}"
"{manufacturer-catalog-id}";"{category2}";"{product3}"