XSLT: Generate comma-separated string from element values

Published:

This is one way to create a comma-separated list of values from a set of elements.

<xsl:for-each select="$orders/cm:Error">
    <xsl:value-of select="cm:errorCode"/>
    <xsl:if test="position() != last()">
        <xsl:text>,</xsl:text>
    </xsl:if>
</xsl:for-each>
<!-- Source: https://stackoverflow.com/a/668850/39321 -->

This is another.

<xsl:for-each select="$elements/Error[1]">
    <xsl:value-of select="errorCode"/>
    <xsl:for-each select="following-sibling::Error">
        <xsl:value-of select="concat(',',errorCode)"/>
    </xsl:for-each>
</xsl:for-each>
<!-- Source: https://stackoverflow.com/a/666600/39321 -->