Xslt 使用xsl根据mstest报告中的类对测试进行分组

Xslt 使用xsl根据mstest报告中的类对测试进行分组,xslt,Xslt,我正在为mstest的trx文件编写xslt。 除了知道整个课程的结果外,我还想知道每门课的成功和失败次数。 我已经尝试了很多方法,但我无法获得某个特定课程的结果。 这就是xml的外观 (编辑) 很抱歉输入了大量代码。但我实际上是在使用javascript在单击特定类时向下滑动测试名称。所以我需要这么多的模板。我是否在代码中遗漏了某些内容。不知道您想要的确切输出,以下样式表符合所述条件: <xsl:stylesheet version="2.0" xmlns:xsl="http://www

我正在为mstest的trx文件编写xslt。
除了知道整个课程的结果外,我还想知道每门课的成功和失败次数。
我已经尝试了很多方法,但我无法获得某个特定课程的结果。
这就是xml的外观

(编辑)


很抱歉输入了大量代码。但我实际上是在使用javascript在单击特定类时向下滑动测试名称。所以我需要这么多的模板。我是否在代码中遗漏了某些内容。

不知道您想要的确切输出,以下样式表符合所述条件:

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tt="http://microsoft.com/schemas/VisualStudio/TeamTest/2006">
<xsl:output method="xml" indent="yes" />
    <xsl:template match="/">
        <tests>
            <xsl:apply-templates select="//tt:UnitTest"/>
        </tests>
    </xsl:template>

    <xsl:template match="tt:UnitTest">
        <xsl:variable name="id" select="@id" />
        <test>
            <className>
                <xsl:value-of select="tt:TestMethod/@className"/>
            </className>
            <outcome>
                <xsl:value-of select="//tt:UnitTestResult[@testId=$id]/@outcome"/>
            </outcome>
        </test>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

并生成以下示例输出:

<?xml version="1.0" encoding="UTF-8"?>
<tests xmlns:tt="http://microsoft.com/schemas/VisualStudio/TeamTest/2006">
    <test>
        <className>TestProject1.Test.LogonInfoTest, TestProject1.Test</className>
        <outcome>Passed</outcome>
    </test>
    <test>
        <className>TestProject1.Test.LogonInfoTest, TestProject1.Test</className>
        <outcome>Passed</outcome>
    </test>
    <test>
        <className>TestProject1.Test.LogonInfoTest, TestProject1.Test</className>
        <outcome>Passed</outcome>
    </test>
</tests>

TestProject1.Test.LogonInfoTest,TestProject1.Test
通过
TestProject1.Test.LogonInfoTest,TestProject1.Test
通过
TestProject1.Test.LogonInfoTest,TestProject1.Test
通过
此样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:output indent="yes"/>
    <xsl:key name="class" match="TestMethod" use="@className"/>
    <xsl:key name="result" match="UnitTestResult" use="@testName"/>

    <xsl:template match="/">
    <table>
      <tr>
         <td>Test Name</td>
         <td>Result</td>
         <td>Duration</td>
         <td>Passed</td>
         <td>Failed</td>
         <td>Inconclusive</td>
      </tr>
      <xsl:apply-templates/>
    </table>
    </xsl:template>

    <xsl:template match="TestMethod[count(.|key('class',@className)[1])=1]">
      <xsl:variable name="result" select="key('result',key('class',@className)/@name)"/>
      <tr>
        <td colspan="3"><xsl:value-of select="@className"/></td>
        <td><xsl:value-of select="count($result[@outcome='Passed'])"/></td>
        <td><xsl:value-of select="count($result[@outcome='Failed'])"/></td>
        <td><xsl:value-of select="count($result[@outcome='Inconclusive'])"/></td>
      </tr>
      <xsl:apply-templates select="key('class',@className)" mode="sub"/>
    </xsl:template>

    <xsl:template match="TestMethod" mode="sub">
      <tr>
        <td><xsl:value-of select="@name"/></td>
        <td><xsl:value-of select="key('result',@name)/@outcome"/></td>
        <td>Not in sample</td>
      </tr>
    </xsl:template> 

</xsl:stylesheet> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:test="http://microsoft.com/schemas/VisualStudio/TeamTest/2006" exclude-result-prefixes="test">
    <xsl:output indent="yes"/>
    <xsl:key name="class" match="test:TestMethod" use="@className"/>
    <xsl:key name="result" match="test:UnitTestResult" use="@testName"/>
    <xsl:template match="text()"/>
    <xsl:template match="/">
        <table>
            <tr>
                <td>Test Name</td>
                <td>Result</td>
                <td>Duration</td>
                <td>Passed</td>
                <td>Failed</td>
                <td>Inconclusive</td>
            </tr>
            <xsl:apply-templates/>
        </table>
    </xsl:template>
    <xsl:template match="test:TestMethod[count(.|key('class',@className)[1])=1]">
        <xsl:variable name="result" select="key('result',key('class',@className)/@name)"/>
        <tr>
            <td colspan="3">
                <xsl:value-of select="@className"/>
            </td>
            <td>
                <xsl:value-of select="count($result[@outcome='Passed'])"/>
            </td>
            <td>
                <xsl:value-of select="count($result[@outcome='Failed'])"/>
            </td>
            <td>
                <xsl:value-of select="count($result[@outcome='Inconclusive'])"/>
            </td>
        </tr>
        <xsl:apply-templates select="key('class',@className)" mode="sub"/>
    </xsl:template>
    <xsl:template match="test:TestMethod" mode="sub">
        <tr>
            <td>
                <xsl:value-of select="@name"/>
            </td>
            <td>
                <xsl:value-of select="key('result',@name)/@outcome"/>
            </td>
            <td>
                <xsl:value-of select="key('result',@name)/@duration"/>
            </td>
        </tr>
    </xsl:template>
</xsl:stylesheet>
你需要一些小的修改。使用此样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:output indent="yes"/>
    <xsl:key name="class" match="TestMethod" use="@className"/>
    <xsl:key name="result" match="UnitTestResult" use="@testName"/>

    <xsl:template match="/">
    <table>
      <tr>
         <td>Test Name</td>
         <td>Result</td>
         <td>Duration</td>
         <td>Passed</td>
         <td>Failed</td>
         <td>Inconclusive</td>
      </tr>
      <xsl:apply-templates/>
    </table>
    </xsl:template>

    <xsl:template match="TestMethod[count(.|key('class',@className)[1])=1]">
      <xsl:variable name="result" select="key('result',key('class',@className)/@name)"/>
      <tr>
        <td colspan="3"><xsl:value-of select="@className"/></td>
        <td><xsl:value-of select="count($result[@outcome='Passed'])"/></td>
        <td><xsl:value-of select="count($result[@outcome='Failed'])"/></td>
        <td><xsl:value-of select="count($result[@outcome='Inconclusive'])"/></td>
      </tr>
      <xsl:apply-templates select="key('class',@className)" mode="sub"/>
    </xsl:template>

    <xsl:template match="TestMethod" mode="sub">
      <tr>
        <td><xsl:value-of select="@name"/></td>
        <td><xsl:value-of select="key('result',@name)/@outcome"/></td>
        <td>Not in sample</td>
      </tr>
    </xsl:template> 

</xsl:stylesheet> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:test="http://microsoft.com/schemas/VisualStudio/TeamTest/2006" exclude-result-prefixes="test">
    <xsl:output indent="yes"/>
    <xsl:key name="class" match="test:TestMethod" use="@className"/>
    <xsl:key name="result" match="test:UnitTestResult" use="@testName"/>
    <xsl:template match="text()"/>
    <xsl:template match="/">
        <table>
            <tr>
                <td>Test Name</td>
                <td>Result</td>
                <td>Duration</td>
                <td>Passed</td>
                <td>Failed</td>
                <td>Inconclusive</td>
            </tr>
            <xsl:apply-templates/>
        </table>
    </xsl:template>
    <xsl:template match="test:TestMethod[count(.|key('class',@className)[1])=1]">
        <xsl:variable name="result" select="key('result',key('class',@className)/@name)"/>
        <tr>
            <td colspan="3">
                <xsl:value-of select="@className"/>
            </td>
            <td>
                <xsl:value-of select="count($result[@outcome='Passed'])"/>
            </td>
            <td>
                <xsl:value-of select="count($result[@outcome='Failed'])"/>
            </td>
            <td>
                <xsl:value-of select="count($result[@outcome='Inconclusive'])"/>
            </td>
        </tr>
        <xsl:apply-templates select="key('class',@className)" mode="sub"/>
    </xsl:template>
    <xsl:template match="test:TestMethod" mode="sub">
        <tr>
            <td>
                <xsl:value-of select="@name"/>
            </td>
            <td>
                <xsl:value-of select="key('result',@name)/@outcome"/>
            </td>
            <td>
                <xsl:value-of select="key('result',@name)/@duration"/>
            </td>
        </tr>
    </xsl:template>
</xsl:stylesheet>

测试名称
结果
期间
通过
失败
无定论的
您将得到以下结果:

<table>
<tr>
<td>Test Name</td>
<td>Result</td>
<td>Duration</td>
<td>Passed</td>
<td>Failed</td>
<td>Inconclusive</td>
</tr>
<tr>
<td colspan="3">TestProject1.Test.LogonInfoTest, TestProject1.Test</td>
<td>3</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>EmptyUserIdInConstructor</td>
<td>Passed</td>
<td>00:00:00.0005633</td>
</tr>
<tr>
<td>NullUserIdInConstructor</td>
<td>Passed</td>
<td>00:00:00.0047344</td>
</tr>
<tr>
<td>LogonInfoConstructorTest</td>
<td>Passed</td>
<td>00:00:00.0234997</td>
</tr>
</table>

测试名称
结果
期间
通过
失败
无定论的
TestProject1.Test.LogonInfoTest,TestProject1.Test
3.
0
0
空序列构造函数
通过
00:00:00.0005633
NullUserIdin构造函数
通过
00:00:00.0047344
LogonInfoConstructorTest
通过
00:00:00.0234997

注意:在模式中使用名称空间(第一个输入文档有默认名称空间,但第二个示例没有)。我在您的第一次输入中找到了您的持续时间数据。您必须过滤文本节点(第一次输入中没有文本节点)

如果您真的希望有人回复,您需要提供一个非常简单的示例(不超过20行)并提供所需的输出。通过阅读您的说明,我可以想象,使用键可以轻松完成此任务(因为这里有交叉引用)如果您能提供一个输入文档结构的小示例(例如,没有此转换不需要的属性)和一个输出文档的示例,我们可以给您一个完整样式表的示例。我用一个示例输入和一个示例输出编辑了这个问题。我试过用钥匙。但也许我用错了。非常感谢你的帮助。非常感谢。我无法调试一个不完整的样式表…另外请注意,我的样式表会产生您发布的所需结果。一个包含第一个输入文档,另一个包含reduce输入文档。所以,这必须是mark as aswered。您可以要求另一个所需的输出,当然,我们将帮助您创建另一个样式表。或者你可以要求别人调试你的样式表,但你必须提供一个完整的。对不起。我想计算每个类通过和失败测试的数量。我的最终输出显示了一个类列表,其中包含通过和失败测试的对应数量。单击类将提供测试的进一步详细信息。@Alejandro:谢谢。这很有帮助。但是钥匙似乎没有给我归还任何东西。你能告诉我这到底是什么吗?TestMethod[count(.| key('class',@className)[1])=1]@Sidd:您是否运行了此转换?结果如何
TestMethod[count(.| key('class',@className)[1])=1]
是指:一个元素
TestMethod
,它与第一个元素
TestMethod
的并集计数后返回一个,该元素的键值等于给定元素的属性
className
。换句话说,第一个
TestMethod
有这样一个键值。@Alejandro:我所有的列中都有0。我尝试更改元素的名称空间,但仍然无法获得正确的数字。我已在文件中进行了这些更改。当我使用时,模板没有被应用。但是它被召唤进来了。但即便如此,值还是为0。我是不是遗漏了一些琐碎的东西?@Sidd:我已经用每个输入文档(你之前发布的文档和缩减的文档)运行了我的样式表,并得到了确切的结果。您是否使用这些输入文档运行这些样式表?结果如何?如果您正在针对另一个输入文档运行这些样式表,请重新发布。我不是算命师。。。
<TestRun id="41242257-adae-4e41-b860-f102021e93c8" name="muthuras@SMUTHURAJA2 2010-06-09 10:13:57" runUser="AMERICAS\muthuras" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2006">
    <ResultSummary outcome="Warning">
        <Counters total="3" executed="3" passed="3" error="0" failed="0" timeout="0" aborted="0" inconclusive="0" passedButRunAborted="0" notRunnable="0" notExecuted="0" disconnected="0" warning="0" completed="0" inProgress="0" pending="0" />
        <RunInfos>
            <RunInfo computerName="SMUTHURAJA2" outcome="Warning" timestamp="2010-06-09T10:13:59.6365402-04:00">
                <Text>Code coverage instrumentation warning while processing file ClassLibrary1.dll: Warning VSP2013 : Instrumenting this image requires it to run as a 32-bit process. The CLR header flags have been updated to reflect this.</Text>
            </RunInfo>
        </RunInfos>
    </ResultSummary>
    <Times creation="2010-06-09T10:13:57.3115402-04:00" queuing="2010-06-09T10:14:00.1315402-04:00" start="2010-06-09T10:14:00.3665402-04:00" finish="2010-06-09T10:14:02.2425402-04:00" />
    <TestDefinitions>
        <UnitTest name="EmptyUserIdInConstructor" storage="c:\users\muthuras\documents\visual studio 2008\projects\classlibrary1\testproject1.test\bin\debug\testproject1.test.dll" id="eeffb9fe-2a08-9a88-c3c9-3008a9aeeb50">
            <Css projectStructure="" iteration="" />
            <Owners>
                <Owner name="" />
            </Owners>
            <Execution id="ec93c5dc-afbb-41b6-81a1-157d39286eca" />
            <TestMethod codeBase="C:\Users\muthuras\Documents\Visual Studio 2008\Projects\ClassLibrary1\TestProject1.Test\bin\Debug\TestProject1.Test.dll" adapterTypeName="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestAdapter, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Adapter, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" className="TestProject1.Test.LogonInfoTest, TestProject1.Test" name="EmptyUserIdInConstructor" />
        </UnitTest>
        <UnitTest name="NullUserIdInConstructor" storage="c:\users\muthuras\documents\visual studio 2008\projects\classlibrary1\testproject1.test\bin\debug\testproject1.test.dll" id="e58f837c-2116-ce69-bf31-1fe6beec73d3">
            <Css projectStructure="" iteration="" />
            <Owners>
                <Owner name="" />
            </Owners>
            <Execution id="4ef585f1-02e0-4eb3-b291-29fa7b02d9e6" />
            <TestMethod codeBase="C:\Users\muthuras\Documents\Visual Studio 2008\Projects\ClassLibrary1\TestProject1.Test\bin\Debug\TestProject1.Test.dll" adapterTypeName="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestAdapter, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Adapter, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" className="TestProject1.Test.LogonInfoTest, TestProject1.Test" name="NullUserIdInConstructor" />
        </UnitTest>
        <UnitTest name="LogonInfoConstructorTest" storage="c:\users\muthuras\documents\visual studio 2008\projects\classlibrary1\testproject1.test\bin\debug\testproject1.test.dll" id="b9bbb3b6-cc0b-7f4d-276e-16c52b0814c6">
            <Css projectStructure="" iteration="" />
            <Owners>
                <Owner name="" />
            </Owners>
            <Execution id="fca1597d-5011-4d16-965b-afaa9d81ee4e" />
            <TestMethod codeBase="C:\Users\muthuras\Documents\Visual Studio 2008\Projects\ClassLibrary1\TestProject1.Test\bin\Debug\TestProject1.Test.dll" adapterTypeName="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestAdapter, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Adapter, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" className="TestProject1.Test.LogonInfoTest, TestProject1.Test" name="LogonInfoConstructorTest" />
        </UnitTest>
    </TestDefinitions>
    <TestLists>
        <TestList name="Results Not in a List" id="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
        <TestList name="All Loaded Results" id="19431567-8539-422a-85d7-44ee4e166bda" />
    </TestLists>
    <TestEntries>
        <TestEntry testId="b9bbb3b6-cc0b-7f4d-276e-16c52b0814c6" executionId="fca1597d-5011-4d16-965b-afaa9d81ee4e" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
        <TestEntry testId="e58f837c-2116-ce69-bf31-1fe6beec73d3" executionId="4ef585f1-02e0-4eb3-b291-29fa7b02d9e6" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
        <TestEntry testId="eeffb9fe-2a08-9a88-c3c9-3008a9aeeb50" executionId="ec93c5dc-afbb-41b6-81a1-157d39286eca" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
    </TestEntries>
    <Results>
        <UnitTestResult executionId="fca1597d-5011-4d16-965b-afaa9d81ee4e" testId="b9bbb3b6-cc0b-7f4d-276e-16c52b0814c6" testName="LogonInfoConstructorTest" computerName="SMUTHURAJA2" duration="00:00:00.0234997" startTime="2010-06-09T10:14:00.8325402-04:00" endTime="2010-06-09T10:14:01.3215402-04:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d">
            <Output />
        </UnitTestResult>
        <UnitTestResult executionId="4ef585f1-02e0-4eb3-b291-29fa7b02d9e6" testId="e58f837c-2116-ce69-bf31-1fe6beec73d3" testName="NullUserIdInConstructor" computerName="SMUTHURAJA2" duration="00:00:00.0047344" startTime="2010-06-09T10:14:01.3235402-04:00" endTime="2010-06-09T10:14:01.3305402-04:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d">
            <Output />
        </UnitTestResult>
        <UnitTestResult executionId="ec93c5dc-afbb-41b6-81a1-157d39286eca" testId="eeffb9fe-2a08-9a88-c3c9-3008a9aeeb50" testName="EmptyUserIdInConstructor" computerName="SMUTHURAJA2" duration="00:00:00.0005633" startTime="2010-06-09T10:14:01.3315402-04:00" endTime="2010-06-09T10:14:01.3345402-04:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d">
            <Output />
        </UnitTestResult>
    </Results>
</TestRun>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:test="http://microsoft.com/schemas/VisualStudio/TeamTest/2006" exclude-result-prefixes="test">
    <xsl:output indent="yes"/>
    <xsl:key name="class" match="test:TestMethod" use="@className"/>
    <xsl:key name="result" match="test:UnitTestResult" use="@testName"/>
    <xsl:template match="text()"/>
    <xsl:template match="/">
        <table>
            <tr>
                <td>Test Name</td>
                <td>Result</td>
                <td>Duration</td>
                <td>Passed</td>
                <td>Failed</td>
                <td>Inconclusive</td>
            </tr>
            <xsl:apply-templates/>
        </table>
    </xsl:template>
    <xsl:template match="test:TestMethod[count(.|key('class',@className)[1])=1]">
        <xsl:variable name="result" select="key('result',key('class',@className)/@name)"/>
        <tr>
            <td colspan="3">
                <xsl:value-of select="@className"/>
            </td>
            <td>
                <xsl:value-of select="count($result[@outcome='Passed'])"/>
            </td>
            <td>
                <xsl:value-of select="count($result[@outcome='Failed'])"/>
            </td>
            <td>
                <xsl:value-of select="count($result[@outcome='Inconclusive'])"/>
            </td>
        </tr>
        <xsl:apply-templates select="key('class',@className)" mode="sub"/>
    </xsl:template>
    <xsl:template match="test:TestMethod" mode="sub">
        <tr>
            <td>
                <xsl:value-of select="@name"/>
            </td>
            <td>
                <xsl:value-of select="key('result',@name)/@outcome"/>
            </td>
            <td>
                <xsl:value-of select="key('result',@name)/@duration"/>
            </td>
        </tr>
    </xsl:template>
</xsl:stylesheet>
<table>
<tr>
<td>Test Name</td>
<td>Result</td>
<td>Duration</td>
<td>Passed</td>
<td>Failed</td>
<td>Inconclusive</td>
</tr>
<tr>
<td colspan="3">TestProject1.Test.LogonInfoTest, TestProject1.Test</td>
<td>3</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>EmptyUserIdInConstructor</td>
<td>Passed</td>
<td>00:00:00.0005633</td>
</tr>
<tr>
<td>NullUserIdInConstructor</td>
<td>Passed</td>
<td>00:00:00.0047344</td>
</tr>
<tr>
<td>LogonInfoConstructorTest</td>
<td>Passed</td>
<td>00:00:00.0234997</td>
</tr>
</table>