[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: find out if my parent is the last sibling

Subject: Re: find out if my parent is the last sibling
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Sun, 29 Aug 2004 10:09:13 +0100
xsl if parent
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.