0

嗨,我有一个 xml 和 xslt 代码。请任何人帮助我与html表格进行正确对齐。我尝试了以下方法,但没有得到正确的结果。在此我添加代码。

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="XSLTFile1.xslt"?>



 <culture-info current-culture="en-US" current-uiculture="en-US" />
  <test-suite type="Test Project" name="" executed="True" result="Failure" success="False" time="0.290" asserts="0">
 <results>
  <test-suite type="Assembly" name="D:\Samples\DemoNUnitTest\UnitTest\bin\Release\UnitTest.dll" executed="True" result="Failure" success="False" time="0.119" asserts="0">
    <results>
      <test-suite type="Namespace" name="UnitTest" executed="True" result="Failure" success="False" time="0.101" asserts="0">
        <results>
          <test-suite type="TestFixture" name="DemoUnitTest" executed="True" result="Success" success="True" time="0.075" asserts="0">
            <results>
              <test-case name="UnitTest.DemoUnitTest.ShouldNotValidNumber" executed="True" result="Success" success="True" time="0.054" asserts="1" />
              <test-case name="UnitTest.DemoUnitTest.ShouldValidNumber" executed="True" result="Success" success="True" time="0.000" asserts="1" />
            </results>
          </test-suite>
          <test-suite type="TestFixture" name="NumberValidationTest" executed="True" result="Failure" success="False" time="0.019" asserts="0">
            <results>
              <test-case name="UnitTest.NumberValidationTest.ShouldNotValidNumber" executed="True" result="Failure" success="False" time="0.014" asserts="1">
                <failure>
                  <message>
                   <![CDATA[  Expected: True  But was:False ]]> 
                  </message>
          <stack-trace>
            <! CDATA[at UnitTest.NumberValidationTest.ShouldNotValidNumber() in D:\Samples\DemoNUnitTest \UnitTest\Class1.cs:line 24 ]]> 
            </stack-trace>
                </failure>
              </test-case>
              <test-case name="UnitTest.NumberValidationTest.ShouldValidNumber" executed="True" result="Success" success="True" time="0.001" asserts="1" />
             </results>
           </test-suite>
         </results>
         </test-suite>
          </results>
          </test-suite>
         <test-suite type="Assembly" name="D:\Samples\UnitTest.dll" executed="True" result="Failure" success="False" time="0.106" asserts="0">
     <results>
      <test-suite type="Namespace" name="UnitTest" executed="True" result="Failure" success="False" time="0.089" asserts="0">
        <results>
          <test-suite type="TestFixture" name="DemoUnitTest" executed="True" result="Success" success="True" time="0.073" asserts="0">
            <results>
              <test-case name="UnitTest.DemoUnitTest.ShouldNotValidNumber" executed="True" result="Success" success="True" time="0.058" asserts="1" />
              <test-case name="UnitTest.DemoUnitTest.ShouldValidNumber" executed="True" result="Success" success="True" time="0.001" asserts="1" />
            </results>
          </test-suite>
          <test-suite type="TestFixture" name="NumberValidationTest" executed="True" result="Failure" success="False" time="0.011" asserts="0">
            <results>
              <test-case name="UnitTest.NumberValidationTest.ShouldNotValidNumber" executed="True" result="Failure" success="False" time="0.007" asserts="1">
                <failure>
                  <message><![CDATA[  Expected: True  But was:  False]]></message>
                  <stack-trace>
                       <![CDATA[at UnitTest.NumberValidationTest.ShouldNotValidNumber()
                     ]]>
                  </stack-trace>
                </failure>
              </test-case>
              <test-case name="UnitTest.NumberValidationTest.ShouldValidNumber" executed="True" result="Success" success="True" time="0.000" asserts="1" />
            </results>
          </test-suite>
          </results>
          </test-suite>
          </results>
        </test-suite>
         </results>
           </test-suite>
       </test-results>

这是我的 XSL 代码

 <?xml version="1.0" encoding="ISO-8859-1"?>
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html"/>
 <xsl:template match="/">
 <html>
  <table border="1">
    <!-- The tabel header -->
    <tr>
      <th>Assembly</th>


    </tr>
    <!-- selecting the assamblys -->
    <xsl:call-template name="cal"/>
  </table>
  </html>
  </xsl:template>
  <!-- template for the creation of tabel rows -->
   <xsl:template match="//test-suite[@type='Assembly']" name="cal" >
  <xsl:for-each select="//test-suite[@type='Assembly']">
  <tr>
  <td>
    <xsl:value-of select="@name"/>
  </td>
  <td colspan="0">
    <table>
      <tr>
        <th>TestFixture</th>
      </tr>
      <xsl:for-each select=".//test-suite[@type='TestFixture']">
        <tr>
          <td colspan="0" >
            <xsl:value-of select="@name"/>
          </td>
          <td >
            <table>
              <tr>
                <th>Test Cases</th>
                <th>Result</th>
                <th>Message</th>
              </tr>
              <xsl:for-each select=".//test-case">
                <tr>
                  <td colspan="0">
                    <xsl:value-of select="@name"/>
                  </td>
                  <xsl:choose >
                    <xsl:when test="@result='Success'">
                      <td colspan="0">
                        <xsl:value-of select="@result"/>
                      </td>
                    </xsl:when>
                    <xsl:otherwise >
                      <td bgcolor="red" >
                        <xsl:value-of select="@result"/>
                      </td>
                      <td bgcolor="red">
                        <xsl:value-of select=".//message"/>
                      </td>
                    </xsl:otherwise>

                  </xsl:choose>
                </tr>
              </xsl:for-each>
            </table>
          </td>
        </tr>
      </xsl:for-each>
    </table>
    </td>
    </tr>
    </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>

我的结果将如下所示: 结果图像

4

1 回答 1

1

以下转换应该可以解决问题。

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>
    <xsl:template match="/">
<html>
    <head>
        <title></title>
    </head>
    <body>
        <table>
            <thead>
                <tr>
                    <th>Assemly</th>
                    <th>Test Fixture</th>
                    <th>Test Case</th>
                    <th>Result</th>
                    <th>Message</th>
                </tr>
            </thead>
            <tbody><xsl:for-each select="//test-case">
                <tr>
                    <td><xsl:if test="count(ancestor::test-suite[@type='TestFixture']/preceding-sibling::test-suite[@type='TestFixture'])=0 and count(preceding-sibling::test-case)=0">
                        <xsl:value-of select="ancestor::test-suite[@type='Assembly']/@name" />
                    </xsl:if></td>
                    <td><xsl:if test="count(preceding-sibling::test-case)=0">
                        <xsl:value-of select="ancestor::test-suite[@type='TestFixture']/@name" />
                    </xsl:if></td>
                    <td><xsl:value-of select="@name"/></td>
                    <xsl:choose>
                    <xsl:when test="@result='Success'">
                        <td><xsl:value-of select="@result"/></td>
                        <td></td>
                    </xsl:when>
                    <xsl:otherwise>
                        <td bgcolor="red"><xsl:value-of select="@result"/></td>
                        <td bgcolor="red"><xsl:value-of select=".//message"/></td>
                    </xsl:otherwise>
                    </xsl:choose>
                </tr>
            </xsl:for-each></tbody>
        </table>
    </body>
</html>
    </xsl:template>
</xsl:stylesheet>
于 2013-10-11T23:21:39.203 回答