Transform reports to CSV using XSLT

The following XSLT stylesheet is a fairly simple transformation allowing to convert XML metric reports to CSV format (the columns do not have a fixed length, instead a “,” char is used as the delimiter). The CSV columns have the following order: Module, Namespace, Type, Member, Maintainability Index, Cyclomatic Complexity, Class Coupling, Depth of Inheritance, Lines of Code.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">

  <xsl:output method="text" indent="no" encoding="utf-8" />
  <xsl:strip-space elements="*" />

  <xsl:template match="/">
    <xsl:apply-templates></xsl:apply-templates>
  </xsl:template>

  <xsl:template name="MetricValues">
    <xsl:variable name="v1" select="./Metric[@Name = 'MaintainabilityIndex']/@Value" />
    <xsl:variable name="v2" select="./Metric[@Name = 'CyclomaticComplexity']/@Value" />
    <xsl:variable name="v3" select="./Metric[@Name = 'ClassCoupling']/@Value" />
    <xsl:variable name="v4" select="./Metric[@Name = 'DepthOfInheritance']/@Value" />
    <xsl:variable name="v5" select="./Metric[@Name = 'LinesOfCode']/@Value" />
    <xsl:variable name="s" select="concat($v1, ',', $v2, ',', $v3, ',', $v4, ',', $v5)" />
    <xsl:value-of select="$s" />
    <xsl:text>&#10</xsl:text>
  </xsl:template>

  <xsl:template match="Metrics">
    <xsl:variable name="moduleName" select="ancestor::Module/@Name" />
    <xsl:variable name="namespaceName" select="ancestor::Namespace/@Name" />
    <xsl:variable name="typeName" select="ancestor::Type/@Name" />
    <xsl:variable name="memberName" select="ancestor::Member/@Name" />
    <xsl:variable name="s" select="concat($moduleName, ',', $namespaceName, ',', $typeName, ',', $memberName, ',')" />
    <xsl:if test="$moduleName != ''">
      <xsl:value-of select="$s" />
    </xsl:if>
    <xsl:call-template name="MetricValues" />
  </xsl:template>
</xsl:stylesheet>