rui*_*eco 4 xml postgresql xpath
我在 Postgres 中创建了一个包含 XML 列的表:
id | integer
date_created | timestamp with time zone
hash | character varying(10)
original | xml
report_name | text
Run Code Online (Sandbox Code Playgroud)
我插入了一个 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> +|
| | | |
Run Code Online (Sandbox Code Playgroud)
在在线XPath 测试器中使用相同的 XML我能够检索其中的值,ReportRequestId但是在查询 Postgresql 时我没有得到任何值:
select xpath('/RequestReportResponse/RequestReportResult/ReportRequestInfo/ReportRequestId', original) from amazon_output where hash='aaaaaaaaaa';
Run Code Online (Sandbox Code Playgroud)
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';
Run Code Online (Sandbox Code Playgroud)
来自xpath 方法的Postgres 文档:
该函数的可选第三个参数是命名空间映射数组。这个数组应该是一个二维文本数组,第二个轴的长度等于2(也就是说,它应该是一个数组数组,每个数组正好由2个元素组成)。每个数组条目的第一个元素是命名空间名称(别名),第二个元素是命名空间 URI。不要求此数组中提供的别名与 XML 文档本身中使用的别名相同(换句话说,在 XML 文档和 xpath 函数上下文中,别名都是本地的)。
| 归档时间: |
|
| 查看次数: |
11670 次 |
| 最近记录: |