如何在PostgreSQL中查询XML列?

6

我在Postgres中创建了一个包含XML列的表:

 id           | integer
 date_created | timestamp with time zone
 hash         | character varying(10)
 original     | xml
 report_name  | text

我已经插入了一个XML字符串:

id |         date_created          |    hash    |                                 original                                 |               report_name                
----+-------------------------------+------------+--------------------------------------------------------------------------+------------------------------------------
  9 | 2017-09-26 17:37:16.823251+02 | aaaaaaaaaa | <RequestReportResponse xmlns="http://mws.amazonaws.com/doc/2009-01-01/">+| _GET_XML_ALL_ORDERS_DATA_BY_LAST_UPDATE_
    |                               |            |   <RequestReportResult>                                                 +| 
    |                               |            |     <ReportRequestInfo>                                                 +| 
    |                               |            |       <ReportType>_GET_XML_ALL_ORDERS_DATA_BY_LAST_UPDATE_</ReportType> +| 
    |                               |            |       <ReportProcessingStatus>_SUBMITTED_</ReportProcessingStatus>      +| 
    |                               |            |       <EndDate>2017-09-26T13:31:02+00:00</EndDate>                      +| 
    |                               |            |       <Scheduled>false</Scheduled>                                      +| 
    |                               |            |       <ReportRequestId>50064017435</ReportRequestId>                    +| 
    |                               |            |       <SubmittedDate>2017-09-26T13:31:02+00:00</SubmittedDate>          +| 
    |                               |            |       <StartDate>2017-09-26T13:31:02+00:00</StartDate>                  +| 
    |                               |            |     </ReportRequestInfo>                                                +| 
    |                               |            |   </RequestReportResult>                                                +| 
    |                               |            |   <ResponseMetadata>                                                    +| 
    |                               |            |     <RequestId>e092cdbe-2978-4064-a5f6-129b88322b02</RequestId>         +| 
    |                               |            |   </ResponseMetadata>                                                   +| 
    |                               |            | </RequestReportResponse>                                                +| 
    |                               |            |                                                                          |

使用相同的XML在在线 XPath 测试器中,我能够检索到ReportRequestId的值,但是在查询Postgresql时,我没有得到任何返回值:
select xpath('/RequestReportResponse/RequestReportResult/ReportRequestInfo/ReportRequestId', original) from amazon_output where hash='aaaaaaaaaa';

我在使用XML数据类型时可能会遗漏什么?
1个回答

8

由于您有一个XML命名空间(xmlns),因此需要在xpath查询中包含它:

select xpath('/mydefns:RequestReportResponse/mydefns:RequestReportResult/mydefns:ReportRequestInfo/mydefns:ReportRequestId',
              original,
              ARRAY[ARRAY['mydefns', 'http://mws.amazonaws.com/doc/2009-01-01/']])
from amazon_output where hash='aaaaaaaaaa';

Postgres文档中xpath方法的说明中可以得知,该函数的可选第三个参数是一个命名空间映射数组。该数组应该是一个二维文本数组,第二个轴的长度应该等于2(即它应该是一个数组的数组,每个数组恰好由2个元素组成)。每个数组条目的第一个元素是命名空间名称(别名),第二个元素是命名空间URI。在这个数组中提供的别名不要求与XML文档本身中使用的别名相同(换句话说,在XML文档和xpath函数上下文中,别名都是局部的)。

不起作用。我也尝试了一些你的答案的变化。 - ruipacheco
1
经过一些实验,看起来你需要在搜索每个元素名称之前包含命名空间前缀“mydefns”。我将更新我的答案以反映这一点。如果有效,请告诉我。 - The Spartan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接