Re: How to parse XML in Postgres newer versions also

From: Thomas Kellerer <spam_eater(at)gmx(dot)net>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: How to parse XML in Postgres newer versions also
Date: 2019-03-17 14:12:56
Message-ID: 6424a000-0492-831a-b748-37806ec7e675@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Andrus schrieb am 17.03.2019 um 08:36:
> In Postgres 9.1.2 script below produces proper results:
>
> 1.34
> 5.56
>
> 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

If you don't need compatibility with old versions, I would use XMLTABLE in Postgres 11

select x.*
from t,
xmltable(
XMLNAMESPACES(t.nsa[1][2] AS ns),
'/ns:Document/ns:BkToCstmrStmt/ns:Stmt/ns:Ntry'
passing t.x
columns tasusumma numeric path 'ns:Amt'
) as x;

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Andrus 2019-03-17 14:19:33 Re: How to parse XML in Postgres newer versions also
Previous Message Andrus 2019-03-17 14:11:44 Re: How to parse XML in Postgres newer versions also