Re: Extract values from XML content - Mailing list pgsql-general

From shammat@gmx.net
Subject Re: Extract values from XML content
Date
Msg-id 44c41d77-8a4f-4929-9827-0c8993a88f14@gmx.net
Whole thread Raw
In response to Extract values from XML content  (celati Laurent <laurent.celati@gmail.com>)
List 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]






pgsql-general by date:

Previous
From: Ron Johnson
Date:
Subject: Re: Help with restoring database from old version of PostgreSQL
Next
From: Adrian Klaver
Date:
Subject: Re: Help with restoring database from old version of PostgreSQL