Re: [xsl] find out if my parent is the last sibling

Subject: Re: [xsl] find out if my parent is the last sibling
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Sun, 29 Aug 2004 10:09:13 +0100
Hi Jan,

> for the child it is easy to find out if it is the last in the row,
> <xsl:if test="position()=last()">*</xsl:if>
> but how can a child know this about it's parent?

Depending on how you're doing the processing of the parents, you can
use a variable as follows:

  <xsl:for-each select="parent">
    <xsl:variable name="is-last" select="position() = last()" />
    ...
    <xsl:for-each select="child">
      ...
      <xsl:if test="$is-last">
        parent has no following siblings
      </xsl:if>
      ...
    </xsl:for-each>
  </xsl:for-each>

Or you can use the test:

  ../following-sibling::*

which tests more literally whether the parent node has any following
siblings (but is slightly less efficient than storing this information
in a variable).

I often find, though, that the best thing to do is to rearrange the
code so that whatever you want to happen when the parent is the last
sibling happens when the current node is the parent. In other words,
to do something like:

  <xsl:for-each select="parent">
    ...
    <xsl:for-each select="child">
      ...
    </xsl:for-each>
    <xsl:if test="position() = last()">...</xsl:if>
  </xsl:for-each>

If you showed us what you were actually trying to do then we could see
if that was a more appropriate solution.
  
Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/

Current Thread