XML output & multiple SELECT queries

Started by Peter Billenalmost 18 years ago2 messagesgeneral
Jump to latest
#1Peter Billen
peter@clueless.be

Dear PostgreSQL users,

I would like to ask a question about outputting data as XML. Say I have two
tables:

team(integer id, text name);
player_of_team(integer id, integer team_id, text name); (team_id is FK to
team.id)

I would like to query both tables to get following example XML output:

<team name="Real Madrid">
<players>
<name>Garcia</name>
<name>Robinho</name>
</players>
</team>

Is this possible in one query? I'm thinking about something like this, but I
haven't figured it out so far:

SELECT XMLElement(name team, XMLAttributes(name as name), SELECT XMLElement(name
players, XMLForest(name)) FROM player_of_team WHERE team_id = t.id) FROM team t
WHERE t.name = 'Real Madrid';

In other words, is it possible to nest multiple SELECT queries in the XML output
functions?

Thanks in advance. Kind regards,

Peter

#2Peter Eisentraut
peter_e@gmx.net
In reply to: Peter Billen (#1)
Re: XML output & multiple SELECT queries

Peter Billen wrote:

I would like to ask a question about outputting data as XML. Say I have two
tables:

team(integer id, text name);
player_of_team(integer id, integer team_id, text name); (team_id is FK to
team.id)

I would like to query both tables to get following example XML output:

<team name="Real Madrid">
� � �<players>
� � � � �<name>Garcia</name>
� � � � �<name>Robinho</name>
� � �</players>
</team>

SELECT XMLElement(name team, XMLAttributes(team.name as name), XMLElement(name
players, XMLAgg(XMLElement(name name, player_of_team.name)))) FROM team JOIN
player_of_team ON team.id = player_of_team.team_id GROUP BY team.name;