Re: [xsl] backwards tree-traversal algorithm?

Subject: Re: [xsl] backwards tree-traversal algorithm?
From: Stephen Cunliffe <scunliffe@xxxxxxxxxxxxxxxxxx>
Date: Wed, 23 Oct 2002 10:51:55 -0400
This isn't quite what you want, but it may help...

Define this template first:
<xsl:template match="*" mode="whereAmI">
<xsl:param name="location"/>
<xsl:choose>
<xsl:when test="name(.) = 'root'">Location = <xsl:value-of select="name(.)"/> / <xsl:value-of select="$location"/> </xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="parent::*" mode="whereAmI">
<xsl:with-param name="location"><xsl:value-of select="name(.)"/><xsl:if test="string($location) != ''"> / <xsl:value-of select="$location"/></xsl:if></xsl:with-param>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</xsl:template>


Then, when you need to know "where you are", call this one.

<xsl:apply-templates select="." mode="whereAmI"/>


It should return *if* 'inside' <c/> (with your data)...


"Location = root / a / b / c"

Cheers,

Steve

On 10/23/02 10:22 AM, jm3 wrote::

hello, having some trouble making this work: within my stylesheet,
i have selected a node somewhere in the midst of the document tree,
several nested elements deep.  what i would like to do is, given
this node N (at arbitrary depth), walk *back* up the document tree
hierarchy, element by element, printing the successive parent elements
(technically, printing each element's "name" @ribute).

so with node N being <c> and the following document:

<root>
 <a>
	  <b>
		  <c/>
		</b>
		<d/>
		<e/>
	</a>
</root>

i want to print: b, a, root.

my pseudocode was along the lines of:

current = node
while current != root
 current = ../
	print current/@name

seems simple.

recursive templates seemed like the way to do this in xsl, but i
can't get this to do anything, apparently because the processor i'm
using (sablotron 0.90) won't let me use XPath on the node set i am
passing with call-template (as a param) because it's "not a node-set".

so i guess i have two questions:

1. what is the best algorithm/approach to walk back up the parent:: tree, printing each element?

2. does sabcmd not have a node-set() function? is this what's stopping me?

(if it helps, the stylesheet will know what depth level the current
node is at, and the unique @name attribute of each parent element.)

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

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

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







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



Current Thread