Map table data to XPath expressions

From: Thangalin <thangalin(at)gmail(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Map table data to XPath expressions
Date: 2011-12-27 08:11:00
Message-ID: CAANrE7oF=GPkvr83LJxWT=647dms0Ap679ir4XZ73M7xf9AFDw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Hi, all.

Am wondering if a feature exists in PostgreSQL that allows mapping tables
and columns to XPath expressions. For example, consider the following map,
"person-account":

person -> people
person.first_name -> person/name/first
person.last_name -> person/name/last
person.age -> person[(at)age]
account.person_id => person.person_id -- JOIN clause
account.number -> person/account[(at)id]

This would allow the following query (or similar):

SELECT * FROM xml.person-account;

And produce the following string (or rows of strings that the calling
client needs to append):

<people>
<person age="18">
<name>
<first>Peter</first>
<last>Parker</last>
</name>
<account id="123456789" />
</person>
<!-- etc. --->
</people>

I know it can be built using the XML functions that exist in PostgreSQL 9.
I am looking for a solution that would not entail having to write a fair
bit of code, such as (without the joining to the account table):

SELECT
xmlelement( name "people",
xmlagg(
xmlelement( name "person",
xmlattributes(age),
xmlforest( first_name as first, last_name as last )
)
)
)
FROM person;

I'm not looking for a perfect solution, but something rather simple that
could handle 80% of the cases.

Thank you!

Browse pgsql-novice by date

  From Date Subject
Next Message Michael Wood 2011-12-27 08:54:40 Re: creating utf-8 random strings
Previous Message Jean-Yves F. Barbier 2011-12-25 15:16:18 creating utf-8 random strings