xml - 如何在 Postgresql 中查询 XML 列?

标签 xml postgresql xpath

我在 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>                                                +| 
    |                               |            |                                                                          |

online 中使用相同的 XML XPath 测试器我能够在 ReportRequestId 中检索值,但是在查询 Postgresql 时我没有得到任何值:

select xpath('/RequestReportResponse/RequestReportResult/ReportRequestInfo/ReportRequestId', original) from amazon_output where hash='aaaaaaaaaa';

我缺少 XML 数据类型的什么?

最佳答案

因为您有一个 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 documentation对于 xpath 方法:

The optional third argument of the function is an array of namespace mappings. This array should be a two-dimensional text array with the length of the second axis being equal to 2 (i.e., it should be an array of arrays, each of which consists of exactly 2 elements). The first element of each array entry is the namespace name (alias), the second the namespace URI. It is not required that aliases provided in this array be the same as those being used in the XML document itself (in other words, both in the XML document and in the xpath function context, aliases are local).

关于xml - 如何在 Postgresql 中查询 XML 列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46430822/

相关文章:

java - 具有不同命名空间的 XML 可以深入了解所需的值

c# - 删除无效的子节点但保持其内容不变..?

json - 基于对象类型的 Spring 集成对象转换器

xml - 从 XML 模板生成 XML 文档

postgresql - psql : symbol lookup error: psql: undefined symbol: PQsetErrorContextVisibility

postgresql - psql 提示符中的颜色

php - 具有 XPath 编码问题的 DOMDocument。 [?] 很多测试

xml - 如何提高基于 MQ 的批处理应用程序的性能?

java - DBUnit PostgresqlDataTypeFactory 不识别枚举列表

css - 在 Google 搜索上查找 "Next"按钮的正确 xpath