我已經寫了一個xsl用於將mstest的trx文件轉換爲html。
從link開始,我無法獲得輸出中要打印的每個類的類名和傳遞和失敗次數。
我不知道我在哪裏錯了。樣式表應用於鏈接中的相同輸入文件。
謝謝。使用xslt中的鍵轉換mstest的trx文件
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:t="http://microsoft.com/schemas/VisualStudio/TeamTest/2006">
<xsl:param name="today"></xsl:param>
<xsl:param name="results"></xsl:param>
<xsl:param name="pass" select="'Passed'"/>
<xsl:param name="fail" select="'Failed'"/>
<xsl:param name="incon" select="'Inconclusive'"/>
<xsl:param name="error" select="'Error'"/>
<xsl:key name="class-key" match="@className" use="."/>
<xsl:variable name="unique-classes" select="//t:TestMethod/@className[generate-id(.)=generate-id(key('class-key',.))]" />
<xsl:template match="/">
<html>
<head>
<script type="text/javascript">
//Some javascript code
</script>
</head>
<body style="font-family:Verdana; font-size:10pt">
<a href="coverage.htm">Coverage Summary</a>
<xsl:call-template name="summary" />
<xsl:call-template name="details2" />
</body>
</html>
</xsl:template>
<xsl:template name="summary">
<h3>Test Summary</h3></code>
<table style="width:640;border:1px solid black;font-family:Verdana; font-size:10pt">
<tr>
<td style="font-weight:bold;">Total</td>
<td style="font-weight:bold;">Failed</td>
<td style="font-weight:bold;">Passed</td>
<td style="font-weight:bold;">Inconclusive</td>
<td style="font-weight:bold;">Error</td>
</tr>
<tr>
<td >
<xsl:value-of select="/t:TestRun/t:ResultSummary/t:Counters/@total"/>
</td>
<td style="background-color:pink;">
<xsl:value-of select="/t:TestRun/t:ResultSummary/t:Counters/@failed"/>
</td>
<td style="background-color:lightgreen;">
<xsl:value-of select="/t:TestRun/t:ResultSummary/t:Counters/@passed"/>
</td>
<td style="background-color:lightblue;">
<xsl:value-of select="/t:TestRun/t:ResultSummary/t:Counters/@inconclusive"/>
</td>
<td style="background-color:yellow;">
<xsl:value-of select="/t:TestRun/t:ResultSummary/t:Counters/@error"/>
</td>
</tr>
</table>
</xsl:template>
<xsl:template name="details2">
<h3>Unit Test Results</h3>
<table border="0" style="width:640;border:1px solid black;font-family:Verdana; font-size:10pt;">
<tr>
<td></td>
<td id="data" style="font-weight:bold;">Test Name</td>
<td id="data" style="font-weight:bold;">Result</td>
<td id="data" style="font-weight:bold;">Duration</td>
</tr>
<xsl:for-each select="$unique-classes">
<xsl:sort />
<xsl:variable name="curClass" select="."/>
<xsl:variable name="parentId" select="generate-id(./..)" />
<xsl:variable name="currentId" select="generate-id(.)" />
<tr id="{$parentId}">
<td id="{$currentId}"
style="font-weight:bold; cursor:pointer;"
onClick="toggleDetail(this)">[+]</td>
<xsl:sort select="@name"/>
<xsl:variable name="testid" select="../@id"/>
<xsl:with-param name="testid" select="."/>
<xsl:with-param name="curClass" select="."/>
<xsl:call-template name="groups" />
</tr>
<xsl:call-template name="classRunsDetail">
<xsl:with-param name="curClass" select="."/>
</xsl:call-template>
<tr id="{$currentId}-end" style="display:none;">
<td style="border-bottom:0px solid black;height:1px;background-color:black" colspan="4"></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
<xsl:template name="classRunsDetail">
<xsl:param name="curClass"/>
<xsl:variable name="parentId" select="generate-id(.)" />
<xsl:for-each select="//t:UnitTest/t:TestMethod[@className=$curClass]">
<xsl:sort select="@name"/>
<xsl:variable name="testid" select="../@id"/>
<xsl:for-each select="//t:UnitTestResult[@testId=$testid]">
<tr id="{$parentId}">
<xsl:attribute name="style">
<xsl:choose>
<xsl:when test="@outcome = $fail">background-color:pink;</xsl:when>
<xsl:when test="@outcome = $pass">background-color:lightgreen;</xsl:when>
<xsl:when test="@outcome = $incon">background-color:lightblue;</xsl:when>
<xsl:otherwise>background-color:yellow;</xsl:otherwise>
</xsl:choose>
display:none;
</xsl:attribute>
<td></td>
<td id="data">
<xsl:value-of select="@testName"/>
</td>
<td id="data">
<xsl:choose>
<xsl:when test="@outcome = $fail">FAILED</xsl:when>
<xsl:when test="@outcome = $pass">Passed</xsl:when>
<xsl:when test="@outcome = $incon">Not Run</xsl:when>
<xsl:otherwise>Error</xsl:otherwise>
</xsl:choose>
</td>
<td id="data">
<xsl:value-of select="@duration"/>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
<xsl:key name="class" match="t:TestMethod" use="@className"/>
<xsl:key name="result" match="t:UnitTestResult" use="@testName"/>
<xsl:template name="groups" match="t:TestMethod[count(.|key('class',@className)[1])=1]">
<xsl:variable name="result" select="key('result',key('class',@className)/@name)"/>
<td valign="bottom" style="background-color:beige;font-weight:bold;" 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>
</xsl:template>
</xsl:stylesheet>
這真是太棒了。非常感謝Alejandro。順便說一下,你能告訴我爲什麼使用for-each導致問題。我很樂意學習.. – Sidd 2010-06-28 22:53:08
@Sidd:我不認爲每個人的指示都是問題,但他們確實阻礙了維護。在聲明式範例中,你必須知道你的輸入和輸出,然後表達他們的關係。另外,這種特殊的轉換可以稱爲種羣:你已經有了一個佈局(XHTML),你必須填充數據。你可以放入一些錨(比如說@id),當你在「身份轉換」中找到它們(查找它)時,你插入數據(用document()函數輸入多個文檔)。看看這個網站:http://www.aranedabienesraices.com.ar – 2010-06-28 23:46:55