Как изменить атрибут элемента в xml значении таблицы в postgreSQL, при наличии нескольких одинаковых элементов?

Есть xml текст добавленный в таблицу PostgreSQL.

CREATE TABLE mytable AS SELECT XML $$
<Root>
<Order>
<Items>
<Product ProductID="1" ProductName="Book1" ProductCategory="A">
<Price val="RUB" PValue="300"/>
<Autors lang="русский">
<Autor>
 Автор1
</Autor>
<Autor>
 Автор2
</Autor>
</Autors>
</Product>
<Product ProductID="2" ProductName="Book2" ProductCategory="F">
<Price val="DOL" PValue="50"/>
<Autors lang="английский">
<Autor>
 Autor1
</Autor>
<Autor>
 Autor1
</Autor>
</Autors>
</Product>
</Items>
</Order>
</Root>
$$ AS Products;

Необходимо изменить значение ProductID в том месте где оно равно 2 на 5.

Пробовал различные способы с использованием unnest например так

WITH updated_products AS (
    SELECT
        xpath('/Root/Order/Items/Product/@ProductID', products) AS product_id,
        xpath('/Root/Order/Items/Product', products) AS product_node
    FROM
        mytable,
        unnest(xpath('/Root/Order/Items/Product/@ProductID', products)) WITH ORDINALITY AS t(id, product_id)
    WHERE
        t.product_id = '2'
),
updated_xml AS (
    SELECT
        xmlagg(
            xmlelement(name product_node, xmlattributes('5' AS "ProductID"), product_node)
        ) AS updated_xml
    FROM
        updated_products
)
UPDATE t3
SET products = (
    SELECT
        updated_xml
    FROM
        updated_xml
)
WHERE
    EXISTS (
        SELECT
            1
        FROM
            xmltable('/Root/Order/Items/Product' PASSING products COLUMNS ProductID text PATH '@ProductID') AS xt
        WHERE
            xt.ProductID = '2'
    );

ничего не выходит,либо ошибки,либо некорректно изменяется значение в таблице.

Просьба помочь,если это возможно.


Ответы (0 шт):