XML 的 XSLT 转换由 metric.exe 生成

XSLT Transform for XML Generated by metric.exe

我正在尝试转换 metric.exe 为我的解决方案中的程序集生成的以下 XML。而且我不是 XSLT 方面的专家,并且努力达到问题底部指示的格式。请帮忙

XML

<?xml version="1.0" encoding="utf-8"?>
<CodeMetricsReport Version="12.0">
  <Targets>
    <Target Name="E:\some-imaginary-path\some-imaginary-dll">
      <Modules>
        <Module Name="some-imaginary-dll" AssemblyVersion="1.0.0.0" FileVersion="1.0.0.0">
          <Metrics>
            <Metric Name="MaintainabilityIndex" Value="84" />
            <Metric Name="CyclomaticComplexity" Value="95" />
            <Metric Name="ClassCoupling" Value="109" />
            <Metric Name="DepthOfInheritance" Value="4" />
            <Metric Name="LinesOfCode" Value="214" />
          </Metrics>
          <Namespaces>
            <Namespace Name="LMS.SomeNamespace">
              <Metrics>
                <Metric Name="MaintainabilityIndex" Value="89" />
                <Metric Name="CyclomaticComplexity" Value="19" />
                <Metric Name="ClassCoupling" Value="18" />
                <Metric Name="DepthOfInheritance" Value="1" />
                <Metric Name="LinesOfCode" Value="29" />
              </Metrics>
              <Types>
                <Type Name="SomeType1">
                  <Metrics>
                    <Metric Name="MaintainabilityIndex" Value="100" />
                    <Metric Name="CyclomaticComplexity" Value="1" />
                    <Metric Name="ClassCoupling" Value="1" />
                    <Metric Name="DepthOfInheritance" Value="0" />
                    <Metric Name="LinesOfCode" Value="0" />
                  </Metrics>
                  <Members>
                    <Member Name="SomeMemberFor1">
                      <Metrics>
                        <Metric Name="MaintainabilityIndex" Value="100" />
                        <Metric Name="CyclomaticComplexity" Value="1" />
                        <Metric Name="ClassCoupling" Value="1" />
                        <Metric Name="LinesOfCode" Value="0" />
                      </Metrics>
                    </Member>
                  </Members>
                </Type>
                <Type Name="SomeType2">
                  <Metrics>
                    <Metric Name="MaintainabilityIndex" Value="76" />
                    <Metric Name="CyclomaticComplexity" Value="3" />
                    <Metric Name="ClassCoupling" Value="6" />
                    <Metric Name="DepthOfInheritance" Value="1" />
                    <Metric Name="LinesOfCode" Value="7" />
                  </Metrics>
                  <Members>
                    <Member Name="SomeMemberFor2" File="e:\some-file.cs" Line="27">
                      <Metrics>
                        <Metric Name="MaintainabilityIndex" Value="67" />
                        <Metric Name="CyclomaticComplexity" Value="2" />
                        <Metric Name="ClassCoupling" Value="5" />
                        <Metric Name="LinesOfCode" Value="6" />
                      </Metrics>
                    </Member>
              </Namespace>
          </Namespaces>
        </Module>
             </Namespace>
          </Namespaces>
        </Module>
      </Modules>
    </Target>
  </Targets>
</CodeMetricsReport>

XSLT

到目前为止,当使用 powershell 脚本进行转换时,这不会产生任何结果(某些变体已经部分起作用,因此 powershell 脚本在这里没有问题)

<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="html" indent="yes"/>
   <xsl:template match="/">
  <html>
  <body>
  <h2>x</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th>Name Space Name</th>
      <th>Type Name</th>
      <th>Member Name</th>
      <th>MaintainabilityIndex</th>
      <th>CyclomaticComplexity</th>
      <th>ClassCoupling</th>
      <th>DepthOfInheritance</th>
      <th>LinesOfCode</th>

    </tr>
    <xsl:for-each select="CodeMetricsReport/Targets/Target/Modules/Namespaces/Namespace/Metrics">
    <tr>
      <td><xsl:value-of select="./Metric[@Name = '????']/@Value" /></td>
      <td><xsl:value-of select="./Metric[@Name = '????']/@Value" /></td>
      <td><xsl:value-of select="./Metric[@Name = '????']/@Value" /></td>
      <td><xsl:value-of select="./Metric[@Name = 'MaintainabilityIndex']/@Value" /></td>
      <td><xsl:value-of select="./Metric[@Name = 'CyclomaticComplexity']/@Value"/></td>
      <td><xsl:value-of select="./Metric[@Name = 'ClassCoupling']/@Value"/></td>
      <td><xsl:value-of select="./Metric[@Name = 'DepthOfInheritance']/@Value"/></td>
      <td><xsl:value-of select="./Metric[@Name = 'LinesOfCode']/@Value"/></td>
      </tr>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

我期待的HTML实现

<table border="1">
  <tr bgcolor="#9acd32">
    <th>Name Space Name</th>
    <th>Type Name</th>
    <th>Member Name</th>
    <th>MaintainabilityIndex</th>
    <th>CyclomaticComplexity</th>
    <th>ClassCoupling</th>
    <th>DepthOfInheritance</th>
    <th>LinesOfCode</th>

  </tr>
  <tr>
    <td>Name of namespace</td>
    <td>Type name</td>
    <td>Member name</td>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
  </tr>

</table>

在此先感谢您提供的任何帮助。

您给定的输入文档格式不正确。我不得不猜测它应该是什么。如果您的输入文档是....

<CodeMetricsReport Version="12.0">
    <Targets>
        <Target Name="E:\some-imaginary-path\some-imaginary-dll">
            <Modules>
                <Module Name="some-imaginary-dll" AssemblyVersion="1.0.0.0" FileVersion="1.0.0.0">
                    <Metrics>
                        <Metric Name="MaintainabilityIndex" Value="84"/>
                        <Metric Name="CyclomaticComplexity" Value="95"/>
                        <Metric Name="ClassCoupling" Value="109"/>
                        <Metric Name="DepthOfInheritance" Value="4"/>
                        <Metric Name="LinesOfCode" Value="214"/>
                    </Metrics>
                    <Namespaces>
                        <Namespace Name="LMS.SomeNamespace">
                            <Metrics>
                                <Metric Name="MaintainabilityIndex" Value="89"/>
                                <Metric Name="CyclomaticComplexity" Value="19"/>
                                <Metric Name="ClassCoupling" Value="18"/>
                                <Metric Name="DepthOfInheritance" Value="1"/>
                                <Metric Name="LinesOfCode" Value="29"/>
                            </Metrics>
                            <Types>
                                <Type Name="SomeType1">
                                    <Metrics>
                                        <Metric Name="MaintainabilityIndex" Value="100"/>
                                        <Metric Name="CyclomaticComplexity" Value="1"/>
                                        <Metric Name="ClassCoupling" Value="1"/>
                                        <Metric Name="DepthOfInheritance" Value="0"/>
                                        <Metric Name="LinesOfCode" Value="0"/>
                                    </Metrics>
                                    <Members>
                                        <Member Name="SomeMemberFor1">
                                            <Metrics>
                                                <Metric Name="MaintainabilityIndex" Value="100"/>
                                                <Metric Name="CyclomaticComplexity" Value="1"/>
                                                <Metric Name="ClassCoupling" Value="1"/>
                                                <Metric Name="LinesOfCode" Value="0"/>
                                            </Metrics>
                                        </Member>
                                    </Members>
                                </Type>
                                <Type Name="SomeType2">
                                    <Metrics>
                                        <Metric Name="MaintainabilityIndex" Value="76"/>
                                        <Metric Name="CyclomaticComplexity" Value="3"/>
                                        <Metric Name="ClassCoupling" Value="6"/>
                                        <Metric Name="DepthOfInheritance" Value="1"/>
                                        <Metric Name="LinesOfCode" Value="7"/>
                                    </Metrics>
                                    <Members>
                                        <Member Name="SomeMemberFor2" File="e:\some-file.cs" Line="27">
                                            <Metrics>
                                                <Metric Name="MaintainabilityIndex" Value="67"/>
                                                <Metric Name="CyclomaticComplexity" Value="2"/>
                                                <Metric Name="ClassCoupling" Value="5"/>
                                                <Metric Name="LinesOfCode" Value="6"/>
                                            </Metrics>
                                        </Member>
                                    </Members>
                                </Type>
                            </Types>
                        </Namespace>
                    </Namespaces>
                </Module>
            </Modules>
        </Target>
    </Targets>
</CodeMetricsReport>

...然后这个 XSLT 1 样式表...

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

<xsl:output method="html" version="5" doctype-system="" encoding="utf-8" />
<xsl:strip-space elements="*" />  

<xsl:template match="/">
  <html>
    <head>
      <title>metric.exe Output</title>
    </head>
    <body>
      <h1>metric.exe Ouput</h1>
      <table border="1">
        <thead>
          <tr bgcolor="#9acd32">
            <th>Name Space Name</th>
            <th>Type Name</th>
            <th>Member Name</th>
            <th>MaintainabilityIndex</th>
            <th>CyclomaticComplexity</th>
            <th>ClassCoupling</th>
            <th>DepthOfInheritance</th>
            <th>LinesOfCode</th>
          </tr>
        </thead>
        <tbody>
          <xsl:apply-templates select="CodeMetricsReport/Targets/Target/Modules/Module/Namespaces/Namespace/Types/Type/Members/Member" />
        </tbody>
       </table>
    </body>
  </html>
</xsl:template>

<xsl:template match="Member">
  <tr>
    <td><xsl:value-of select="../../../../@Name" /></td>
    <td><xsl:value-of select="../../@Name" /></td>
    <td><xsl:value-of select="@Name" /></td>
    <td><xsl:value-of select="Metrics/Metric[@Name='MaintainabilityIndex']/@Value" />&#160;</td>
    <td><xsl:value-of select="Metrics/Metric[@Name='CyclomaticComplexity']/@Value" />&#160;</td>
    <td><xsl:value-of select="Metrics/Metric[@Name='ClassCoupling']/@Value" />&#160;</td>
    <td><xsl:value-of select="../../Metrics/Metric[@Name='DepthOfInheritance']/@Value" />&#160;</td>
    <td><xsl:value-of select="Metrics/Metric[@Name='LinesOfCode']/@Value" />&#160;</td>
  </tr>  
</xsl:template>

</xsl:stylesheet>

... 会将输入文档转换成此输出文档...

<!DOCTYPE html SYSTEM "">
<html>
  <head>
    <META http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>metric.exe Output</title>
  </head>
  <body>
    <h1>metric.exe Ouput</h1>
    <table border="1">
      <thead>
        <tr bgcolor="#9acd32">
          <th>Name Space Name</th>
          <th>Type Name</th>
          <th>Member Name</th>
          <th>MaintainabilityIndex</th>
          <th>CyclomaticComplexity</th>
          <th>ClassCoupling</th>
          <th>DepthOfInheritance</th>
          <th>LinesOfCode</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>LMS.SomeNamespace</td>
          <td>SomeType1</td>
          <td>SomeMemberFor1</td>
          <td>100 </td>
          <td>1 </td>
          <td>1 </td>
          <td>0 </td>
          <td>0 </td>
        </tr>
        <tr>
          <td>LMS.SomeNamespace</td>
          <td>SomeType2</td>
          <td>SomeMemberFor2</td>
          <td>67 </td>
          <td>2 </td>
          <td>5 </td>
          <td>1 </td>
          <td>6 </td>
        </tr>
      </tbody>
    </table>
  </body>
</html>