Re: [xsl] backwards tree-traversal algorithm?

Subject: Re: [xsl] backwards tree-traversal algorithm?
From: jm3 <lists@xxxxxxx>
Date: Wed, 23 Oct 2002 11:59:18 -0400
On Wed, Oct 23, 2002 at 03:54:25PM +0100, Andrew Welch wrote:
> just use the ancestor::* axis
> 
> <xsl:for-each select="ancestor::*">
>   <xsl:value-of select="name()"/>
> </xsl:for-each/>
> 
> I bet you are kicking yourself...

:-] yup!  thank you, all who replied.

since i am doing (i believe it is called) "push-based" processing rather than pull, how would i specify which node is my starting node? 

neither
	<xsl:for-each select="$ancestor::$current">
nor
	<xsl:for-each select="$current/$ancestor::*">
worked.

after seeing how simple the above was, i am thinking i could do something similar to what you guys recommended (read: smarter than what i'm doing now) for my initial step of locating the "current" node.

i make document() calls to an xml file "dirs.xml" containing an xml representation of the directory structure where the source document is located. the reason that i want to iterate over the ancestor nodes is that i am doing a "breadcrumbs" sort of thing, where i want to show what the progression of parent nodes was in the tree.

at transformation time, the stylesheet gets a set of parameters from the command line: $parent0, $parent1, $parent2, etc. which contain the directory names of the preceding directories, and a $depth param which contains the depth of the file in the structure, eg.

sab sheet.xsl /foo/bar/baz/file.xml $depth=3 $parent0=foo $parent1=bar $parent3=baz $depth=3

i then use those parameters to create a nodeset variable pointing to the node within dirs.xml which corresponds to the location of the source document in the filesystem.

eg. i have a variable called $current, defined as follows:

<xsl:variable name="current">
  <xsl:choose>
    <xsl:when test="$depth=1"><xsl:copy-of select="document($d)/root/section" /></xsl:when>
    <xsl:when test="$depth=2"><xsl:copy-of select="document($d)/root/section[@id=$parent0]/section" 
    <xsl:when test="$depth=3"><xsl:copy-of select="document($d)/root/section[@id=$parent0]/section[@id=$parent1]/section" 
		...
		<xsl:otherwise> no logic coded for this level of hierarchy </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

this obviously seems very crude because it puts a strict upper bound on how deep the hierarchy can go, and it's really redundant.  but when i initially passed in the location path as a string, eg.

sab sheet.xsl /foo/bar/baz/file.xml $location=/foo/bar/baz/file.xml

and tried to turn $location into an xpath expression, i fell on my face because it's not possible to construct xpath on fly in XSL.

 jm3 / john manoogian III / jm3@xxxxxxx / http://jm3.net

          > > > http://jm3.net/linkworld < < <

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


Current Thread