Thread: Transform mapped XPath expressions into XML containing relational data

Transform mapped XPath expressions into XML containing relational data

From
Thangalin
Date:
Hi,

Is it possible to build an XML (or JSON) document using a map database columns to XPath expressions? For example:

root               > people
person             > person
person.first_name -> name/first
person.last_name  -> name/last
person.age        -> [@age]
account.person_id => person.person_id
account            > person/account
account.number    -> [@id]

The arrows have meanings (RHS = right-hand side; LHS = left-hand side):

  • > ... Container element (no text values); RHS is the element name.
  • -> ... Path to a text value; RHS is the XPath to the element name.
  • => ... Join expression; RHS references the attribute in a previously listed LHS table name.

PERSON table might resemble:

person_id | first_name | last_name | age      123 | Peter      | Parker    | 18      456 | James      | Jameson   | 42

And an ACCOUNT table might resemble:

account_id | person_id | number         1 |       123 | 123456789

Querying the database using the XPath map would produce the following XML document:

<people>  <person age="18">    <name>      <first>Peter</first>      <last>Parker</last>    </name>    <account id="123456789" />  </person>  <person age="42">    <name>      <first>James</first>      <last>Jameson</last>    </name>  </person>
</people>

In this case, James Jameson does not have an account and so the corresponding XML element (account) is not included in the final document.

This would make it trivial to generate a JSON document without changing the underlying code; whereas, hand-coding the XML document with combinations of the xml* functions semantically prohibits a direct transmogrification of the output format.

Thoughts?

Re: Transform mapped XPath expressions into XML containing relational data

From
Peter Eisentraut
Date:
On Mon, 2012-11-26 at 09:26 -0800, Thangalin wrote:
> Is it possible to build an XML (or JSON) document using a map database
> columns to XPath expressions? For example:
> 
> root               > people
> person             > person
> person.first_name -> name/first
> person.last_name  -> name/last
> person.age        -> [@age]
> account.person_id => person.person_id
> account            > person/account
> account.number    -> [@id] 

I would go about this by using table_to_xml or query_to_xml and then
converting the resulting XML document to your particular format using
XSLT.