Re: xml build a list of all elements

Steve Midgley <science@misuse.org>

From: Steve Midgley <science@misuse.org>
To: ml@ft-c.de
Cc: pgsql-sql@lists.postgresql.org
Date: 2024-03-26T22:27:27Z
Lists: pgsql-sql
On Tue, Mar 26, 2024 at 2:05 PM ft <ml@ft-c.de> wrote:

> Hello,
>
> I have a postgresql database table with xml data.
> What I need is a list of all elements and attributes.
> Example:
> <area>
>   <sub>
>    <sub2 attr='1'>comment</sub2>
>    <sub2 attr='2'>comment</sub2>
>   <sub>
> </area>
>
> The result should be:
> /area
> /area/sub
> /area/sub/sub2
> /area/sub/sub2@attr
> /area/sub/sub2
> /area/sub/sub2@attr
>
> or distinct (it's enough)
> /area
> /area/sub
> /area/sub/sub2
> /area/sub/sub2@attr
>
> Is there a postgresql function to build this list?
>
>
> I think it can be done but it'll be a painful function. I'd personally
recommend writing this in a higher level language that deals with xml as
rationally as it is possible to deal with xml (which is very unfriendly to
most programming languages). Possibly you could consider using a PL/Python
extension if you want processing to happen on the server, but I have no
idea about security/resource consumption implications of that approach. But
if you can get Python's lxml installed on your Pg server, having a library
like that would (for me) make this function a lot easier to write..

Steve