Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I render a comma delimited list using xsl:for-each

Tags:

xslt

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?

like image 849
MadMax1138 Avatar asked Dec 22 '09 17:12

MadMax1138


People also ask

What is xsl for-each select?

The <xsl:for-each> element selects a set of nodes and processes each of them in the same way. It is often used to iterate through a set of nodes or to change the current node. If one or more <xsl:sort> elements appear as the children of this element, sorting occurs before processing.

What is the use of for-each in XSLT?

The <xsl:for-each> element allows you to do looping in XSLT.

What is XSLT 3. 0?

XSLT 3.0 allows XML streaming which is useful for processing documents too large to fit in memory or when transformations are chained in XML Pipelines. Packages, to improve the modularity of large stylesheets. Improved handling of dynamic errors with, for example, an xsl:try instruction.

What is Tokenize in XSLT?

XSLT/XPath Reference: XSLT elements, EXSLT functions, XPath functions, XPath axes. str:tokenize() splits a string using a set of characters as delimiters that determine where the splits should occur, returning a node-set containing the resulting strings.


2 Answers

<xsl:for-each select="/Tickers/Ticker">
    <xsl:if test="position() > 1">, </xsl:if>
    <xsl:value-of select="TickerSymbol"/>
</xsl:for-each>
like image 88
mmx Avatar answered Oct 03 '22 10:10

mmx


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, ',')"/>  
like image 20
Mads Hansen Avatar answered Oct 03 '22 11:10

Mads Hansen