Re: Extract values from XML content

From: shammat(at)gmx(dot)net
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Extract values from XML content
Date: 2024-11-13 20:07:53
Message-ID: 44c41d77-8a4f-4929-9827-0c8993a88f14@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Am 13.11.24 um 15:58 schrieb celati Laurent:
> I have a table 'metadata' with 2000 records. With one column 'id' and one column 'data' with XML content.
> I need to extract for all records the values regarding the Organisation names.
> I success in querying without error message thanks to this following sql query :
>
> SELECT id, xpath('/contact/CI_ResponsibleParty/organisationName/CharacterString/text()',
>             CAST(data AS XML)) AS organisation_name
> FROM public.metadata;
>

I typically find xmltable() a lot easier to work with, especially if the XML contains namespaces.

I guess the namespaces are the problem in your case, you will have to pass them to the xpath()
function and reference them in the path expression as well

So you will need something like:

xpath('//cit:CI_Responsibility/cit:party/cit:CI_Organisation/cit:name/gco:CharacterString/text()',
cast(data as xml),
ARRAY[ARRAY['cit', 'http://...'], array['gco', 'http://...']])

The actual value for the namespace URIs depends on the definition in your XML

Note that xpath() returns an array, so you probably want (xpath(....))[1]

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2024-11-13 20:42:23 Re: Help with restoring database from old version of PostgreSQL
Previous Message Ron Johnson 2024-11-13 19:47:22 Re: Help with restoring database from old version of PostgreSQL