PDA

View Full Version : [SOLVED] Using XSLT to place commas between authors


Gregoryt
08-04-08, 06:23 AM
CODE:

<author><strong>Author: </strong>
<xsl:for-each select="author">
<xsl:value-of select="."/><xsl:text>, </xsl:text>
</xsl:for-each>
<br/>
</author>

PROBLEM:
This code does place a comma after each author node's text data, but it does so for the last node too, resulting in an extra comma.
Is there a way to check for when at the last node and not place a comma, or is there a code for the HTML escape code for the backspace character?

USE: This is from an xsl file for a data sheet that is used to transform various types of XML documents.

Gregoryt
08-04-08, 06:42 AM
CODE SOLUTION:
<author><strong>Author: </strong>
<xsl:for-each select="author">
<xsl:value-of select="."/>
<xsl:if test="following-sibling::author">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
<br/>
</author>

EXPLANATION:
I had to use the following-sibling::author XPath axis in the <xsl:if> element as the value of the test attribute. When there is no following sibling, then no comma and space (, ) will be placed between the <xsl:text> and </xsl:text> nodes.