XSLT格式化一个xml数据文件,使用HTML格式显示多个表格

时间:2013-07-02 01:58:13

标签: html xml xslt xslt-2.0 w3c

我有一个xml格式的数据文件,它包含有关测试的信息,我希望在浏览器中看到一个好看的视图,我想在不同的表格,所有测试信息表,每个模块测试信息表和测试中确切地知道这些信息案例表失败。

<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>
非常感谢!!!

1 个答案:

答案 0 :(得分: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选择表达式。