如何使用具有多个条件的单个xpath表达式查询所有后代节点

时间:2013-12-21 05:51:24

标签: xml xslt xpath xhtml xslt-grouping

nodes in xml file

图像显示xml文件中的节点。我需要找到失败的测试组。如果任何一个测试用例失败,则假定相应的测试组失败。在图像中,测试组计数失败= 2。

我在这里遇到的主要问题是,在XSLT中我不能在for-each循环中有一个计数器变量,也不能打破for-each循环。我是功能语言的新手。我不知道在XSLT中如何完成这么简单的事情,或者是否可以用XSLT这样的函数式语言来完成它。

给定的XSLT设法显示失败的测试组的位置。 然而,要求的是失败的测试组的数量。

XML的文件

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes" ?>
<testmodule starttime="2013-12-18 15:55:37" timestamp="   2.640000">
  <testgroup>
  <testgroup>
      <testcase starttime="2013-12-18 15:55:37" timestamp="   2.750000">
        <testlogfile file="" />
        <testpattern timestamp="   2.750000" name="statecheck" type="testprimitive">
          <title>Test   Signal</title>
          <teststep timestamp="   3.750000" level="1" type="auto" ident="Resume reason" result="na"> Elapsed time=1000ms (max=1000ms)</teststep>
          <teststep timestamp="   3.750000" level="2" type="auto" ident="1" result="pass">Waited for 1000 ms.</teststep>
          <teststep timestamp="   3.750000" level="0" type="auto" ident="" result="na">Check value condition.\nCAN signal &apos;TGW_AUD_MD_CAB_DR&apos; on bus CANI: = 0 (AM_SEL), condition: == 0 (AM_SEL)</teststep>
          <teststep timestamp="   3.750000" level="2" type="auto" ident="2" result="pass">Validation of the expected parameters</teststep>
          <result timestamp="   3.750000" result="pass" />
        </testpattern>
        <verdict time="2013-12-18 15:55:38" timestamp="   3.750000" endtime="2013-12-18 15:55:38" endtimestamp="   3.750000" result="pass" />
        <title>Test_Signal_Value(TGW_AUD_MD_CAB_DR, 0)</title>
        <ident>TF:9</ident>
      </testcase>
      <testcase starttime="2013-12-18 15:55:38" timestamp="   3.750000">
        <testlogfile file="" />
        <testpattern timestamp="   3.750000" name="capltestfunction Test_Confirmation_Dialog" type="testpattern">
          <title>Call confirmation dialog exe</title>
          <teststep timestamp="   4.750000" level="1" type="auto" ident="Resume reason" result="na"> Elapsed time=1000ms (max=1000ms)</teststep>
          <teststep timestamp="   4.750000" level="0" type="user" ident="User Verification Popup : " result="na">Verify Radio Main - AM source page displayed with 940 kHz frequency tuned</teststep>
          <teststep timestamp="   6.270000" level="1" type="auto" ident="Resume reason" result="na">Resumed on process termination, exit code = 0  Elapsed time=1520ms (max=4.29497e+009ms)</teststep>
          <teststep timestamp="   6.270000" level="0" type="user" ident="" result="pass">User Pressed Yes for : Verify Radio Main - AM source page displayed with 940 kHz frequency tuned</teststep>
          <result timestamp="   6.270000" result="pass" />
        </testpattern>
        <verdict time="2013-12-18 15:55:41" timestamp="   6.270000" endtime="2013-12-18 15:55:41" endtimestamp="   6.270000" result="fail" />
        <title>Test Confirmation(Verify Radio Main - AM source page displayed with 940 kHz frequency tuned)</title>
        <ident>TF:10</ident>
      </testcase>
      <testcase starttime="2013-12-18 15:55:41" timestamp="   6.270000">
        <testlogfile file="" />
        <testpattern timestamp="   6.270000" name="capltestfunction Test_Info_Dialog" type="testpattern">
          <title>Call test info dialog exe</title>
          <teststep timestamp="   7.270000" level="1" type="auto" ident="Resume reason" result="na"> Elapsed time=1000ms (max=1000ms)</teststep>
          <teststep timestamp="   8.580000" level="1" type="auto" ident="Resume reason" result="na">Resumed on process termination, exit code = 0  Elapsed time=1310ms (max=4.29497e+009ms)</teststep>
          <teststep timestamp="   8.580000" level="0" type="user" ident="" result="pass">User Pressed Yes for : User Action, Connect USB HDD with some audio media</teststep>
          <result timestamp="   8.580000" result="pass" />
        </testpattern>
        <verdict time="2013-12-18 15:55:44" timestamp="   8.580000" endtime="2013-12-18 15:55:44" endtimestamp="   8.580000" result="pass" />
        <title>Test Info(User Action, Connect USB HDD with some audio media )</title>
        <ident>TF:11</ident>
      </testcase>
    </testgroup>
  <testgroup>
      <testcase starttime="2013-12-18 15:55:37" timestamp="   2.750000">
        <testlogfile file="" />
        <testpattern timestamp="   2.750000" name="statecheck" type="testprimitive">
          <title>Test   Signal</title>
          <teststep timestamp="   3.750000" level="1" type="auto" ident="Resume reason" result="na"> Elapsed time=1000ms (max=1000ms)</teststep>
          <teststep timestamp="   3.750000" level="2" type="auto" ident="1" result="pass">Waited for 1000 ms.</teststep>
          <teststep timestamp="   3.750000" level="0" type="auto" ident="" result="na">Check value condition.\nsignal &apos;TGW_AUD_MD_CAB_DR&apos; on bus I: = 0 (AM_SEL), condition: == 0 (AM_SEL)</teststep>
          <teststep timestamp="   3.750000" level="2" type="auto" ident="2" result="pass">Validation of the expected parameters</teststep>
          <result timestamp="   3.750000" result="pass" />
        </testpattern>
        <verdict time="2013-12-18 15:55:38" timestamp="   3.750000" endtime="2013-12-18 15:55:38" endtimestamp="   3.750000" result="pass" />
        <title>Test_Signal_Value(TGW_AUD_MD_CAB_DR, 0)</title>
        <ident>TF:9</ident>
      </testcase>
      <testcase starttime="2013-12-18 15:55:38" timestamp="   3.750000">
        <testlogfile file="" />
        <testpattern timestamp="   3.750000" name="capltestfunction Test_Confirmation_Dialog" type="testpattern">
          <title>Call confirmation dialog exe</title>
          <teststep timestamp="   4.750000" level="1" type="auto" ident="Resume reason" result="na"> Elapsed time=1000ms (max=1000ms)</teststep>
          <teststep timestamp="   4.750000" level="0" type="user" ident="User Verification Popup : " result="na">Verify Radio Main - AM source page displayed with 940 kHz frequency tuned</teststep>
          <teststep timestamp="   6.270000" level="1" type="auto" ident="Resume reason" result="na">Resumed on process termination, exit code = 0  Elapsed time=1520ms (max=4.29497e+009ms)</teststep>
          <teststep timestamp="   6.270000" level="0" type="user" ident="" result="pass">User Pressed Yes for : Verify Radio Main - AM source page displayed with 940 kHz frequency tuned</teststep>
          <result timestamp="   6.270000" result="pass" />
        </testpattern>
        <verdict time="2013-12-18 15:55:41" timestamp="   6.270000" endtime="2013-12-18 15:55:41" endtimestamp="   6.270000" result="pass" />
        <title>Test Confirmation(Verify Radio Main - AM source page displayed with 940 kHz frequency tuned)</title>
        <ident>TF:10</ident>
      </testcase>
      <testcase starttime="2013-12-18 15:55:41" timestamp="   6.270000">
        <testlogfile file="" />
        <testpattern timestamp="   6.270000" name="capltestfunction Test_Info_Dialog" type="testpattern">
          <title>Call test info dialog exe</title>
          <teststep timestamp="   7.270000" level="1" type="auto" ident="Resume reason" result="na"> Elapsed time=1000ms (max=1000ms)</teststep>
          <teststep timestamp="   8.580000" level="1" type="auto" ident="Resume reason" result="na">Resumed on process termination, exit code = 0  Elapsed time=1310ms (max=4.29497e+009ms)</teststep>
          <teststep timestamp="   8.580000" level="0" type="user" ident="" result="pass">User Pressed Yes for : User Action, Connect USB HDD with some audio media</teststep>
          <result timestamp="   8.580000" result="pass" />
        </testpattern>
        <verdict time="2013-12-18 15:55:44" timestamp="   8.580000" endtime="2013-12-18 15:55:44" endtimestamp="   8.580000" result="fail" />
        <title>Test Info(User Action, Connect USB HDD with some audio media )</title>
        <ident>TF:11</ident>
      </testcase>
    </testgroup>
  </testgroup>
</testmodule>

XSLT

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<table border="1">
<tr>
    <xsl:for-each select="testmodule/testgroup/testgroup">
        <xsl:variable name="testCaseCount" select="position()" />
        <xsl:for-each select="./testcase">
                <xsl:variable name="result" select="./verdict/@result" />
                <xsl:if test="$result='fail' ">
                    <td><xsl:value-of select="concat('test case failed: ', $testCaseCount)"/></td>
                </xsl:if>   
        </xsl:for-each>
    </xsl:for-each> 
</tr>
</table>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

在XSLT中,计算内容的方法是使用count()函数:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/">
    <html>
      <body>
        <xsl:variable name="allTestGroups" 
                      select="testmodule/testgroup/testgroup" />
        <xsl:variable name="allTestCases" 
                      select="$allTestGroups/testcase" />
        <xsl:variable name="failedTestCases" 
                      select="$allTestCases[verdict/@result = 'fail']" />

        <!-- List names of failed test cases -->           
        <xsl:apply-templates select="$failedTestCases" />

        <xsl:variable name="failedCaseCount" select="count($failedTestCases)" />
        <xsl:variable 
                 name="failedGroupCount" 
                 select="count($allTestGroups[testcase/verdict/@result = 'fail'])" />

        <p>
          <xsl:value-of select="concat('There were ', 
                                       $failedCaseCount, 
                                       ' failed test cases.')"/>
        </p>
        <p>
          <xsl:value-of select="concat('There were ', 
                                       $failedGroupCount, 
                                       ' failed test groups.')"/>
        </p>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="testcase[verdict/@result = 'fail']">
    <p>
      <xsl:value-of select="concat('Test case &quot;', title, '&quot; failed.')"/>
    </p>
  </xsl:template>

</xsl:stylesheet>

在样本输入上运行时,会产生输出:

<html>
  <body>
    <p>Test case "Test Confirmation(Verify Radio Main - AM source page displayed with 940 kHz frequency tuned)" failed.</p>
    <p>Test case "Test Info(User Action, Connect USB HDD with some audio media )" failed.</p>
    <p>There were 2 failed test cases.</p>
    <p>There were 2 failed test groups.</p>
  </body>
</html>