Martin
Martin

Reputation: 3472

How to query all descendant nodes using single xpath expression with multiple conditions

nodes in xml file

The image shows the nodes in the xml file. I need to find the no of failed testgroups. If any one testcase fails the corresponding testgroup is assumed failed. In the image, failed testgroup count = 2.

The main issue i face here is that in XSLT i cannot have a counter variable in the for-each loop neither can i break out of a for-each loop. I am totally new to functional languages. I dont know how such simple thing are done in XSLT or is it possible to do it in a functional language like XSLT.

The given XSLT manages to show the position of the failed test groups. However what is requied is the count of failed testgroups.

XML-file

<?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>

Upvotes: 0

Views: 191

Answers (1)

JLRishe
JLRishe

Reputation: 101730

In XSLT, the way to count something is to use the count() function:

<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>

When run on your sample input, this produces the output:

<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>

Upvotes: 1

Related Questions