我需要從postgres中的xml列提取三列數據,以便將xml擴展到適當的列中。其中一個列需要是xml的一個嵌套級別的屬性,其他列是向下一級的嵌套的屬性。應該重複更高一級的數據。這可能嗎?有關具體內容,請參閱下面的示例。在postgres中使用xpath提取多個級別的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
)
- -EDITED ---
返回此數據:
"{}";"{category1}";"{product1}"
"{}";"{category1}";"{product2}"
"{}";"{category2}";"{product3}"
我需要這樣的數據:
"{manufacturer-catalog-id}";"{category1}";"{product1}"
"{manufacturer-catalog-id}";"{category1}";"{product2}"
"{manufacturer-catalog-id}";"{category2}";"{product3}"
如果您不需要任何結果,則不需要發送查詢。我想你忘了在你的問題中包含一些東西? :) –
嗯,對不起。我修好了它。 –