position() and last()

D

Dennis

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,
 
M

Martin Honnen

Dennis said:
<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.
 
D

Dennis

<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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top