Re: [xsl] Position of a Child

Subject: Re: [xsl] Position of a Child
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Thu, 20 Sep 2001 14:58:24 -0400
Frank,

What you mean by "position of that child inside the context node" is actually a bit problematic, possibly meaning any of a number of things. How to get it depends both on what you mean, and what your input data looks like.

For starters, you could try (count(*[name()=$NodeName]/preceding-sibling::*) + 1) -- that is, 1 + the number of the node's preceding element siblings. If you have mixed content (and thus the siblings you want to count include text nodes), or if there's some other wrinkle (such as more than one child node being named $NodeName), come back.

position() isn't very useful because it returns the position of the context node within the current node list, which isn't what you want. You could make it be what you want by doing something like:

<xsl:for-each select="*">
  <xsl:if test="name()=$NodeName">
    <xsl:value-of select="."/>
    <xsl:value-of select="position()"/>
  </xsl:if>
</xsl:for-each>

But again, I'm making assumptions about what you really want -- and you can see this is more roundabout than simple counting the preceding siblings.

Cheers,
Wendell


At 02:17 PM 9/20/01, you wrote:
I'm having problems getting the child's position() of the context node.
I reference the child by its name (stored in a variable), like this:

<xsl:value-of select="*[name()=$NodeName]"/>

What I would like to get is the position of that child inside the context
node.


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================


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



Current Thread