Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Link list to hierarchy tree

Reply
Thread Tools

Link list to hierarchy tree

 
 
Rolf Kemper
Guest
Posts: n/a
 
      03-07-2004
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
 
Reply With Quote
 
 
 
 
Patrick TJ McPhee
Guest
Posts: n/a
 
      03-07-2004
In article <(E-Mail Removed) >,
Rolf Kemper <(E-Mail Removed)> 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"/>
% <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 xmlnssl='http://www.w3.org/1999/XSL/Transform'
version = '1.0'>
<xslutput 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.
--

Patrick TJ McPhee
East York Canada
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
object_id tree hierarchy in ruby ? Pokkai Dokkai Ruby 0 02-18-2008 11:02 AM
Tree view hierarchy Patrick Spence Ruby 5 12-21-2006 06:37 PM
Displaying Preorder Tree Traversal Hierarchy in ASP? FrankEBailey ASP General 2 02-18-2006 07:48 PM
Dynamic Controls in asp.net...Building a tree hierarchy..please he =?Utf-8?B?SXJmYW4gQWtyYW0=?= ASP .Net 0 12-13-2004 07:39 PM
B tree, B+ tree and B* tree Stub C Programming 3 11-12-2003 01:51 PM



Advertisments