Link list to hierarchy tree

Discussion in 'XML' started by Rolf Kemper, Mar 7, 2004.

  1. Rolf Kemper

    Rolf Kemper Guest

    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
    Rolf Kemper, Mar 7, 2004
    #1
    1. Advertising

  2. 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"/>
    % <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.
    --

    Patrick TJ McPhee
    East York Canada
    Patrick TJ McPhee, Mar 7, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SXJmYW4gQWtyYW0=?=

    Dynamic Controls in asp.net...Building a tree hierarchy..please he

    =?Utf-8?B?SXJmYW4gQWtyYW0=?=, Dec 13, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    498
    =?Utf-8?B?SXJmYW4gQWtyYW0=?=
    Dec 13, 2004
  2. Stub

    B tree, B+ tree and B* tree

    Stub, Nov 12, 2003, in forum: C Programming
    Replies:
    3
    Views:
    10,109
  3. FrankEBailey
    Replies:
    2
    Views:
    195
    Anthony Jones
    Feb 18, 2006
  4. Patrick Spence

    Tree view hierarchy

    Patrick Spence, Dec 21, 2006, in forum: Ruby
    Replies:
    5
    Views:
    128
    Tom Pollard
    Dec 21, 2006
  5. Replies:
    1
    Views:
    434
Loading...

Share This Page