我在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 XPath测试器中使用相同的XML,我能够在ReportRequestId
中检索值,但在查询Postgresql时,我没有得到任何值:
select xpath('/RequestReportResponse/RequestReportResult/ReportRequestInfo/ReportRequestId', original) from amazon_output where hash='aaaaaaaaaa';
XML数据类型缺少什么?
答案 0 :(得分:2)
由于你有一个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方法:
该函数的可选第三个参数是命名空间映射数组。该数组应该是二维文本数组,第二轴的长度等于2(即,它应该是数组的数组,每个数组恰好由2个元素组成)。每个数组条目的第一个元素是命名空间名称(别名),第二个是命名空间URI。不要求此数组中提供的别名与XML文档本身中使用的别名相同(换句话说,在XML文档和xpath函数上下文中,别名都是本地的)。