【发布时间】:2011-11-22 00:02:25
【问题描述】:
我如何向 PostgreSQL 表达我想要在 XPath 查询中同时来自多个层级的值?
我有一个具有多级层次结构的文档(在 PostgreSQL XML 值中)。对于这个问题,可以创建一个示例:
SELECT XMLPARSE(DOCUMENT '
<parrots>
<parrot name="Fred">
<descriptor>Beautiful plumage</descriptor>
<descriptor>Resting</descriptor>
</parrot>
<parrot name="Ethel">
<descriptor>Pining for the fjords</descriptor>
<descriptor>Stunned</descriptor>
</parrot>
</parrots>
') AS document
INTO TEMPORARY TABLE parrot_xml;
我可以从该文档中获得不同级别的信息。
=> SELECT
(XPATH('./@name', parrot.node))[1] AS name
FROM (
SELECT
UNNEST(XPATH('./parrot', parrot_xml.document))
AS node
FROM parrot_xml
) AS parrot
;
name
-------
Fred
Ethel
(2 rows)
=> SELECT
(XPATH('./text()', descriptor.node))[1] AS descriptor
FROM (
SELECT
UNNEST(XPATH('./parrot/descriptor', parrot_xml.document))
AS node
FROM parrot_xml
) AS descriptor
;
descriptor
-----------------------
Beautiful plumage
Resting
Pining for the fjords
Stunned
(4 rows)
不过,我想不通的是如何连接多个级别,以便查询返回与其应用的鹦鹉相关的每个描述符。
=> SELECT
??? AS name,
??? AS descriptor
FROM
???
;
name descriptor
------- -----------------------
Fred Beautiful plumage
Fred Resting
Ethel Pining for the fjords
Ethel Stunned
(4 rows)
如何做到这一点?应该用什么来代替“???”?
单个复杂的 XPath 查询——但是如何同时引用多个级别?几个 XPath 查询——但是如何为结果关系保留祖先-后代信息?还有什么?
【问题讨论】:
标签: xml postgresql xpath