2013-07-02 37 views
0

我有一個xml格式的數據文件,它包含了關於測試的信息,我想在瀏覽器中獲得一個好看的視圖,我想要在不同的表格中精確的看到這些信息,所有測試信息表,每個模塊測試信息表和測試失敗案例表。XSLT格式一個xml數據文件以多種表格顯示使用HTML格式

<testsuites tests="111" failures="3" disabled="0" errors="0" time="60.947" name="AllTests"> 
    <testsuite name="ChdrvTestAout" tests="4" failures="0" disabled="0" errors="0" time="0"> 
    </testsuite> 
    <testsuite name="ChdrvTestTuner" tests="28" failures="3" disabled="0" errors="0" time="60.944"> 
    <testcase name="Case0001" status="run" time="0.001" classname="ChdrvTestTuner" /> 
    <testcase name="Case0007" status="run" time="27.271" classname="ChdrvTestTuner"> 
     <failure message="Value of: CHDRV_TEST_TUNER_0007()&#x0A; Actual: 0&#x0A;Expected: (1)&#x0A;Which is: 1" type=""><![CDATA[src/chdrv_tuner_test.cc:71 
Value of: CHDRV_TEST_TUNER_0007() 
    Actual: 0 
Expected: (1) 
Which is: 1]]></failure> 
    </testcase> 
    </testsuite> 
    <testsuite name="FactorialTest" tests="3" failures="0" disabled="0" errors="0" time="0"> 
    </testsuite> 
    <testsuite name="IsPrimeTest" tests="3" failures="0" disabled="0" errors="0" time="0"> 
    </testsuite> 
</testsuites> 

我想使用XSLT格式的HTML,以顯示多個表中的數據,我想表明在單獨的模塊表這個數據,如格式:

------------------------------------- 
module name | tests |  failures 
--------------------------------------- 
alltests  | 111  |  3 
-------------------------------------- 

------------------------------------- 
module name | tests |  failures 
--------------------------------------- 
ChdrvTestAout| 4  |  0 
-------------------------------------- 

------------------------------------- 
module name | tests |  failures 
--------------------------------------- 
ChdrvTestTuner| 28  |  3 
-------------------------------------- 

---------------------------------------------------------------------- 
casename | module    |  failed message 
---------------------------------------------------------------------- 
case0007 | ChdrvTestTuner  | src/chdrv_tuner_test.cc:71 
---------------------------------------------------------------------- 

請參閱我曾嘗試這裏http://www.pastebin.ca/2414163, 但它只顯示上面第一個「alltest」表? 如何編寫XSLT來做到這一點?非常感謝您的幫助

這裏是XSLT的 「/」 模板:

<xsl:template match="/"> 
    <html> 
    <body> 
    <h2 align="center">ChangHong driver test report!!!</h2> 
    <xsl:apply-templates select="testsuites"/> 
    <xsl:apply-templates select="testsuite"/> 
    <xsl:apply-templates select="testcase"/> 
    <xsl:apply-templates select="failure"/> 
    </body> 
    </html> 
</xsl:template> 

非常感謝!

+0

我試圖使用一些的xsl:模板等, '的 '它只顯示第一個模板格式。我不知道爲什麼它只顯示上面關於「alltest」的第一個表格。 你想看看我試過了嗎?但我不知道如何展示你? – gladman

+0

@LarsH請看我試過的東西,http://www.pastebin.ca/2414163 – gladman

回答

1

當這個樣式表被執行時,它首先將模板應用到根節點/。這會觸發你的xsl:template match="/"

在應用該模板時,上下文節點爲/。因此,當它處理

<xsl:apply-templates select="testsuites"/> 
    <xsl:apply-templates select="testsuite"/> 
    <xsl:apply-templates select="testcase"/> 
    <xsl:apply-templates select="failure"/> 

每個那些XPath表達式的相對於/評價。因此,對於第一個,您要求它將模板應用於/testsuites(即名爲testsuites的根節點的直接子節點[ren])。沒關係,因爲有這樣一個節點。

但對於第二個,你要求它將模板應用到/testsuite(根節點的直接子節點,名爲testsuite)。沒有這樣的節點存在。 testcasefailure也是如此。

不要緊,你有模板匹配那些其他元素,因爲你從來沒有將模板應用到他們。

要解決此問題,使用後代軸應用模板:

<xsl:apply-templates select="testsuites"/> 
    <xsl:apply-templates select="//testsuite"/> 
    <xsl:apply-templates select="//testcase"/> 
    <xsl:apply-templates select="//failure"/> 

順便說一句,在你的比賽模式,比如

<xsl:template match="//testsuite"> 

//沒有完成任何事情。匹配模式可以匹配文檔中任何位置的任何節點;就好像它是一個XPath表達式,其上下文節點是任意的。 做什麼的問題在於匹配節點已被其他地方的xsl:apply-templates選中。

另一種說法是,apply-templates select表達式需要明確地關於上下文,而match模式不需要。因此,您需要將//match模式移動到apply-templates選擇表達式。