Fwd: Re: [xsl] flat tree to expanded tree

Subject: Fwd: Re: [xsl] flat tree to expanded tree
From: Kevin Jones <kjones@xxxxxxxxxxx>
Date: Fri, 13 Dec 2002 15:13:07 +0000
Hi,

On Thursday 12 Dec 2002 10:04 pm, bix xslt wrote:
> I am working on a grouping problem that I have been placed in charge of.
> From the list I've gathered that I more than likely need to use Steve
> Muench's algorithm to quickly and effectively sort my somewhat complex
> list. However, I'm at a loss as to how to procede.
>
> I've included the DTD and a sample XML for the problem at hand, however I
> feel that I should explain why this is more complex than a simple sort.  In
> addition to sorting each of the 'proc' elements by alphabetizing, I would
> also like to sort by creating a collapsable tree.  In addition, I am
> expecting my xml tree to be scattered across several files.

Using keys across multiple files input files creates problems so you might
want to investigate if you can get them merged into one input, e.g. by
running two transforms, one to merge the input and a second to perform the
operation you want.

The difficult part of your problem appears to be getting an execution
listing. You can do this with a recursive template that follows the
dependencies to find an execution order (see below).

It should be fairly straight forward to expand this to provide the desired
output format, flat, hierarchical or something else. One thing to watch is I
have not implemented any checking for cyclic dependencies in the input. Your
processor may stop the transform running out of control but its probably
safer to implement your own limits.

Kev.

 <xsl:variable name='all' select='document("file1.xml")/list/proc |
document("file2.xml")/list/proc |
    document("file3.xml")/list/proc | document("file4.xml")/list/proc'/>

 <xsl:template match='/'>
  <xsl:call-template name='order'>
   <xsl:with-param name='unordered' select='$all'/>
  </xsl:call-template>
 </xsl:template>

 <xsl:template name='order'>
  <xsl:param name='unordered'/>
  <xsl:param name='process' select='1'/>

  <xsl:variable name='try' select='$unordered[$process]'/>
  <xsl:variable name='deps' select='$unordered[@id = $try/dpnd/item/@idr]'/>

  <xsl:choose>
   <xsl:when test='$deps'>
    <xsl:for-each select='$unordered'>
     <xsl:if test='generate-id() = generate-id($deps[1])'>
      <xsl:variable name='at' select='position()'/>
      <xsl:call-template name='order'>
       <xsl:with-param name='unordered' select='$unordered'/>
       <xsl:with-param name='process' select='$at'/>
      </xsl:call-template>
     </xsl:if>
    </xsl:for-each>
   </xsl:when>
   <xsl:when test='$try'>
    <xsl:copy-of select='$try'/>
    <xsl:call-template name='order'>
     <xsl:with-param name='unordered'
select='$unordered[position()!=$process]'/>
    </xsl:call-template>
   </xsl:when>
  </xsl:choose>
 </xsl:template>

-------------------------------------------------------


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread