Postgres查询以获取与xpath条件匹配的行

时间:2018-05-25 06:36:35

标签: sql postgresql

我的xmldata是,

<Transaction >
   <UUID>2017-03-17T08:00:00-086F0ADD43</UUID>
   <SequenceNumber Type="a">1</SequenceNumber>
   <SequenceNumber Type="b">1</SequenceNumber>
</Transaction>
<Transaction >
   <UUID>2017-03-17T08:00:00-086F0ADD43</UUID>
   <SequenceNumber Type="a">2</SequenceNumber>
   <SequenceNumber Type="b">2</SequenceNumber>
</Transaction>

我目前的查询是:

select xmldata, cast ((xpath('/Transaction/SequenceNumber[@Type="b" and text()="1"]/text()', xmldata)) AS TEXT) from tbltransaction

这导致所有行

    xmldata    | xpath
---------------+-----
 <Transaction> | {1}
 <Transaction> | {}

但我希望结果集具有如下所示的精确值,

     xmldata    | xpath
---------------+-----
 <Transaction> | {1}

如何修改上述查询以获取此内容?

2 个答案:

答案 0 :(得分:1)

如何使用WHERE过滤:

select s.r, xmldata 
from tbltransaction t
JOIN LATERAL (
    SELECT (xpath('/Transaction/SequenceNumber[@Type="b" and text()="1"]/text()',
            t.xmldata)::TEXT) AS r
     )s ON TRUE
WHERE s.r != '{}';

<强> DBFiddle Demo

答案 1 :(得分:0)

如果您使用PostgreSQL 10 you can use XMLTABLE

WITH data (xmldata) AS (VALUES('<Transaction >
   <UUID>2017-03-17T08:00:00-086F0ADD43</UUID>
   <SequenceNumber Type="a">1</SequenceNumber>
   <SequenceNumber Type="b">1</SequenceNumber>
</Transaction>'::xml),('
<Transaction >
   <UUID>2017-03-17T08:00:00-086F0ADD43</UUID>
   <SequenceNumber Type="a">2</SequenceNumber>
   <SequenceNumber Type="b">2</SequenceNumber>
</Transaction>'::xml))

SELECT *
  FROM data
     , XMLTABLE('/Transaction/SequenceNumber[@Type="b" and text()="1"]/text()'
                PASSING xmldata
                COLUMNS xpath XML PATH '.'
               ) t;

给你:

                    xmldata                     | xpath
------------------------------------------------+-------
 <Transaction >                                +| 1
    <UUID>2017-03-17T08:00:00-086F0ADD43</UUID>+|
    <SequenceNumber Type="a">1</SequenceNumber>+|
    <SequenceNumber Type="b">1</SequenceNumber>+|
 </Transaction>                                 |
(1 row)