0

我有一个 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>

非常感谢!!!

4

1 回答 1

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(名为 的根节点的直接子 [ren] testsuites)。没关系,因为有这样一个节点。

但是对于第二个,您要求它将模板应用于/testsuite(根节点的直接子节点,名为testsuite)。不存在这样的节点。testcase和也是如此failure

您是否拥有与其他元素匹配的模板并不重要,因为您永远不会将模板应用于它们。

要解决此问题,请使用后代轴应用模板:

  <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选择表达式。

于 2013-07-02T10:51:02.857 回答