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

Re: position in a conditional for-each

Subject: Re: position in a conditional for-each
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Thu, 4 Jan 2001 09:15:41 +0000
xsl conditional for each
Hi James,

> I'm trying treat the last element that passes a condition in a
> for-each differently. Every element which passes the condition
> displays its value, and that is followed by a comma. The problem is
> that I can't know when the last element to meet the condition has
> passed.I would like to omit trailing comma.

The usual way of omitting the trailing comma, as others have said, is
to use the position of the current node to work out whether to add the
comma or not.  In your case, this would look like:

<xsl:for-each select="COOLJEX_ATTRIBUTE">
  <xsl:if test="@key = 'true'">
    <xsl:value-of select="@name" />
    <xsl:if test="position() != last()">, </xsl:if>
  </xsl:if>
</xsl:for-each>

However, this won't work with the above because the last
COOLJEX_ATTRIBUTE visited by the xsl:for-each (and therefore the last
node in the context node list) is not necessarily the same as the last
COOLJEX_ATTRIBUTE with @key equal to 'true' (and therefore not
necessarily the same as the last name that's outputted).

What you need to do is to make the context node list (the stuff that's
selected for iterating over) to be representative of the output. You
need to select only those COOLJEX_ATTRIBUTEs that have @key = 'true'
in the first place.  You can move the test that you have within the
xsl:if into a predicate on the select expression:

  COOLJEX_ATTRIBUTE[@key = 'true']

Heather gave a solution that involved using xsl:apply-templates.  If
you wanted to stick to using xsl:for-each, then the solution is:

  <xsl:for-each select="COOLJEX_ATTRIBUTE[@key = 'true']">
    <xsl:value-of select="@name" />
    <xsl:if test="position() != last()">, </xsl:if>
  </xsl:for-each>

I hope that helps,

Jeni

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



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


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.