Re: [xsl] Get Position of Node in Ancestor Context

Subject: Re: [xsl] Get Position of Node in Ancestor Context
From: Ted Stresen-Reuter <tedmasterweb@xxxxxxx>
Date: Mon, 10 Mar 2003 11:02:35 -0600
Answered my own question, but would still be interested in knowing what others think of this solution...

<xsl:template match="input|select|textarea">
<xsl:copy>
<xsl:for-each select="@*">
<xsl:copy />
</xsl:for-each>
<xsl:variable name="currElemId" select="@id" />
<xsl:attribute name="tabindex"></xsl:attribute>
<xsl:for-each select="ancestor::form//select|ancestor::form//input|ancestor::form// textarea">
<xsl:if test="not(@type = 'hidden') or @type = 'submit'">
<xsl:if test="@id = $currElemId">
<xsl:attribute name="tabindex"><xsl:value-of select="position()" /></xsl:attribute>
</xsl:if>
</xsl:if>
</xsl:for-each>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>


The style sheet basically adds tabindex attributes to all form elements. What I'm unhappy with is the value of the select attribute of the second for-each element (the one that creates a node set of all child elements of a FORM element, regardless of what separates them). It seems odd that I have to fully qualify the child element I'm looking for, but using ancestor::form//select|input|textarea only matches the first element in the list.

Also, what is the performance impact on setting up such for-each loops and is there a faster way to reproduce this node-set in the context of the current element (call-template with-param???)?

Thanks!

Ted Stresen-Reuter

On Monday, March 10, 2003, at 09:21 AM, Ted Stresen-Reuter wrote:

Hi,

I'm trying to get the position of the current node in the context of an ancestor.

For example, I have a form element that has a number of children elements, including non-form elements (such as P and DIV elements). I'm trying to retrieve the position of an INPUT element relative to the parent FORM element, but not taking into account any non-form elements (such as P and DIV elements).

Currently I have a template that matches "input | select | textarea" but because they occur in the context of a TD element, position() returns "1". I've tried using <xsl:value-of select="ancestor::form/*/*[position()]" />, but this just isn't doing the trick.

Any suggestions how I can get the position of the current element relative to a parent form element?

Ted Stresen-Reuter


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