Link list to hierarchy tree

Rolf Kemper

Usenet Archive
Joined
Mar 26, 2003
Messages
35
Reaction score
0
Dear Experts,

I have very often the following problem (but never really solved it in a good way)

I have a table on SQL Server which holds the relations bettween a tree of objects like the example below.

Parent Child
a b
a c
a d
b e
b f
f g
e h
e i

The xml crerated fro SQL Server could look like this
<LinkList>
<T P="a" C="b"/>
<T P="a" C="c"/>
<T P="a" C="d"/>
<T P="b" C="e"/>
<T P="b" C="f"/>
<T P="f" C="g"/>
<T P="e" C="h"/>
<T P="e" C="i"/>
</LinkList>

Now I would like to do an XSLT which creates a real tree out of that (eg to show the tree as a tree in HTML)

<Tree>
<a>
<b>
<e>
<h>
</h>
<i>
</i>
<f>
<g>
</g>
</f>
</b>
<c>
</c>
<d>
</d>
</a>
</Tree>

In my imagination it would be easy if I could reference to the result of the XSLT output (FO) under processing. But I do not see how. There are probably several ways to achive such a list->tree translation.

Any help is highly welcome as I'm still at a beginner level regrading XSLT.

Thanks a lot
 

Patrick TJ McPhee

Usenet Archive
Joined
Apr 2, 2000
Messages
269
Reaction score
0
[...]

% The xml crerated fro SQL Server could look like this
% <LinkList>
% <T P="a" C="b"/>
% <T P="a" C="c"/>
% <T P="a" C="d"/>
% <T P="b" C="e"/>
% <T P="b" C="f"/>
% <T P="f" C="g"/>
% <T P="e" C="h"/>
% <T P="e" C="i"/>
% </LinkList>
%
% Now I would like to do an XSLT which creates a real tree out of that % (eg to show the tree as a tree in HTML) % % <Tree> % <a> % <b> % <e> % <h> % </h> % <i> % </i> % <f> % <g> % </g> % </f> % </b> % <c> % </c> % <d> % </d> % </a> % </Tree>

You can do this by defining a couple of keys (one which for P and one for C), and using them in your match expressions. I had trouble getting this to work without using a mode attribute to distinguish between the two matches on T, although I don't understand why (originally, I had a predicate on the currently modeless match, ``of course'').

This replaces `LinkList' with `Tree'. Within tree, it creates an element whose name is the value of the first P attribute which does not occur as the value of a C attribute, then it creates similar elements for all its children, and its children's children. This will break whenever if there's more than one possible root element.

<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version = '1.0'> <xsl:eek:utput method='xml' indent='yes'/> <xsl:key name='children' use='@C' match='T'/> <xsl:key name='parents' use='@P' match='T'/>

<xsl:template match = 'LinkList'>
<Tree>
<xsl:apply-templates select='T[count(key("children", @P)) = 0][1]'/> </Tree> </xsl:template>

<xsl:template match = 'T'>
<xsl:element name='{@P}'>
<xsl:apply-templates select='key("parents", @P)' mode='sub'/> </xsl:element> </xsl:template>

<xsl:template match = 'T' mode='sub'>
<xsl:element name='{@C}'>
<xsl:apply-templates select='key("parents", @C)' mode='sub'/> </xsl:element> </xsl:template> </xsl:stylesheet>

% In my imagination it would be easy if I could reference to the result % of the XSLT output (FO) under processing.

I don't know what you mean, but FO usually means `formatting object', which is the name given to a specific set of XML elements which describe physical page layouts, more or less. I expect what you're talking about is the result tree, and there are extensions which allow you to get at it, at the cost of being tied to xslt processors which support whatever extension you end up using. It's not clear to me how that will be helpful, though.
 

Rolf Kemper

Usenet Archive
Joined
Mar 26, 2003
Messages
35
Reaction score
0
(Patrick TJ McPhee) wrote:
In article ,
Rolf Kemper wrote:

[...]

% The xml crerated fro SQL Server could look like this
% <LinkList>
% <T P="a" C="b"/>
% <T P="a" C="c"/>
% <T P="a" C="d"/>
% <T P="b" C="e"/>
% <T P="b" C="f"/>
...

Dear Patrick,
thanks for the quick answer. I will try to study (and understand) it next days. Your suggestion about FO is right ( I just concentrated on the tree here, format info is added later). Thanks for the hint that not all xslt procesors may be able to do that.

Rolf
 

Rolf Kemper

Usenet Archive
Joined
Mar 26, 2003
Messages
35
Reaction score
0
(Rolf Kemper) wrote:
(Patrick TJ McPhee) wrote:
> In article ,
> Rolf Kemper wrote:
>
> [...]
>
> % The xml crerated fro SQL Server could look like this
> % <LinkList>
> % <T P="a" C="b"/>
> % <T P="a" C="c"/>
> % <T P="a" C="d"/>
> % <T P="b" C="e"/>
...

Dear Patrick,

meantime I tried your xslt. I expected an xml output shown in my browser window but what I see is just a blank page. The same occcurs in case I execute it in XML SPY. My browser is IE 6.0...

In case I put just XXX after the <tree> elemnt in your xslt I see just XXX in the browser page.

Any idea what I'm doing wrong ?

Thanks

Rolf
 

Patrick TJ McPhee

Usenet Archive
Joined
Apr 2, 2000
Messages
269
Reaction score
0
Rolf Kemper wrote:
[...]

% meantime I tried your xslt. I expected an xml output shown in my % browser window but what I see is just a blank page. The same occcurs % in case I execute it in XML SPY. My browser is IE 6.0...

The xslt gives the exact output you asked for. I would not expect this to show up in a browser as it consists entirely of element tags, and browsers typically display only text content.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,042
Messages
3,104,223
Members
248,131
Latest member
Tonypshaw1

Latest Threads

Top