协助XSLT?

时间:2013-10-14 13:53:38

标签: xml xslt

我没有使用XSLT的经验。我尝试了几个在线教程,包括W3C的一个,但没有成功。我正在尝试从Visual Studio Code Metrics命令行工具创建的XML生成HTML页面。 XML看起来像:

<?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(不包括我也非常喜欢的模块名称)但是当我运行它时我只得到列标题,表中没有数据。非常感谢任何帮助...

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 个答案:

答案 0 :(得分: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>