使用XSLT过滤掉不需要的XML数据

时间:2014-10-08 12:50:21

标签: xml xslt

我有以下NUnit结果XML

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="C:\Selenium\TestResults\NewVersion.xsl"?>
<!--This file represents the results of running a test suite-->
<test-results name="C:\Selenium\VisualStudio\Automated Tests\Automated Tests\AutomatedTests\bin\Debug\AutomatedTests.dll" total="2" errors="1" failures="0" not-run="0" inconclusive="0" ignored="0" skipped="0" invalid="0" date="2014-10-06" time="11:54:11">
<environment nunit-version="2.6.3.13283" clr-version="2.0.50727.5485" os-version="Microsoft Windows NT 6.1.7601 Service Pack 1" platform="Win32NT" cwd="C:\Selenium" machine-name="U0130917-TPL-A" user="U0130917" user-domain="TEN" />
  <culture-info current-culture="en-GB" current-uiculture="en-US" />
 <test-suite type="Assembly" name="C:\Selenium\VisualStudio\Automated Tests\Automated Tests\AutomatedTests\bin\Debug\AutomatedTests.dll" executed="True" result="Failure" success="False" time="123.997" asserts="0">
   <results>
  <test-suite type="Namespace" name="AutomatedTests" executed="True" result="Failure" success="False" time="123.971" asserts="0">
    <results>
      <test-suite type="TestFixture" name="GridClientListPageVerificationGridTest" executed="True" result="Failure" success="False" time="123.913" asserts="0">
        <categories>
          <category name="GridSpitfireTests" />
        </categories>
        <results>
          <test-case name="AutomatedTests.GridClientListPageVerificationGridTest.ClientListPageVerificationGridTest_MainTest" executed="True" result="Error" success="False" time="123.269" asserts="33">
            <failure>
              <message><![CDATA[My MESSAGE ]]></message>
              <stack-trace>
                <![CDATA[Mystack trace]]>   
</stack-trace>
                </failure>
          </test-case>
        </results>
      </test-suite>
      <test-suite type="Namespace" name="PracticeCode" executed="True" result="Failure" success="False" time="0.017" asserts="0">
        <results>
          <test-suite type="Namespace" name="Basics" executed="True" result="Failure" success="False" time="0.016" asserts="0">
            <results>
              <test-suite type="TestFixture" name="PracticeCode_ArithmaticOperators" executed="True" result="Failure" success="False" time="0.016" asserts="0">
                <results>
                  <test-case name="AutomatedTests.PracticeCode.Basics.PracticeCode_ArithmaticOperators.PracticeCode_ArithmaticOperators_Main" executed="True" result="Success" success="True" time="0.015" asserts="0">                     
                    <failure>
                      <message>
                        <![CDATA[My MESSAGE]]>
                      </message>
                      <stack-trace>
                        <![CDATA[Mystack trace ]]>
                      </stack-trace>
                    </failure>
                  </test-case>
                </results>
              </test-suite>
            </results>
          </test-suite>
        </results>
      </test-suite>
      <test-suite type="Namespace" name="PracticeCode" executed="True" result="Success" success="True" time="0.013" asserts="0">
        <results>
          <test-suite type="Namespace" name="Basics" executed="True" result="Success" success="True" time="0.013" asserts="0">
            <results>
              <test-suite type="TestFixture" name="PracticeCode_ArithmaticOperators" executed="True" result="Success" success="True" time="0.012" asserts="0">
                <results>
                  <test-case name="AutomatedTests.PracticeCode.Basics.PracticeCode_ArithmaticOperators.PracticeCode_ArithmaticOperators_Main" executed="True" result="Success" success="True" time="0.011" asserts="0" />
                </results>
              </test-suite>
            </results>
          </test-suite>
        </results>
      </test-suite>
    </results>
  </test-suite>
</results>
</test-suite>
</test-results>

...并拥有以下样式表

 <xsl:template match="/">
<html>
  <body>
    <!--If failures is greater than 0, display a failure summary-->
    <xsl:if test="/test-results/@errors>0 or /test-results/@failures>0">

      <!--Header for Fail Summary-->
      <span style="font-size:medium;">
        <br/>
        <h3>Failure/Message Summary</h3>
      </span>
      <br/>

      <!--Table for Failed/Errored Test Cases-->
      <table border="2" cellpadding="5" cellspacing="0">
        <tr bgcolor="#9acd32">
          <!--Specify the headers-->
          <th style="text-align:center">Test Case Name</th>
          <th style="text-align:center">Message</th>
        </tr>
        <xsl:for-each select="//test-case">
          <!--Sort table by test case name-->
          <xsl:sort select="@name"/>
          <tr>
            <td style="text-align:left">
              <xsl:value-of select="@name"/>
            </td>
            <td style="text-align:left">
              <span style="font-size:large; font-weight:bold; color:red;">
                Message =
              </span>
              <xsl:value-of select="failure/message"/>
              <br/>
              <br/>
              <span style="font-size:large; font-weight:bold; color:red;">
                StackTrace =
              </span>
              <xsl:value-of select="failure/stack-trace"/>
            </td>
          </tr>
        </xsl:for-each>
      </table>
      <br/>
    </xsl:if>
  </body>
</html>
 </xsl:template>

我正在尝试围绕这些NUnit结果构建一个表,表中包含了失败的测试及其消息和堆栈跟踪,但是表中不包括未失败的测试。

目前上面的样式表构建了表格,但包括所有通过或失败的测试。

如果有任何帮助,我将不胜感激。请注意,我两天前开始学习XSL,因此最好采用简单的解决方案。

1 个答案:

答案 0 :(得分:0)

找到以下答案作为我的问题的解决方案...

在if语句中包装表行以评估是否成功&#39;是假的

 <!--Table for Failed/Errored Test Cases-->
      <table border="2" cellpadding="5" cellspacing="0">
        <tr bgcolor="#9acd32">
          <!--Specify the headers-->
          <th style="text-align:center">Test Case Name</th>
          <th style="text-align:center">Message</th>
        </tr>
        <!--Fore each test case, if Success = false, display the test name and message/stack trace-->         
        <xsl:for-each select="//test-case">
          <xsl:sort select="@name"/>
          <xsl:if test="./@success = 'False'">
          <!--Sort table by test case name-->        
          <tr>
            <td style="text-align:left">
              <xsl:value-of select="@name"/>
            </td>
            <td style="text-align:left">
              <span style="font-size:large; font-weight:bold; color:red;">
                Message =
              </span>
              <xsl:value-of select="failure/message"/>
              <br/>
              <br/>
              <span style="font-size:large; font-weight:bold; color:red;">
                StackTrace =
              </span>
              <xsl:value-of select="failure/stack-trace"/>
            </td>
          </tr>
          </xsl:if>
        </xsl:for-each>
      </table>
相关问题