Created
July 11, 2012 16:47
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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