2013-10-14 25 views
0

我對XSLT沒有經驗。我嘗試了幾個在線教程,其中包括W3C的教程,但沒有成功。我試圖從Visual Studio Code Metrics命令行工具創建的XML中生成一個HTML頁面。該XML的樣子:協助使用XSLT?

<?xml version="1.0" encoding="utf-8"?> 
<CodeMetricsReport Version="10.0"> 
<Targets> 
<Target Name="C:\Users\rs02130\Documents\Visual Studio 2010\Projects\TestVS10XML\TestVS10XML\Bin\Debug\TestVS10XML.dll"> 
    <Modules> 
     <Module Name="TestVS10XML.dll" AssemblyVersion="1.0.0.0" FileVersion="1.0.0.0"> 
      <Metrics> 
      <Metric Name="MaintainabilityIndex" Value="97" /> 
      <Metric Name="CyclomaticComplexity" Value="10" /> 
      <Metric Name="ClassCoupling" Value="8" /> 
      <Metric Name="DepthOfInheritance" Value="5" /> 
      <Metric Name="LinesOfCode" Value="4" /> 
      </Metrics> 
      <Namespaces> 
      <Namespace Name="TestVS10XML"> 
       <Metrics> 
       <Metric Name="MaintainabilityIndex" Value="97" /> 
       <Metric Name="CyclomaticComplexity" Value="10" /> 
       <Metric Name="ClassCoupling" Value="8" /> 
       <Metric Name="DepthOfInheritance" Value="5" /> 
       <Metric Name="LinesOfCode" Value="4" /> 
       </Metrics> 
       <Types> 
       <Type Name="_Default"> 
        <Metrics> 
        <Metric Name="MaintainabilityIndex" Value="91" /> 
        <Metric Name="CyclomaticComplexity" Value="2" /> 
        <Metric Name="ClassCoupling" Value="5" /> 
        <Metric Name="DepthOfInheritance" Value="4" /> 
        <Metric Name="LinesOfCode" Value="2" /> 
        </Metrics> 
        <Members> 
        <Member Name="Page_Load(object, EventArgs) : void" File="C:\Users\rs02130\Documents\Visual Studio 2010\Projects\TestVS10XML\TestVS10XML\Default.aspx.cs" Line="14"> 
         <Metrics> 
         <Metric Name="MaintainabilityIndex" Value="90" /> 
         <Metric Name="CyclomaticComplexity" Value="1" /> 
         <Metric Name="ClassCoupling" Value="4" /> 
         <Metric Name="LinesOfCode" Value="1" /> 
         </Metrics> 
        </Member> 
        <Member Name="_Default()"> 
         <Metrics> 
         <Metric Name="MaintainabilityIndex" Value="100" /> 
         <Metric Name="CyclomaticComplexity" Value="1" /> 
         <Metric Name="ClassCoupling" Value="1" /> 
         <Metric Name="LinesOfCode" Value="1" /> 
         </Metrics> 
        </Member> 
        </Members> 
       </Type> 
       <Type Name="SiteMaster"> 
        <Metrics> 
        <Metric Name="MaintainabilityIndex" Value="100" /> 
        <Metric Name="CyclomaticComplexity" Value="2" /> 
        <Metric Name="ClassCoupling" Value="3" /> 
        <Metric Name="DepthOfInheritance" Value="5" /> 
        <Metric Name="LinesOfCode" Value="1" /> 
        </Metrics> 
        <Members> 
        <Member Name="Page_Load(object, EventArgs) : void" File="C:\Users\rs02130\Documents\Visual Studio 2010\Projects\TestVS10XML\TestVS10XML\Site.Master.cs" Line="13"> 
         <Metrics> 
         <Metric Name="MaintainabilityIndex" Value="100" /> 
         <Metric Name="CyclomaticComplexity" Value="1" /> 
         <Metric Name="ClassCoupling" Value="1" /> 
         <Metric Name="LinesOfCode" Value="0" /> 
         </Metrics> 
        </Member> 
        <Member Name="SiteMaster()"> 
         <Metrics> 
         <Metric Name="MaintainabilityIndex" Value="100" /> 
         <Metric Name="CyclomaticComplexity" Value="1" /> 
         <Metric Name="ClassCoupling" Value="1" /> 
         <Metric Name="LinesOfCode" Value="1" /> 
         </Metrics> 
        </Member> 
        </Members> 
       </Type> 
       <Type Name="Global"> 
        <Metrics> 
        <Metric Name="MaintainabilityIndex" Value="100" /> 
        <Metric Name="CyclomaticComplexity" Value="6" /> 
        <Metric Name="ClassCoupling" Value="2" /> 
        <Metric Name="DepthOfInheritance" Value="2" /> 
        <Metric Name="LinesOfCode" Value="1" /> 
        </Metrics> 
        <Members> 
        <Member Name="Application_Start(object, EventArgs) : void" File="C:\Users\rs02130\Documents\Visual Studio 2010\Projects\TestVS10XML\TestVS10XML\Global.asax.cs" Line="14"> 
         <Metrics> 
         <Metric Name="MaintainabilityIndex" Value="100" /> 
         <Metric Name="CyclomaticComplexity" Value="1" /> 
         <Metric Name="ClassCoupling" Value="1" /> 
         <Metric Name="LinesOfCode" Value="0" /> 
         </Metrics> 
        </Member> 
        <Member Name="Application_End(object, EventArgs) : void" File="C:\Users\rs02130\Documents\Visual Studio 2010\Projects\TestVS10XML\TestVS10XML\Global.asax.cs" Line="20"> 
         <Metrics> 
         <Metric Name="MaintainabilityIndex" Value="100" /> 
         <Metric Name="CyclomaticComplexity" Value="1" /> 
         <Metric Name="ClassCoupling" Value="1" /> 
         <Metric Name="LinesOfCode" Value="0" /> 
         </Metrics> 
        </Member> 
        <Member Name="Application_Error(object, EventArgs) : void" File="C:\Users\rs02130\Documents\Visual Studio 2010\Projects\TestVS10XML\TestVS10XML\Global.asax.cs" Line="26"> 
         <Metrics> 
         <Metric Name="MaintainabilityIndex" Value="100" /> 
         <Metric Name="CyclomaticComplexity" Value="1" /> 
         <Metric Name="ClassCoupling" Value="1" /> 
         <Metric Name="LinesOfCode" Value="0" /> 
         </Metrics> 
        </Member> 
        <Member Name="Session_Start(object, EventArgs) : void" File="C:\Users\rs02130\Documents\Visual Studio 2010\Projects\TestVS10XML\TestVS10XML\Global.asax.cs" Line="32"> 
         <Metrics> 
         <Metric Name="MaintainabilityIndex" Value="100" /> 
         <Metric Name="CyclomaticComplexity" Value="1" /> 
         <Metric Name="ClassCoupling" Value="1" /> 
         <Metric Name="LinesOfCode" Value="0" /> 
         </Metrics> 
        </Member> 
        <Member Name="Session_End(object, EventArgs) : void" File="C:\Users\rs02130\Documents\Visual Studio 2010\Projects\TestVS10XML\TestVS10XML\Global.asax.cs" Line="38"> 
         <Metrics> 
         <Metric Name="MaintainabilityIndex" Value="100" /> 
         <Metric Name="CyclomaticComplexity" Value="1" /> 
         <Metric Name="ClassCoupling" Value="1" /> 
         <Metric Name="LinesOfCode" Value="0" /> 
         </Metrics> 
        </Member> 
        <Member Name="Global()"> 
         <Metrics> 
         <Metric Name="MaintainabilityIndex" Value="100" /> 
         <Metric Name="CyclomaticComplexity" Value="1" /> 
         <Metric Name="ClassCoupling" Value="1" /> 
         <Metric Name="LinesOfCode" Value="1" /> 
         </Metrics> 
        </Member> 
        </Members> 
       </Type> 
       </Types> 
      </Namespace> 
      </Namespaces> 
     </Module> 
     </Modules> 
    </Target> 
    </Targets> 
</CodeMetricsReport> 

我只希望報告中包含的頂級指標:

<Module Name="TestVS10XML.dll" AssemblyVersion="1.0.0.0" FileVersion="1.0.0.0"> 
    <Metrics> 
    <Metric Name="MaintainabilityIndex" Value="97" /> 
    <Metric Name="CyclomaticComplexity" Value="10" /> 
    <Metric Name="ClassCoupling" Value="8" /> 
    <Metric Name="DepthOfInheritance" Value="5" /> 
    <Metric Name="LinesOfCode" Value="4" /> 
    </Metrics> 

並非所有它下面的命名空間信息。我想出了這個XSLT(不包括我真的很喜歡的Module Name),但是當我運行它時,我只能得到列標題,表中沒有數據。任何幫助,不勝感激...

XSLT:

<?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="html" indent="yes"/> 
    <xsl:template match="/"> 
    <table width="100%" align="center" cellpadding="0" cellspacing="0" border="1" 
     style="font-family:verdana;font-size:10pt;border:1"> 
<tr> 
    <td width="20%" align="left" > 
    Maintainability Index 
    </td> 
    <td width="20%" align="left"> 
    Cyclomatic Complexity 
    </td> 
    <td width="20%" align="left"> 
    Class Coupling 
    </td> 
    <td width="20%" align="left"> 
    Depth Of Inheritance 
    </td> 
    <td width="20%" align="left"> 
    Lines Of Code 
    </td> 
</tr> 
    </table> 
<xsl:for-each select="Targets/Modules/Metrics"> 
<table width="100%" align="center" cellpadding="0" cellspacing="0" border="1" 
    style="font-family:verdana;font-size:10pt;border:1"> 
    <tr > 
     <td width="20%" align="left" > 
     <xsl:value-of select="MaintainabilityIndex"/> 
    </td> 
    <td width="20%" align="left" > 
      <xsl:value-of select="CyclomaticComplexity"/> 
    </td> 
    <td width="20%" align="left" > 
     <xsl:value-of select="ClassCoupling"/> 
    </td> 
    <td width="20%" align="left" > 
     <xsl:value-of select="DepthOfInheritance"/> 
    </td> 
    <td width="20%" align="left" > 
     <xsl:value-of select="LinesOfCode"/> 
    </td> 
    </tr> 
</table> 
</xsl:for-each> 
</xsl:template> 
</xsl:stylesheet> 
+1

我不相信這是在W3C的在線教程。如果您指的是w3schools,請注意,這與W3C沒有任何關係。一般來說,我認爲試圖從這種教程中學習XSLT並不是一個好方法。給自己一本正確解釋概念的好書。 –

回答

0
<!-- you don't use the msxsl namespace, you could drop its declaration --> 
<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="CodeMetricsReport"> 
    <!-- you really ought to do all this styling in CSS --> 
    <table width="100%" align="center" cellpadding="0" cellspacing="0" border="1" 
     style="font-family:verdana;font-size:10pt;border:1"> 
     <tr> 
     <td width="20%" align="left">Maintainability Index</td> 
     <td width="20%" align="left">Cyclomatic Complexity</td> 
     <td width="20%" align="left">Class Coupling</td> 
     <td width="20%" align="left">Depth Of Inheritance</td> 
     <td width="20%" align="left">Lines Of Code</td> 
     </tr> 

     <!-- that's what you forgot --> 
     <xsl:apply-templates select="Targets/Target/Modules/Module/Metrics" /> 
    </table> 
    </xsl:template> 

    <xsl:template match="Metrics"> 
    <tr> 
     <td width="20%" align="left"> 
     <xsl:value-of select="Metric[@Name = 'MaintainabilityIndex']/@Value" /> 
     </td> 
     <td width="20%" align="left"> 
     <xsl:value-of select="Metric[@Name = 'CyclomaticComplexity']/@Value" /> 
     </td> 
     <td width="20%" align="left"> 
     <xsl:value-of select="Metric[@Name = 'ClassCoupling']/@Value" /> 
     </td> 
     <td width="20%" align="left"> 
     <xsl:value-of select="Metric[@Name = 'DepthOfInheritance']/@Value" /> 
     </td> 
     <td width="20%" align="left"> 
     <xsl:value-of select="Metric[@Name = 'LinesOfCode']/@Value" /> 
     </td> 
    </tr> 
    </xsl:template> 
</xsl:stylesheet> 
+0

如果我的目的是要學習XSLT的長期使用,我會得到好幾本好書。作爲另一部分工作的一部分,這種剛剛跳出我身邊。我結束了一個XML文件,並被要求把它變成一個HTML頁面。從未這樣做過... –

+0

感謝您的幫助@Michael Kay。如果我可以再問一件事。有沒有辦法在表格上方插入的「名稱」值,或者作爲表格的第一行,可以給我所需的所有東西。如果他們回來並要求更多(即命名空間),那麼我會去拿一兩本書。再次感謝。 –

+0

呃...我不是邁克爾凱。當然你想問他? – Tomalak