Link list to hierarchy tree

R

Rolf Kemper

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
 
P

Patrick TJ McPhee

[...]

% 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.
 

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
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top