[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: position in a 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
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|