在PostgreSQL中提取xml的值

问题描述 投票:0回答:1

请告诉我如何从该 XML 中提取 guid 字段?问题是 xmlns 参数的存在。目前返回空。

with XML_text(col) as
(
select
'<?xml version="1.0" encoding="UTF-8"?>
<purchasePlan
xmlns:ns2="http://zakupki.gov.ru/223fz/purchasePlan/1"
xmlns="http://zakupki.gov.ru/223fz/types/1"
xmlns:ns10="http://zakupki.gov.ru/223fz/decisionSuspension/1"
xmlns:ns11="http://zakupki.gov.ru/223fz/disagreementProtocol/1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://zakupki.gov.ru/223/integration/schema/TFF-13.1 https://zakupki.gov.ru/223/integration/schema/TFF-13.1/purchasePlan.xsd">
<body>
<item>
<guid>096c4bf6-d656-4441-9032-0b7c45423af1</guid>
</item>
</body>
</purchasePlan>'::xml
)
SELECT r.guid
  FROM XML_text as x,
       XMLTABLE('purchasePlan/body/item' passing x.col
                COLUMNS guid varchar(50) path './guid'
                ) as r
;

结果必须是“096c4bf6-d656-4441-9032-0b7c45423af1”。

xml postgresql xpath xml-parsing xmltable
1个回答
0
投票

请尝试以下解决方案。

您的 XML 有一个默认名称空间。它需要通过

xmlnamespaces(...)
子句声明并在 XPath 表达式中使用。

SQL

with XML_text(col) as
(
select
'<?xml version="1.0" encoding="UTF-8"?>
<purchasePlan xmlns:ns2="http://zakupki.gov.ru/223fz/purchasePlan/1"
              xmlns="http://zakupki.gov.ru/223fz/types/1"
              xmlns:ns10="http://zakupki.gov.ru/223fz/decisionSuspension/1"
              xmlns:ns11="http://zakupki.gov.ru/223fz/disagreementProtocol/1"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="https://zakupki.gov.ru/223/integration/schema/TFF-13.1 https://zakupki.gov.ru/223/integration/schema/TFF-13.1/purchasePlan.xsd">
    <body>
        <item>
            <guid>096c4bf6-d656-4441-9032-0b7c45423af1</guid>
        </item>
    </body>
</purchasePlan>'::xml
)
SELECT r.guid
  FROM XML_text as x,
       XMLTABLE(xmlnamespaces('http://zakupki.gov.ru/223fz/types/1' AS "ns1"),
                '/ns1:purchasePlan/ns1:body/ns1:item' passing x.col
                COLUMNS guid varchar(50) path 'ns1:guid'
                ) as r
;
© www.soinside.com 2019 - 2024. All rights reserved.