This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: how to add a cell in a for-each iteration?
- From: Jeni Tennison <jeni at jenitennison dot com>
- To: "Thomas Sandor" <tsandor at rndsoft dot com>
- Cc: XSL-List at lists dot mulberrytech dot com
- Date: Tue, 30 Jul 2002 12:51:59 +0100
- Subject: Re: [xsl] how to add a cell in a for-each iteration?
- Organization: Jeni Tennison Consulting Ltd
- References: <003e01c237bd$de78f1d0$1c01010a@tomaxp>
- Reply-to: xsl-list at lists dot mulberrytech dot com
Hi Thomas,
> I've just started to build a website using Cocoon engine, and use
> XML/XSLT -> HTML tranforms. I've read a lot on XSL formatting, and
> everything goes clear, except for one problem. I'd like to add eg.:
> " | " in between an iteration.
>
> Eg.: <list>
> <item>menu 1</item>
> <item>menu 2</item>
> <item>menu 3</item>
> </list>
>
> and in a XSLT template:
> <xsl:for-each select="list/item">
> <xsl:value-of select = "."/>
> </xsl:for-each>
>
> would list them.
>
> So what I'd like to achieve is to insert eg.: " | " string in between my
> <item>s to perform: "menu 1 | menu 2 | menu 3" string.
The secret is to use the position() function to test whether the
particular item you're looking at is the last in the list or not. If
it isn't, then you want to add the " | "; if it is, you don't. So use:
<xsl:for-each select="list/item">
<xsl:value-of select="." />
<xsl:if test="position() != last()"> | </xsl:if>
</xsl:for-each>
---
In XSLT 2.0, use my personal favourite cute feature:
<xsl:value-of select="list/item" separator=" | " />
Cheers,
Jeni
---
Jeni Tennison
http://www.jenitennison.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list