2013-07-02 40 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选择表达式。