cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Meyer <>
Subject Re: recursion & sql in xsp
Date Tue, 29 Aug 2000 22:01:03 GMT
This can be done in a slightly different manner using xsl.

Make your XML output something like this:

   <category id="0" name="root"   parent="0"/>
   <category id="1" name="blue"   parent="0"/>
   <category id="2" name="red"    parent="0"/>
   <category id="3" name="orange" parent="2"/>
   <category id="4" name="green"  parent="1"/>

<topoftree id="0"/>

This should be the direct result of a single database query.

Then make your XSL something like this:

<xsl:template name="level">
   <xsl:param name="id"/>
   <li><xsl:value-of select="/categorylist/category[id=$id]/@name"/></li>
   <xsl:for-each select="/categorylist/category[parent=$id]">
     <xsl:call-template name="level">
       <xsl:param name="id"><xsl:value-of select="@parent"/></xsl:param>

<xsl:call-template name="level">
   <xsl:param name="id"><xsl:value-of select="topoftree/@id"/></xsl:param>

I did this from the top of my head so you'll probably have to modify 
it slightly to get it to work. Also, you'll want to add in some 
<xsl:if>'s to count the number of children nodes before outputting an 
empty <li></li> pair... but this should give you the general idea.

The disadvantage of doing things this way is that you have to include 
your entire category tree with each XML document. There's probably a 
way to reduce that too though.

And of course, this doesn't allow you to do other XML processing on 
the resulting tree... but it may be a start in the right direction.

Chris Meyer

>On Tue, 29 Aug 2000, Steve Conover wrote:
>>  Another problem:
>>  I have a generic category tree stored in a table, like this
>>  catgid	catgname	catgparentid
>>  0		root		0
>>  1		blue		0
>>  2		red		0
>>  3		orange	2
>>  4		green		1
>>  Imagine there are 20+ levels in my hierarchy...I need to design something in
>>  xsp to start with a catgid, make a sql call, get that category's parentid,
>>  and feed it back to the same sql call until it reaches the root id (0).  All
>>  the while I need to construct some xml that reflects the hierarchy. 
>hmm, that's a kinda tricky one - unlike the classic department/employee
>example, you don't even know how deep the nest is going to be - which
>means you can't even use the new esql logicsheet to do it afaict. you're
>probably best off writing a little logicsheet to do this using straight up
>- donald

View raw message