PostgreSQL
 sql >> Database >  >> RDS >> PostgreSQL

Come analizzare xml con elementi facoltativi

Devi prima estrarre tutti gli elementi ns:Ntry, quindi ottenere l'importo e EndToEndId per ciascuno di essi:

SELECT 
    endaaa,
    (xpath('ns:Amt/text()', x,nsa))[1]::text::numeric AS tasusumma,
    (xpath('ns:NtryDtls/ns:TxDtls/ns:Refs/ns:EndToEndId/text()', x,nsa))[1] AS orderinr

FROM (
    SELECT (xpath('/ns:Document/ns:BkToCstmrStmt/ns:Stmt/ns:Acct/ns:Id/ns:IBAN/text()', x,nsa))[1] as endaaa,
    unnest(xpath('/ns:Document/ns:BkToCstmrStmt/ns:Stmt/ns:Ntry', x,nsa)) as x, nsa
    FROM t
) Ntry