tags:

views:

212

answers:

3

I am rendering a list of tickers to html via xslt and I would like for the list to be comma deliimited. Assuming I was going to use xsl:for-each...

<xsl:for-each select="/Tickers/Ticker">
    <xsl:value-of select="TickerSymbol"/>,
</xsl:for-each>

What is the best way to get rid of the trailing comma? Is there something better than xsl:for-each?

+4  A: 
<xsl:for-each select="/Tickers/Ticker">
    <xsl:if test="position() &gt; 1">, </xsl:if>
    <xsl:value-of select="TickerSymbol"/>
</xsl:for-each>
Mehrdad Afshari
Don't know if you meant to encode the greater than symbol or not, but using .Net the correct syntax was:<xsl:if test="position() > 1">, </xsl:if>
MadMax1138
+4  A: 

In XSLT 2.0 you could do it (without a for-each) using the string-join function:

<xsl:value-of  select="string-join(/Tickers/Ticker, ',')"/>
Mads Hansen
Thanks, I'm currently using .Net 2.0 so as far as I know xslt 2.0 is not an option.
MadMax1138
You could use Saxon .NET http://sourceforge.net/projects/saxondotnet/
Mads Hansen
+1  A: 

In XSLT 1.0, another alternative to using xsl:for-each would be to use xsl:apply-templates

<xsl:template match="/">

   <!-- Output first element without a preceding comma -->
   <xsl:apply-templates select="/Tickers/Ticker[position()=1]" />

   <!-- Output subsequent elements with a preceding comma -->
   <xsl:apply-templates select="/Tickers/Ticker[position()&gt;1]">
      <xsl:with-param name="separator">,</xsl:with-param>
   </xsl:apply-templates>

</xsl:template>

<xsl:template match="Ticker">
   <xsl:param name="separator" />
   <xsl:value-of select="$separator" /><xsl:value-of select="TickerSymbol" />
</xsl:template>
Tim C