Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanwsmith/3091646 to your computer and use it in GitHub Desktop.
Save alanwsmith/3091646 to your computer and use it in GitHub Desktop.
Putting commas after each value in a loop except for the last one. Useful for JSON and the like
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!--
Example of putting commas after everything but the last item in a list.
This will process the XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item attKey="value1" />
<item attKey="value2" />
<item attKey="value3" />
<item attKey="value4" />
</root>
And output:
value1, value2, value3, value4
-->
<xsl:output method="text"/>
<xsl:template match="/root">
<!-- Put commas after everything except the last one. -->
<xsl:for-each select="item">
<xsl:value-of select="@attKey"/>
<xsl:if test="position()!=last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment