如何使用xpath查询来创建临时表从xml内容过滤数据

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

我正在尝试使用xpath创建一个sql查询,在这里我希望过滤所需的数据并将其放在临时表中。示例:

  <superStarsDoc>
        <names>
            <starname>
                <preferredname>pref</preferredname>
                <firstNm>Bradd</firstNm>
                <lastNm>Pitt</lastNm>
            </starname>
        </names>
    </superStarsDoc>

and i am trying to get something like this but not working



with data(firstName,lastName) as 
(
unnest(xpath('/superStarsDoc/names/starname/firstNm[@firstNm="Bradd"]/text()',
(select xmlparse(document superstar_doc))))::text as firstName
,unnest(xpath('/superStarsDoc/names/starname/lastNm[@lastNm="Pitt"]lastNm="/text()',
(select xmlparse(document superstar_doc))))::text as lastName
from dbname.superstartable
)

我尝试搜索解决方案,但没有找到满足我要求的任何内容,我没有任何属性可以精确指向该记录。

我尝试使用以下解决方案,但该方法不起作用,出现语法错误。XPath 1.0 to find if an element's value is in a list of values

注意:我在此处键入代码,因为我无法完全复制粘贴我的代码,所以请原谅任何错字

sql xml postgresql xpath xquery
1个回答
0
投票
您可能应该使用:]修复XPath。

/superStarsDoc/names/starname/firstNm[.="Bradd"]/text() /superStarsDoc/names/starname/lastNm[.="Pitt"]/text()

通用代码:

with superstartable(superstar_doc) as ( values ( '<?xml version="1.0" encoding="UTF-8"?> <superStarsDoc> <names> <starname> <preferredname>pref</preferredname> <firstNm>Bradd</firstNm> <lastNm>Pitt</lastNm> </starname> </names> </superStarsDoc> '::xml) ) SELECT xpath('/superStarsDoc/names/starname/firstNm[.="Bradd"]/text()', superstar_doc)[1] as "first-name", xpath('/superStarsDoc/names/starname/lastNm[.="Pitt"]/text()', superstar_doc)[1] as "last-name" from superstartable

© www.soinside.com 2019 - 2024. All rights reserved.