Since I wrote my article on arrays and commas last week, my blog has received quite a lot of Google searches about how to remove the last comma when using an XSLT stylesheet, so it seems only fitting that I should explain this in more detail.
It is possible for an XSLT stylesheet to generate a comma-separated list from a node set. The basic idea is to place a comma before every element, except the first. In essence:
<xsl:for-each select="..."> <xsl:if test="position() > 1">, </xsl:if> ... </xsl:for-each>
When processing a node set, the position() function indicates the index of the element within that node set. The first element has position 1.
This gets much harder if you have to further filter the elements in the node set before printing them, because the position doesn’t let you find out the first displayed element. In XSLT 2.0 you can generate a new list from the old list by filtering, then traverse the new list to turn into a comma-separated output. In XSLT 1.0 you will have to either do all the filtering as part of the XPath selector of your loop, or determine the position of the valid element beforehand.
Hi. I'm Victor Nicollet,
Recent Comments