2015-09-03 41 views
0

我試圖轉換下列由metric.exe生成的用於我的解決方案中的程序集的XML。而且我不是XSLT的專家,並且努力達到問題底部所示的格式。請幫忙。通過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婷achive

<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>

預先感謝作出的任何幫助。

+0

沒有很好地形成你的輸入文檔。你能糾正嗎? –

回答

1

您提供的輸入文檔格式不正確。我不得不猜測它應該是什麼。如果輸入文檔是....

<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>

+0

謝謝我已經嘗試了最後幾個小時,並設法做到了(儘管我的方法不同),但我會嘗試你的並接受你的答案。 – Ahsan