Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

position() and last()

Question posted by: Dennis (Guest) on June 27th, 2008 07:07 PM
Here is what I am trying to do...

I have an XML doc that looks like:

<root>
<node>value1</node>
<node>value2</node>
<node>value3</node>
<node>value4</node>
</root>

I am looping thru the node's with a for-each like:

<xsl:for-each select="node">
<xsl:value-of select="."/>
</xsl:for-each>

I want to put a comma between each value so the output looks like:

value1,value2,value3,value4

I think I need to use position() and last() to get it to work so that I
only output the comma in the loop IF the node that is being processed is
NOT the last one.

How would I do this?

TIA,

--

Dennis
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
Martin Honnen's Avatar
Martin Honnen
Guest
n/a Posts
June 27th, 2008
07:07 PM
#2

Re: position() and last()
Dennis wrote:
Quote:
Originally Posted by
<xsl:for-each select="node">
<xsl:value-of select="."/>
</xsl:for-each>
>
I want to put a comma between each value so the output looks like:
>
value1,value2,value3,value4
>
I think I need to use position() and last() to get it to work so that I
only output the comma in the loop IF the node that is being processed is
NOT the last one.


<xsl:for-each select="node">
<xsl:value-of select="."/>
<xsl:if test="position() != last()">,</xsl:if>
</xsl:for-each>

With XSLT 2.0 it is easier, you don't need for-each,
<xsl:value-of select="node" separator=","/>
suffices.


--

Martin Honnen
http://JavaScript.FAQTs.com/

Dennis's Avatar
Dennis
Guest
n/a Posts
June 27th, 2008
07:07 PM
#3

Re: position() and last()
On Wed, 21 May 2008 18:13:49 +0200, Martin Honnen <mahotrash@yahoo.de>
wrote:
Quote:
Originally Posted by
>Dennis wrote:
>
Quote:
Originally Posted by
><xsl:for-each select="node">
> <xsl:value-of select="."/>
></xsl:for-each>
>>
>I want to put a comma between each value so the output looks like:
>>
>value1,value2,value3,value4
>>
>I think I need to use position() and last() to get it to work so that I
>only output the comma in the loop IF the node that is being processed is
>NOT the last one.

>
<xsl:for-each select="node">
<xsl:value-of select="."/>
<xsl:if test="position() != last()">,</xsl:if>
</xsl:for-each>
>
>With XSLT 2.0 it is easier, you don't need for-each,
<xsl:value-of select="node" separator=","/>
>suffices.


Thanks. I was on the right track.

--

Dennis

 
Not the answer you were looking for? Post your question . . .
182,263 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors