Это мой точный пример вопроса.
CREATE TABLE xml_mobile_data AS SELECT
xml $$
<rows>
<row id="1">
<model>Samsung Galaxy Note 9</model>
<price>$3,000.00</price>
</row>
<row id="2">
<model>iPhone X</model>
<price>$1,000.00</price>
</row>
<row id="3">
<model>Samsung Galaxy S9+</model>
<price>$999.00</price>
</row>
<row id="4">
<model>Huawei P20 Pro</model>
<price>$2,000.00</price>
</row>
<row id="5">
<model>Google Pixel XL 3</model>
<price>$899.00</price>
</row>
</rows>
$$ AS mobile_data;
Мы можем получить это в формате строки.
SELECT xmltable.*
FROM xml_mobile_data,
XMLTABLE('/rows/row'
PASSING mobile_data
COLUMNS
ordinality FOR ORDINALITY,
model_id INT PATH '@id',
model TEXT PATH 'model',
"price" MONEY
);
Другим способом является использование WITH QUERY CTE (COMMON TABLE EXPRESSIONS)
WITH xmldata(data) AS (VALUES ('
<rows>
<row id="1">
<model>Samsung Galaxy Note 9</model>
<price>$3,000.00</price>
</row>
<row id="2">
<model>iPhone X</model>
<price>$1,000.00</price>
</row>
<row id="3">
<model>Samsung Galaxy S9+</model>
<price>$999.00</price>
</row>
<row id="4">
<model>Huawei P20 Pro</model>
<price>$2,000.00</price>
</row>
<row id="5">
<model>Google Pixel XL 3</model>
<price>$899.00</price>
</row>
</rows>
'::XML))
SELECT xmltable.*
FROM xmldata,
XMLTABLE('/rows/row'
PASSING data
COLUMNS
ordinality FOR ORDINALITY,
model_id INT PATH '@id',
model TEXT PATH 'model',
"price" MONEY
);