Apparently Mohammad Azam has recently discovered using XSLT for code generation. One of the issues he's learned is that when using XSLT to generate code you have to be aware of where you are in the list of nodes that's currently being traversed. The particular problem he's got is that looping through a list of fields and tacking on a comma afterwards leaves one stray comma on the end of the list.
The way I solved this problem was a combination of a “switch” statement and using the built-in position() and last() functions to know where you are:
<xsl:when test=”position() = 1”>emit code here for first node in list</xsl:when>
<xsl:when test=”position() = last()>emit code here for last node in list</xsl:when>
<xsl:otherwise>emit code here for all other nodes in list</xsl:otherwise>
I have been successful in building various templates for code generation over the years using XSLT. However, as the complexity of the generated code increases (think multi-tiered, layered application) then it becomes harder to manage (you can go crazy from staring at XSL for many hours!). Kathleen Dollard does a good job of pulling together various technologies (XSL, Code Dom, Db Info Schema, XSD) in her book Code Generation in Microsoft .NET. Unfortunately, it came out a few months too late for me! Why? Because I had already hit my head many times and worked through most all the issues she does in her book by the time it was published.
Instead, I had already decided to give CodeSmith a try and haven't looked back since. Not only is the familiar ASP/ASP.NET style more “natural“ for developers, but the built-in support for database schemas as well as Xml-based schemas/input and copious samples makes getting off the ground much, much easier.