Subject: Re: [xsl] position in a conditional for-each From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx> Date: Thu, 4 Jan 2001 09:15:41 +0000 |
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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] position in a conditional, Craig Pfeifer | Thread | Re: [xsl] position in a conditional, David Carlisle |
Re: [xsl] Does XSL have arrays?, Richard Light | Date | Re: [xsl] Variable as Position Indi, Jeni Tennison |
Month |