如何在Postgres 11和Postgres 9.1.2中解析XML

时间:2019-03-17 07:10:26

标签: xml postgresql xml-parsing postgresql-11

在Postgres 9.1.2中,以下脚本会产生正确的结果:

1.34
5.56

在Postgres 11中,它产生错误的结果:

null
null

如何使其也可以在Postgres的新版本中使用?

create temp table t(x xml, nsa text[][]) on commit drop;
insert into t values(
    '<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:camt.053.001.02">
  <BkToCstmrStmt>
    <Stmt>
      <Ntry>
        <Amt Ccy="EUR">1.34</Amt>
      </Ntry>
      <Ntry>
        <Amt Ccy="EUR">5.56</Amt>
      </Ntry>
    </Stmt>
  </BkToCstmrStmt>
</Document> '::xml,
    ARRAY[ARRAY['ns','urn:iso:std:iso:20022:tech:xsd:camt.053.001.02']]);

    SELECT 
    (xpath('Amt/text()', x,nsa))[1]::text::numeric AS tasusumma
    FROM (
        SELECT unnest(xpath('/ns:Document/ns:BkToCstmrStmt/ns:Stmt/ns:Ntry', x,nsa)) as x,
        nsa
        FROM t
    ) Ntry

1 个答案:

答案 0 :(得分:1)

足以使用其命名空间别名限定Amt

SELECT (xpath('ns:Amt/text()', x, nsa))[1]::text::numeric AS tasusumma
FROM (
    SELECT unnest(xpath('/ns:Document/ns:BkToCstmrStmt/ns:Stmt/ns:Ntry', x, nsa)) as x,
    nsa
    FROM t
) Ntry;

我在10.6上进行了测试;我很确定它也可以在11上使用。它也可以在以前的版本(在9.6上测试)上工作。