gpt4 book ai didi

xml - 使用 XPath : SQL Error [42601]: ERROR: syntax error at or near "[" 在 PostgreSQL 中提取 XML 值时出错

转载 作者:行者123 更新时间:2023-11-29 14:09:27 25 4
gpt4 key购买 nike

我正在尝试使用 XPath 从存储在 PostgreSQL 数据库中的 XML 中提取值。我有一个错误:

SQL Error [42601]: ERROR: syntax error at or near "["

我的 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 数据库版本 9.6 和 10.1 上尝试过这个,但没有成功。

最佳答案

那是因为您的查询缺少括号。无论如何,如果您只想返回满足该条件的 e 元素,您需要分解 XML,例如:

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 e
FROM tbl
CROSS JOIN LATERAL UNNEST(xpath('//e[p1 and not(p3)]', p_xml)) e

rextester 演示:http://rextester.com/FZGP41665

关于xml - 使用 XPath : SQL Error [42601]: ERROR: syntax error at or near "[" 在 PostgreSQL 中提取 XML 值时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47453989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com