If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > SQL to XML Query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-14-06, 10:42
iblair iblair is offline
Registered User
 
Join Date: Sep 2005
Posts: 16
Question SQL to XML Query

Hi all,

I have a table as follows:

ID PARENT NAME
1 - root
2 1 superuser
3 1 basicuser
4 1 blockeduser
5 2 ian
6 2 bob
7 3 tim
8 3 chaz
9 4 dave
10 4 seymour

I would like to convert this to an XML tree structure using the parent column to know which is the parent id, forming something like this:

<manager name="root">
<team_member name="superuser">
<team_member name="ian" />
<team_member name="bob" />
</team_member>
<team_member name="basicuser">
<team_member name="tim" />
<team_member name="chaz" />
</team_member>
<team_member name="blockeduser">
<team_member name="dave" />
<team_member name="seymour" />
</team_member>
</manager>

I do not know how many levels the tree has, so I need to recursively traverse it. I can get down to a known amount of levels by using and extending something like the following structure:

SELECT XML2CLOB(XMLELEMENT(name "manager", XMLATTRIBUTES(name AS "name"), (SELECT XMLAGG(XMLELEMENT(NAME "team_member",XMLATTRIBUTES(name AS "name"))) from temp.parents p where p.parent=i.id))) from temp.parents i where parent is null'

I'm not worried about the element names, as obviously I can't name these recusively if I don't know how many levels there are, but if anyone can give any pointers on where to start with the recursion I'd really appreciate it!

Many thanks in advance,
Ian
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On