如何在使用xslt时给出根选择路径?

时间:2013-10-10 07:24:48

标签: xml xslt

我需要使用xslt将其转换为html表。任何人帮助我这样做。

<?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代码中,我需要将其格式化为类似于第一列Assembly列表的表,在下一列中对应的Test-fixture名称。另外我在下面添加了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 个答案:

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