【发布时间】:2018-05-07 07:55:52
【问题描述】:
我正在尝试使用 XPath 从存储在 PostgreSQL 数据库中的 XML 中提取值。我有一个错误:
SQL 错误 [42601]:错误:“[”处或附近的语法错误
我的 SQL 是:
-- Find "e" nodes which have "p1" child nodes and does not have "p3" child nodes
WITH tbl(p_xml) AS (
SELECT '<root>
<e id="1">
<p1>P1</p1>
<p2>P2</p2>
</e>
<e id="2">
<p1>P1</p1>
<p3>P2</p3>
</e>
<e id="3">
<p2>P1</p2>
<p3>P3</p3>
</e>
</root>'::xml
)
select *
FROM tbl
where
(xpath('count(/root/e/p1)', p_xml)[1]::text)::int > 0 and
(xpath('count(/root/e/p3)', p_xml)[1]::text)::int = 0
我在 StackOverflow 上看到了很多使用正方形获取数据的示例(因为 xpath 函数返回数组),但在我的 PostgreSQL 上所有这些示例都失败并出现相同的错误。我在 PostgreSQL DB 版本 9.6 和 10.1 上试过这个,但没有运气。
【问题讨论】:
标签: xml postgresql xpath