2013-10-10 23 views
0

我需要使用xslt將其轉換爲html表格。任何人都可以幫助我做到這一點。如何在使用xslt時給出根選擇路徑?

<?xml version="1.0" encoding="utf-8" standalone="no"?> 

    <test-results name="D:\Samples\DemoNUnitTest\UnitTest\bin\Release\UnitTest.dll" total="7" errors="0" failures="1" not-run="0" inconclusive="0" ignored="0" skipped="0" invalid="0" date="2013-10-09" time="17:17:32"> 

<culture-info current-culture="en-US" current-uiculture="en-US" /> 
<test-suite type="Test Project" name="" executed="True" result="Failure" success="False" time="1.261" asserts="0"> 
<results> 
    <test-suite type="Assembly" name="D:\Samples\DemoNUnitTest\UnitTest\bin\Release\UnitTest.dll" executed="True" result="Failure" success="False" time="0.118" asserts="0"> 
    <results> 

     <test-suite type="Namespace" name="UnitTest" executed="True" result="Failure" success="False" time="0.099" 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.053" 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.018" asserts="0"> 
      <results> 
       <test-case name="UnitTest.NumberValidationTest.ShouldNotValidNumber" executed="True" result="Failure" success="False" time="0.013" 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\DemoUnitTest\DemoUnitTest.Tests\bin\Release\DemoUnitTest.Tests.dll" executed="True" result="Success" success="True" time="1.074" asserts="0"> 
    <results> 
     <test-suite type="Namespace" name="DemoUnitTest" executed="True" result="Success" success="True" time="1.056" asserts="0"> 
     <results> 
      <test-suite type="Namespace" name="Tests" executed="True" result="Success" success="True" time="1.056" asserts="0"> 
      <results> 
       <test-suite type="Namespace" name="Controllers" executed="True" result="Success" success="True" time="1.055" asserts="0"> 
       <results> 
        <test-suite type="TestFixture" name="HomeControllerTest" executed="True" result="Success" success="True" time="1.052" asserts="0"> 
        <results> 
         <test-case name="DemoUnitTest.Tests.Controllers.HomeControllerTest.About" executed="True" result="Success" success="True" time="0.735" asserts="1" /> 
         <test-case name="DemoUnitTest.Tests.Controllers.HomeControllerTest.Contact" executed="True" result="Success" success="True" time="0.003" asserts="1" /> 
         <test-case name="DemoUnitTest.Tests.Controllers.HomeControllerTest.Index" executed="True" result="Success" success="True" time="0.299" asserts="1" /> 
        </results> 
        </test-suite> 
       </results> 
       </test-suite> 
      </results> 
      </test-suite> 
     </results> 
     </test-suite> 
    </results> 
    </test-suite> 
</results> 

從上面的XML代碼,我需要把它格式化爲像第一列大會列表中的表,在相應的測試夾具名下一列。另外我添加下面的XSLT代碼,我已經嘗試過了。

<?xml version="1.0" encoding="ISO-8859-1"?> 

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
    <html> 
    <table border="1"> 
    <!-- The tabel header --> 
    <tr> 
     <th>Assembly</th> 
     <th>TestFixture</th> 
    </tr> 
    <!-- selecting the assamblys --> 
    <xsl:apply-templates select="//test-suite[@type='Assembly']"/> 
    </table> 
    </html> 
    </xsl:template> 
    <!-- template for the creation of tabel rows --> 
    <xsl:template match="test-suite[@type='Assembly']"> 
    <tr> 
    <td> 
    <xsl:value-of select="@name"/> 
    </td> 
    <td> 
    <table> 
     <xsl:for-each select="//test-suite[@type='TestFixture']"> 
     <tr> 
     <td> 
      <xsl:value-of select="@name"/> 
     </td> 
     </tr> 
     </xsl:for-each> 
    </table> 
    </td> 
    </tr> 
    </xsl:template> 
    </xsl:stylesheet> 
+1

請出示你的企圖至今。 StackOverflow並不是一個平臺,您可以發佈任務並讓其他人爲您編寫代碼。 – Tomalak

回答

1

你可以做這樣的事情:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="html" version="4.0" encoding="utf-8" indent="yes" omit-xml-  declaration="yes"/> 
    <xsl:template match="/"> 
     <html> 
      <table border="1"> 
       <!-- The tabel header --> 
       <tr> 
        <th>Assembly</th> 
        <th>TestFixture</th> 
       </tr> 
       <!-- selecting the assamblys --> 
       <xsl:apply-templates select="//test-suite[@type='Assembly']"/> 
      </table> 
     </html> 
    </xsl:template> 
    <!-- template for the creation of tabel rows --> 
    <xsl:template match="test-suite[@type='Assembly']"> 
     <tr> 
      <td>here you put the data for the first column</td> 
      <td>second column</td> 
     </tr> 
    </xsl:template> 
</xsl:stylesheet> 
+0

感謝您的回覆。在添加第二列的同時,另一個程序集中的一個程序集的樹結構也不相同。爲此我不能用選擇的根路徑寫for-each語句。請深入瞭解我的XML代碼。 –

+0

寫'.// test-suite [@ type ='TestFixture']'而不是'// test-suite [@ type ='TestFixture']'沒有搜索整個xml的點 – sst103

+0

感謝哥們。工作好:) –