[xsl] Determine the position of an element relative to its sibling

Subject: [xsl] Determine the position of an element relative to its sibling
From: "Ross, Douglas" <DRoss@xxxxxxxxxx>
Date: Tue, 1 Mar 2005 15:38:21 -0500
I want to use the position of an element ralative to its siblings to
construct an xpath to another element.

For example,

Input:
<TABLE>
	<THEAD>
		<TR>
			<TD>Header Col 1<A class="sort">Sort</A></TD>
			<TD>Header Col 2<A class="sort">Sort</A></TD>
		</TR>
	</THEAD>
	<TBODY>
		<TR>
			<TD id="column1">Col 1 Prototype</TD>
			<TD id="column2">Col 2 Prototype</TD>
		</TR>
	</TBODY>
</TABLE>

Template:
<xsl:template match="A[@class='sort']">
	<A>
		<xsl:attribute name="url">
			base?field=<xsl:value-of
select="ancestor::TABLE/TBODY/TR/TD[<what goes here?>]/@id"/>
		</xsl:attribute>
	</A>
</xsl:template>

Output:
<TABLE>
	<THEAD>
		<TR>
			<TD>Header 1<A url="base?field=column1"
class="sort">Sort</A></TD>
			<TD>Header 2<A url="base?field=column2"
class="sort">Sort</A></TD>
		</TR>
	</THEAD>
	<TBODY>
		<TR>
			<TD id="column1">Col 1 Prototype</TD>
			<TD id="column2">Col 2 Prototype</TD>
		</TR>
	</TBODY>
</TABLE>

Things I have tried in <what goes here?>:

position()

for $i in ../TD return if (generate-id(../TD[$i]) = generate-id(..))
then $i else 0

And various other things to no avail. I'm sure there is an easy way to
do this but searching for position and element adnosium finds thousands
of hits. My eyeballs are crosseyed from reading so many wonderful list
entries about stuff that I'm sure will help me in the long run but are
not solving my immediate problem.

Thanks for any advice, hints, etc.

Douglas Ross
Developer, HTML UI Framework
Kronos
E-mail: dross@xxxxxxxxxx
www.kronos.com

Current Thread